-
Notifications
You must be signed in to change notification settings - Fork 88
/
GameObject.lua
62 lines (53 loc) · 1.75 KB
/
GameObject.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
GameObject = Object:extend()
function GameObject:new(area, x, y, opts)
local opts = opts or {}
if opts then for k, v in pairs(opts) do self[k] = v end end
self.area = area
self.x, self.y = x, y
self.id = UUID()
self.creation_time = love.timer.getTime()
self.timer = Timer()
self.dead = false
self.depth = 50
self.previous_collisions = {}
self.current_collisions = {}
end
function GameObject:update(dt)
if self.timer then self.timer:update(dt) end
if self.shape then
self.previous_collisions = table.copy(self.current_collisions)
self.current_collisions = {}
end
end
function GameObject:draw()
end
function GameObject:destroy()
self.timer:destroy()
if self.shape then HC.remove(self.shape) end
self.shape = nil
end
function GameObject:enter(tag)
local shapes = {}
for shape in pairs(HC.neighbors(self.shape)) do
if shape.tag == tag then
if self.shape:collidesWith(shape) then
self.current_collisions[shape.id] = true
if not self.previous_collisions[shape.id] then table.insert(shapes, shape) end
end
end
end
return shapes
end
function GameObject:enemyProjectileCollisions()
for _, shape in ipairs(self:enter('Projectile')) do
local object = shape.object
if object then
self:hit(object.damage, object.x, object.y, object.r)
if self.hp <= 0 then current_room.player:onKill(self) end
if object.pierce > 0 then
object.pierce = object.pierce - 1
if object.attack == 'Explode' then object.area:addGameObject('Explosion', object.x, object.y, {color = self.color}) end
else object:die() end
end
end
end