-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_object.lua
111 lines (90 loc) · 2.64 KB
/
game_object.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
106
107
108
109
110
111
local GameObject = Object:extend()
local resource_manager = require("resource_manager")
local utilities = require("utilities")
local flashTime = 0.1
function GameObject:new(x, y, quadName, scale, hitboxProportion)
self.x = x or 0
self.y = y or 0
self.quadName = quadName or nil
self.scale = scale or 1
self.hitBoxProportion = hitboxProportion or 1.0
self.offsetX = 0
self.offsetY = 0
self.height = 15
self.width = 30
self.rotation = 0
self.originX = 0
self.originX = 0
GameObject.recalcScale(self)
self.r = 1
self.g = 1
self.b = 1
self.a = 1
self.time = 0
self.flashStartTime = 0
self.flashing = false
self.disabled = false
end
function GameObject:recalcScale()
if not self.quadName then
return
end
local image, quad = resource_manager.getQuad(self.quadName)
local x, y, width, height = quad:getViewport()
self.offsetX = -(self.scale*width*(1.0 - self.hitBoxProportion))/2
self.offsetY = -(self.scale*height*(1.0 - self.hitBoxProportion))/2
self.width = width*self.scale*self.hitBoxProportion
self.height= height*self.scale*self.hitBoxProportion
end
function GameObject:setColor(r, g, b, a)
self.r = r
self.g = g
self.b = b
self.a = a
end
function GameObject:getHeight()
return self.height
end
function GameObject:getWidth()
return self.width
end
function GameObject:getX()
return self.x
end
function GameObject:getY()
return self.y
end
function GameObject:setQuadName(quadName)
self.quadName = quadName
end
function GameObject:startFlash()
self.flashing = true
self.flashStartTime = self.time
end
function GameObject:update(dt)
self.time = self.time + dt
if self.time > (self.flashStartTime + flashTime) then
self.flashing = false
end
end
function GameObject:draw()
if self.flashing then
love.graphics.setShader(resource_manager.getShader("white"))
elseif self.disabled then
love.graphics.setShader(resource_manager.getShader("grey"))
else
love.graphics.setShader()
end
love.graphics.setColor(self.r,self.g,self.b,self.a)
if self.quadName then
local image, quad = resource_manager.getQuad(self.quadName)
love.graphics.draw(image, quad, self.x + self.offsetX, self.y + self.offsetY, self.rotation*(180.0/math.pi), self.scale, self.scale, self.originX, self.originY)
else
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end
if utilities.getShowHitbox() then
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
love.graphics.setShader()
end
return GameObject