-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.lua
More file actions
230 lines (174 loc) · 4.71 KB
/
Entity.lua
File metadata and controls
230 lines (174 loc) · 4.71 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
Entity = {}
Entity.__index = Entity
function Entity.new(x, y)
local self = setmetatable({}, Entity)
self.x = x or 0
self.y = y or 0
self.targetX = x or 0
self.targetY = y or 0
self.animationSpeed = 1
self.subItems = {}
return self
end
function Entity:childIndex(child)
assert(child ~= nil, "child can't be nil!")
for k, v in ipairs(self.subItems) do
if v == child then
return k
end
end
-- not found
return -1
end
function Entity:addChild(child)
assert(child ~= nil, "child can't be nil!")
-- look for item index
local idx = self:childIndex(child)
-- if not in the list, then add it
if idx == -1 then
table.insert(self.subItems, child)
end
end
function Entity:removeChild(child)
assert(child ~= nil, "child can't be nil!")
-- look for item index
local idx = self:childIndex(child)
if idx ~= -1 then
table.remove(self.subItems, idx)
end
end
function Entity:update(dt)
-- update animation
local speed = self.animationSpeed * dt
self.x = self.x + math.max(-speed, math.min(speed, self.targetX - self.x))
self.y = self.y + math.max(-speed, math.min(speed, self.targetY - self.y))
-- update child
for k, v in ipairs(self.subItems) do
v:update(dt)
end
end
function Entity:draw(parentX, parentY)
local _x = self.x + (parentX or 0)
local _y = self.y + (parentY or 0)
-- draw child
for k, v in ipairs(self.subItems) do
v:draw(_x, _y)
end
end
function Entity:animateTo(x, y, speed)
self.targetX = x
self.targetY = y
if speed then
self.animationSpeed = speed
else
animationSpeed = 1
self.x = x
self.y = y
end
end
function Entity:moveTo(x, y)
self:animateTo(x, y)
end
Button = {}
Button.image = love.graphics.newImage("Gfx/button.png")
Button.image:setFilter("nearest", "nearest")
Button.activeQuad = love.graphics.newQuad(0, 0, 80, 16, Button.image:getDimensions())
Button.quad = love.graphics.newQuad(0, 16, 80, 16, Button.image:getDimensions())
Button.smallImage = love.graphics.newImage("Gfx/button_small.png")
Button.smallImage:setFilter("nearest", "nearest")
Button.smallActiveQuad = love.graphics.newQuad(0, 0, 16, 16, Button.smallImage:getDimensions())
Button.smallQuad = love.graphics.newQuad(0, 16, 16, 16, Button.smallImage:getDimensions())
function Button.new(title, x, y, small)
local self = Entity.new(x, y)
self.active = false
self.small = small
self.title = title
self.draw = Button.draw
return self
end
function Button:draw(parentX, parentY)
local _x = self.x + parentX
local _y = self.y + parentY
love.graphics.setColor(255, 255, 255, 255)
local w = 80
local image = Button.image
local quad = Button.quad
if self.active then
quad = Button.activeQuad
end
if self.small then
w = 16
image = Button.smallImage
quad = Button.smallQuad
if self.active then
quad = Button.smallActiveQuad
end
end
if active then
love.graphics.draw(image, quad, _x, _y)
else
love.graphics.draw(image, quad, _x, _y)
end
if self.active then
local t = math.floor(love.timer.getTime()*8)
if t % 2 == 0 then
love.graphics.setColor(128, 128, 128, 255)
end
end
love.graphics.setFont(gameFont)
love.graphics.printf(self.title, _x, _y + 4, w, "center")
Entity.draw(self, parentX, parentY)
end
Text = {}
function Text.new(text, x, y, width, align, font)
local self = Entity.new(x, y)
self.text = text
self.draw = Text.draw
self.width = width
self.align = align
self.font = font
return self
end
function Text:draw(parentX, parentY)
local _x = self.x + parentX
local _y = self.y + parentY
if not self.font then
love.graphics.setFont(gameFont)
else
love.graphics.setFont(self.font)
end
if self.color then
love.graphics.setColor(self.color.r, self.color.g, self.color.b, self.color.a)
else
love.graphics.setColor(255, 255, 255, 255)
end
if self.width and self.align then
love.graphics.printf(self.text, _x, _y, self.width, self.align)
else
love.graphics.print(self.text, _x, _y)
end
Entity.draw(self, parentX, parentY)
end
Sprite = {}
function Sprite.new(image, quad, x, y)
local self = Entity.new(x, y)
self.quad = quad
self.image = image
self.draw = Sprite.draw
return self
end
function Sprite:draw(parentX, parentY)
local _x = self.x + parentX
local _y = self.y + parentY
love.graphics.setColor(255, 255, 255, 255)
if quad then
love.graphics.draw(self.image, self.quad, _x, _y)
else
love.graphics.draw(self.image, _x, _y)
end
Entity.draw(self, parentX, parentY)
end
gameFont = love.graphics.newImageFont("Gfx/font.png"," !\"#$%&`()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~µ", 1)
gameFont:setFilter("nearest", "nearest")
bigFont = love.graphics.newImageFont("Gfx/BigFont.png"," !\"#$%&`()*+,-./0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ@[\\]^_~", 0)
bigFont:setFilter("nearest", "nearest")