-
Notifications
You must be signed in to change notification settings - Fork 1
/
gob.lua
105 lines (90 loc) · 2.47 KB
/
gob.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Gob = {
x = 0,
y = 0,
angle = 0,
time = 0.0,
fps = 12,
sprite = nil,
currentFrame = 1,
alive = true,
visible = true,
}
function Gob:new(data)
o = {}
setmetatable(o, self)
self.__index = self
if data then
for k, v in pairs(data) do
o[k] = v
end
end
return o
end
function Gob:draw()
if (not self.visible) then
return
end
local sprite = self.sprite
love.graphics.setColor(255, 255, 255, sprite.opacity * 255)
if (sprite.anim) then
love.graphics.drawq(
sprite.image,
sprite.anim.quads[self.currentFrame],
self.x, self.y, -self.angle,
sprite.scale.x, sprite.scale.y, sprite.center.x, sprite.center.y)
else
love.graphics.draw(
sprite.image,
self.x, self.y, -self.angle,
sprite.scale.x, sprite.scale.y, sprite.center.x, sprite.center.y)
end
end
function Gob:drawHitbox()
if (not self.alive) then
return
end
local s = self.sprite
if (s) then
love.graphics.setColor(255, 255, 0)
love.graphics.rectangle(
"line", self.x, self.y, self:getWidth(), self:getHeight())
love.graphics.setColor(0, 255, 255)
love.graphics.rectangle(
"line", self.x + s.hitbox.x, self.y + s.hitbox.y,
s.hitbox.w, s.hitbox.h)
end
end
function Gob:getWidth()
return self.sprite:getWidth()
end
function Gob:getHeight()
return self.sprite:getHeight()
end
function Gob:update(dt)
for k,v in ipairs(getmetatable(self)) do
print(k .. ": " .. v)
end
self.time = self.time + dt
if (self.sprite.anim) then
self.currentFrame = (math.floor(self.time * self.fps) % self.sprite.anim.count) + 1
end
end
function Gob:hitGob(obj)
if (not self.alive or not obj.alive) then
return false
end
return rectsIntersect(
self:getWorldSpaceBbox(),
obj:getWorldSpaceBbox())
end
function Gob:getWorldSpaceBbox()
local sprite = self.sprite
local ul_x = self.x + sprite.hitbox.x - sprite.center.x
local ul_y = self.y + sprite.hitbox.y - sprite.center.y
local br_x = ul_x + sprite.hitbox.w
local br_y = ul_y + sprite.hitbox.h
return { ul_x = ul_x, ul_y = ul_y, br_x = br_x, br_y = br_y }
end
function rectsIntersect(rect1, rect2)
return not (rect1.ul_x > rect2.br_x or rect1.br_x < rect2.ul_x or rect1.ul_y > rect2.br_y or rect1.br_y < rect2.ul_y)
end