-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgun.lua
executable file
·61 lines (50 loc) · 1.62 KB
/
gun.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local global = require "global"
local class = require "middleclass.middleclass"
local Gun = class("Gun")
local Bullet = require "bullet"
local speed = 100
function Gun:initialize(radius, damage, ammo, cooldown, reload, accuracy)
self.damage = damage
self.ammo = ammo
self.maxammo = ammo
self.cooldown = 0
self.maxcooldown = cooldown
self.maxreload = reload
self.accuracy = accuracy -- bullet scatter
self.radius = radius
end
function Gun:update(dt)
if self.cooldown > 0 then
self.cooldown = self.cooldown - dt
end
end
-- x, y - where the bullet initially appears (not including radius of the gun)
function Gun:fire(x, y, target)
if global.endGame or self.ammo <= 0 or self.cooldown > 0 then
return
end
-- Get the target coordinates (fuzzed slightly)
local tx = target.x + love.math.random(-self.accuracy, self.accuracy)
local ty = target.y + love.math.random(-self.accuracy, self.accuracy)
-- Get the vector
local dx = tx - x
local dy = ty - y
-- And normalise to speed
local mag = math.sqrt(dx*dx + dy*dy)
local vx = dx / mag * speed
local vy = dy / mag * speed
-- Spawn a bullet
global.addDrawable(Bullet:new(x + (dx / mag * self.radius),
y + (dy / mag * self.radius),
vx, vy, self.damage))
self.ammo = self.ammo - 1
-- Reload if we have no ammo, and set the cooldown
-- appropriately
if self.ammo == 0 then
self.cooldown = self.maxreload
self.ammo = self.maxammo
else
self.cooldown = self.maxcooldown
end
end
return Gun