-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
272 lines (249 loc) · 7.23 KB
/
main.lua
File metadata and controls
272 lines (249 loc) · 7.23 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
--import table
function table.clone(org)
return {table.unpack(org)}
end
function sign(num)
return num / math.abs(num)
end
love.graphics.setDefaultFilter('nearest', 'nearest')
local Util = {
pointInBox = function(pt, bx)
return (pt.x > bx.x) and
(pt.x < (bx.x + bx.w)) and
(pt.y > bx.yr) and
(pt.y < (bx.y + bx.h))
end,
aabb = function(one, two)
local oneL = one.x
local oneR = one.x + one.width
local twoL = two.x
local twoR = two.x + two.width
local oneT = one.y
local oneB = one.y + one.height
local twoT = two.y
local twoB = two.y + two.height
return (((oneL > twoL) and (oneL < twoR)) or
((oneR > twoL) and (oneR < twoR))) and
(((oneT > twoT) and (oneT < twoB)) or
((oneB > twoT) and (oneB < twoB)))
end
}
local Engine = {
totalTime = 0,
accumulator = 0,
timestep = 1 / 60,
zoomFactor = 2,
camera = { x = 0, y = 0, easing = 0, target = nil },
actors = {},
graphicCache = {},
inputState = {
left = { pressed = false, justPressed = false, justReleased = false },
right = { pressed = false, justPressed = false, justReleased = false },
button = { pressed = false, justPressed = false, justReleased = false }
},
touches = {},
screenButtonBoxes = {
left = { x = 0, y = 600, w = 150, h = 150 },
right = { x = 150, y = 600, w = 150, h = 150 },
button = { x = 300, y = 600, w = 150, h = 150 }
},
init = function(self)
function love.touchpressed(id, x, y, dx, dy, pressure)
touches[id] = { x = x, y = y, justPressed = true }
end
function love.touchmoved(id, x, y, dx, dy, pressure)
touches[id] = { x = x, y = y, justPressed = false }
end
function love.touchreleased(id, x, y, dx, dy, pressure)
touches[id] = nil
end
function love.update(dt)
self.totalTime = self.totalTime + dt
self.accumulator = self.accumulator + dt
if (self.accumulator >= self.timestep) then
local iterations = math.floor(self.accumulator / self.timestep)
self.accumulator = self.accumulator % self.timestep
for i = 1, iterations do
self:fixedUpdate()
end
end
end
function love.draw()
for y = 1, self.map.height do
for x = 1, self.map.width do
local tileframe = self.map.mapdata[y][x]
print(tileframe)
if tileframe > 0 then
love.graphics.draw(self.map.tileset.image,
self.map.tileset.frames[self.map.mapdata[y][x] + 1],
math.floor(((x - 1) * 16) - self.camera.x) * self.zoomFactor,
math.floor(((y - 1) * 16) - self.camera.y) * self.zoomFactor,
0,
self.zoomFactor)
end
end
end
for i = 1, table.getn(self.actors) do
local actor = self.actors[i]
local displayedFrame = 1
if actor.activeAnimation then
local animation = actor.animations[actor.activeAnimation]
local frameCount = table.getn(animation.frames)
local animationSeconds = ((actor.timeCreated + actor.elapsedSinceCreated) - actor.animationStart)
displayedFrame = 1 + math.floor((animationSeconds * animation.fps) % frameCount)
end
if (actor.graphic) then
love.graphics.draw(actor.graphic.image,
actor.graphic.frames[displayedFrame],
math.floor(actor.x - self.camera.x) * self.zoomFactor,
math.floor(actor.y - self.camera.y) * self.zoomFactor,
0,
self.zoomFactor)
end
end
end
end,
makeGraphic = function(self, imageFilename, frameWidth, frameHeight)
if not frameWidth then frameWidth = 0 end
if not frameHeight then frameHeight = 0 end
if ((not imageFilename) or (not love.filesystem.exists(imageFilename))) then
imageFilename = 'missing_graphic.png'
end
local cacheKey = imageFilename .. '_' .. frameWidth .. 'x' .. frameHeight
if self.graphicCache[cacheKey] then
return self.graphicCache[cacheKey]
end
local loadedImage = love.graphics.newImage(imageFilename)
local imageWidth, imageHeight = loadedImage:getDimensions()
frameWidth = math.min(imageWidth, frameWidth)
frameHeight = math.min(imageHeight, frameHeight)
local graphic = {
image = loadedImage,
frameWidth = frameWidth,
frameHeight = frameWidth,
width = imageWidth,
height = imageHeight,
frames = {},
frameCount = 0
};
if (frameWidth > 0) and
(frameHeight > 0) then
local gridWidth = math.floor(imageWidth / frameWidth)
local gridHeight = math.floor(imageHeight / frameHeight)
graphic.frameCount = gridWidth * gridHeight
for y = 0, gridHeight do
for x = 0, gridWidth do
local frameNum = 1 + x + (y * gridWidth)
graphic.frames[frameNum] = love.graphics.newQuad(
x * frameWidth,
y * frameHeight,
frameWidth,
frameHeight,
imageWidth,
imageHeight
)
end
end
else
graphic.frames[1] = love.graphics.newQuad(
0,
0,
imageWidth,
imageHeight,
imageWidth,
imageHeight
)
end
self.graphicCache[cacheKey] = graphic
return graphic
end,
fixedUpdate = function(self)
for i = 1, table.getn(self.actors) do
local actor = self.actors[i]
actor.elapsedSinceCreated = actor.elapsedSinceCreated + self.timestep
if (actor.behavior) then actor:behavior(self) end
if (math.abs(actor.acceleration.x) > 0) then
actor.velocity.x = math.max(actor.maxVelocity.x, actor.velocity.x + actor.acceleration.x)
elseif (actor.velocity.x > 0) then
local vsign = sign(actor.velocity.x)
actor.velocity.x = math.max(0, math.abs(actor.velocity.x) - actor.deceleration.x) * vsign
end
if (math.abs(actor.acceleration.y) > 0) then
actor.velocity.y = math.max(actor.maxVelocity.y, actor.velocity.y + actor.acceleration.y)
elseif (actor.velocity.y > 0) then
local vsign = sign(actor.velocity.y)
actor.velocity.y = math.max(0, math.abs(actor.velocity.y) - actor.deceleration.y) * vsign
end
actor.x = actor.x + actor.velocity.x
actor.y = actor.y + actor.velocity.y
end
end,
makeActor = function(self, x, y, w, h, gfx)
return {
x = x or 0,
y = y or 0,
width = w or 0,
height = h or 0,
graphic = gfx,
velocity = {
x = 0,
y = 0
},
maxVelocity = {
x = 0,
y = 0
},
acceleration = {
x = 0,
y = 0
},
deceleration = {
x = 0,
y = 0
},
animations = {},
activeAnimation = nil,
animationStart = 0,
timeCreated = self.totalTime,
elapsedSinceCreated = 0
}
end,
makeAnimation = function(self, frames, fps)
return {
frames = frames,
fps = fps
}
end
}
function Engine:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function playAnimation(actor, animationName, restart)
if(restart or (activeAnimation ~= animationName)) then
actor.activeAnimation = animationName
actor.animationStart = actor.timeCreated + actor.elapsedSinceCreated
end
end
local eng = Engine:new()
local newGfx = eng:makeGraphic('hero_sprites.png', 16 ,16)
local newActor = eng:makeActor(10, 10, 16, 16, newGfx)
newActor.animations['walk'] = eng:makeAnimation({1, 2}, 1)
newActor.behavior = function(self, engine)
self.velocity.y = 0.5
end
playAnimation(newActor, 'walk')
newActor.velocity.x = 0.2
eng.actors = { newActor }
local tileset = eng:makeGraphic('basictiles.png', 16 ,16)
local mapdata = {
{1,1,1,1,1},
{1,0,0,0,1},
{1,0,0,0,1},
{1,0,0,0,1},
{1,1,1,1,1}
}
eng.map = { tileset = tileset, mapdata = mapdata, width = 5, height = 5 }
eng:init()