-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweapon.lua
52 lines (39 loc) · 1.15 KB
/
weapon.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
local entity = require "entity"
local _M = {}
local _MT = {__index = _M}
local _MMT = {__index = entity}
setmetatable(_M, _MMT)
Bullet = _M
-- graphics
local GFX_Bullet = love.graphics.newImage("gfx/Bullet.png")
local function set_texture(self, texture)
self.texture = texture
self.width = texture:getWidth()
self.height = texture:getHeight()
end
--- Create new bullet
--@param `t` should always be the ship firing the bullet
function Bullet.new(owner, t)
t = t or {}
local e = entity.new()
e.kind = 'bullet'
e.pos_x = t.pos_x or owner.pos_x or 0
e.pos_y = t.pos_y or owner.pos_y or 0
e.dir_x = t.dir_x or 1
e.dir_y = t.dir_y or 0
e.speed = t.speed or 200;
e.radius = t.radius or 1;
set_texture(e, GFX_Bullet)
e.damage = t.damage or 10;
return setmetatable(e, _MT)
end
function _M:update(dt)
entity.update(self, dt)
self.state = self.pos_x <= 0 and 'remove' or self.state
self.state = self.pos_x > SCREEN_WIDTH and 'remove' or self.state
end
function _M:collidewith(e, dt)
assert(e and e._TYPE == 'entity', string.format("Expected 'entity', got '%s' instead.", e and e.type or type(e)))
e:dohit(self.damage)
end
return _M