-
Notifications
You must be signed in to change notification settings - Fork 0
/
collidable.lua
executable file
·75 lines (61 loc) · 1.67 KB
/
collidable.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local class = require "middleclass.middleclass"
local Drawable = require "drawable"
local Collidable = class("Collidable", Drawable)
local global = require "global"
function Collidable:initialize(x, y, maxhp)
Drawable.initialize(self, x, y)
self.stopsHumans = true
self.maxhp = maxhp
self.hp = maxhp
-- this is wrong, but will disappear quickly
self.centerx = x
self.centery = y
self.is_ghost = false
end
function Collidable:update(dt)
-- self.hitbox should probably never be nil, but I can't think of a
-- good default hitbox to add.
if self.hitbox ~= nil then
local hx0, hy0, hx1, hy1 = self.hitbox:bbox()
local hw = hx1 - hx0
local hh = hy1 - hy0
self.centerx = self.x + hw/2
self.centery = self.y + hh/2
self.hitbox:moveTo(self.centerx, self.centery)
end
Drawable.update(self, dt)
end
function Collidable:stopsBullets()
return not self.is_ghost
end
function Collidable:center()
return self.centerx, self.centery
end
function Collidable:onCollision(other, dx, dy)
print("Collidable colliding")
end
function Collidable:hurt(damage)
self.hp = self.hp - damage
if self.hp <= 0 then
self:destroy()
end
end
function Collidable:destroy()
if self.hitbox ~= nil then
global.removeHitbox(self.hitbox)
end
Drawable.destroy(self)
end
function Collidable:ghost()
if not self.is_ghost and self.hitbox ~= nil then
self.is_ghost = true
global.setGhost(self.hitbox)
end
end
function Collidable:solidify()
if self.is_ghost and self.hitbox ~= nil then
self.is_ghost = false
global.setSolid(self.hitbox)
end
end
return Collidable