-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.lua
410 lines (375 loc) · 11.2 KB
/
level.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
local Camera = require 'camera'
local Collectible = require 'collectible'
local Colors = require 'colors'
local Craft = require 'craft'
local Door = require 'door'
local Goal = require 'goal'
local LevelData = require 'leveldata'
local Particles = require 'particles'
local Pond = require 'pond'
local SharedState = require 'sharedstate'
local Level = {
segments = {},
ponds = {},
collectibles = {},
doors = {},
size = { width = 0, height = 0 },
backgroundSegments = {},
goal = nil,
palettes = nil,
numCollectiblesHeld = 0,
initialPlayerPosition = {},
restartTimer = 0,
Event = {
NONE = 0,
COLLECT = 1,
PLAYER_DEATH = 2,
ENTER_DOOR = 3,
GAME_OVER = 4,
RESTART = 5,
},
}
local _GRID_SIZE = 64
function Level:reset()
--[[for index = 1, 20 do
self.segments[index] = math.random(0, 3)
end--]]
self.numCollectiblesHeld = 0
end
function Level:update(dt)
for index, pond in pairs(self.ponds) do
pond:update(dt)
end
for index, collectible in pairs(self.collectibles) do
collectible:update(dt)
end
if SharedState.environment.windy then
if math.random() < 0.2 then
Particles:wind()
end
end
if self.goal then
self.goal:update(dt)
end
if self.restartTimer > 0 then
self.restartTimer = self.restartTimer - dt
if self.restartTimer <= 0 then
self.restartTimer = -1
end
end
Particles:update(dt)
end
function Level:interactWith(craft)
-- goal
if self.goal then
local result = self.goal:interactWith(craft)
if result == Goal.Event.REACHED then
craft.state = craft.states.HIDDEN
Goal:animate(craft)
elseif result == Goal.Event.FINISHED then
return self.Event.GAME_OVER
end
end
-- death timer
if craft.state == craft.states.HIDDEN then
if self.restartTimer < 0 then
self.restartTimer = 0
return self.Event.RESTART
else
-- preclude other interaction
return self.Event.NONE
end
end
-- terrain
if self:collidesWith(craft.position.x, craft.position.y) then
self:_onPlayerDeath(craft)
return self.Event.PLAYER_DEATH
end
-- ponds
for index, pond in pairs(self.ponds) do
local result = pond:interactWith(craft)
if result == Pond.Event.PLAYER_DEATH then
self:_onPlayerDeath(craft)
return self.Event.PLAYER_DEATH
end
end
-- collectibles
for index, collectible in pairs(self.collectibles) do
local result = collectible:interactWith(craft)
if result == Collectible.Event.COLLECT then
self.collectibles[index] = nil
self:_onPlayerCollect(collectible)
return self.Event.COLLECT
end
end
-- door
for index, door in pairs(self.doors) do
local result = door:interactWith(craft)
if result == Door.Event.ENTER then
return self.Event.ENTER_DOOR, door
end
end
return self.Event.NONE
end
function Level:collidesWith(x, y)
local groundY = self:_getHeightAtX(x)
return y >= groundY
end
function Level:getGroundBaseline()
return SharedState.viewport.height - 32
end
function Level:draw()
local cameraPosition = Camera:getPosition()
love.graphics.push()
love.graphics.translate(-cameraPosition.x, -cameraPosition.y)
for index, pond in pairs(self.ponds) do
pond:draw()
end
for index, collectible in pairs(self.collectibles) do
collectible:draw()
end
for index, door in pairs(self.doors) do
door:draw(self.palettes[1])
end
if self.goal then
self.goal:draw()
end
Craft:draw(self.palettes[1])
Colors.useColorInterpPalettes(self.palettes, Camera:getXInterp(), Colors.Value.TERRAIN)
self:_drawSegments(self.segments, 0, self:getGroundBaseline(), _GRID_SIZE)
Particles:draw()
love.graphics.pop()
Particles:drawForeground()
if self.goal then
self.goal:drawForeground()
end
end
function Level:drawBackground()
local cameraPosition = Camera:getPosition()
self:drawSky()
love.graphics.push()
love.graphics.translate(-cameraPosition.x * 0.85, -cameraPosition.y * 0.9)
self:_drawBackgroundLayer()
love.graphics.pop()
end
function Level:_drawSegments(segments, xStart, yStart, gridSize)
local prevHeight
local xx, yy = xStart, yStart
for index, height in pairs(segments) do
if prevHeight then
love.graphics.polygon(
'fill',
xx - gridSize, yy - prevHeight * gridSize,
xx, yy - height * gridSize,
xx, SharedState.viewport.height,
xx - gridSize, SharedState.viewport.height
)
end
prevHeight = height
xx = xx + gridSize
end
end
function Level:_getSegmentIndexAtX(xx)
return math.floor(xx / _GRID_SIZE) + 1
end
function Level:_getHeightAtX(xx)
local baseline = self:getGroundBaseline()
local collidingSegment = self:_getSegmentIndexAtX(xx)
local left = { x = (collidingSegment - 1) * _GRID_SIZE, y = baseline }
local right = { x = collidingSegment * _GRID_SIZE, y = baseline }
local nSegments = table.getn(self.segments)
if collidingSegment > 0 then
left.y = baseline - self.segments[collidingSegment] * _GRID_SIZE
end
if collidingSegment < nSegments then
right.y = baseline - self.segments[collidingSegment + 1] * _GRID_SIZE
end
local interp = (xx - left.x) / _GRID_SIZE
return left.y + (right.y - left.y) * interp
end
function Level:_drawCollisionTest(collidingX)
local collidingSegment = self:_getSegmentIndexAtX(collidingX)
if (collidingSegment > 0 and collidingSegment < nSegments) then
love.graphics.setColor(1, 0, 0, 1)
love.graphics.line(
(collidingSegment - 1) * _GRID_SIZE, yy - self.segments[collidingSegment] * _GRID_SIZE,
(collidingSegment) * _GRID_SIZE, yy - self.segments[collidingSegment + 1] * _GRID_SIZE
)
end
local collideY = self:_getHeightAtX(collidingX)
love.graphics.circle('line', collidingX, collideY, 10)
end
function Level:drawSky()
local color1 = { r = 87, g = 136, b = 98 }
local color2 = { r = 227, g = 207, b = 126 }
local numRegions = 12
local regionSize = SharedState.viewport.height / numRegions
for i = 0, numRegions - 1 do
local interp = i / (numRegions - 1)
Colors.useInterpColorInterpPalettes(
self.palettes, Camera:getXInterp(),
Colors.Value.SKYTOP, Colors.Value.SKYBOTTOM, interp
)
love.graphics.rectangle(
'fill',
0, i * regionSize,
SharedState.viewport.width, regionSize
)
end
end
function Level:_drawBackgroundLayer()
Colors.useColorInterpPalettes(self.palettes, Camera:getXInterp(), Colors.Value.BACKGROUND)
self:_drawSegments(self.backgroundSegments, 0, self:getGroundBaseline() - 32, _GRID_SIZE * 0.8)
end
function Level:_onPlayerDeath(craft)
Particles:playerDeath(craft)
craft.state = craft.states.HIDDEN
self.restartTimer = 1
end
function Level:_onPlayerCollect(collectible)
if collectible.shape == Collectible.Shapes.BORING then
self.numCollectiblesHeld = self.numCollectiblesHeld + 1
elseif collectible.shape == Collectible.Shapes.SPECIAL then
SharedState.boost = 1
elseif collectible.shape == Collectible.Shapes.UPGRADE then
SharedState.isBoostEnabled = true
Particles:doorOpened(collectible)
end
if self.levelId == 1 then
if self.numCollectiblesHeld == 1 then
self.doors[2]:open()
end
elseif self.levelId == 2 then
if self.numCollectiblesHeld == 4 then
self.doors[2]:open()
end
elseif self.levelId == 3 then
if self.numCollectiblesHeld == 3 then
self.doors[2]:open()
end
elseif self.levelId == 5 then
if self.numCollectiblesHeld == 6 then
self.doors[2]:open()
end
if collectible.shape == Collectible.Shapes.SPECIAL then
self.doors[1]:open()
end
elseif self.levelId == 6 then
if self.numCollectiblesHeld == 2 then
self.doors[2]:open()
end
if collectible.shape == Collectible.Shapes.SPECIAL then
self.doors[1]:open()
end
elseif self.levelId == 7 then
self.doors[1]:open()
end
end
function Level:loadLevelData(data, initialDoorIndex)
self.levelId = data.id
self.palettes = data.palettes or { Colors.Palette.START }
initialDoorIndex = initialDoorIndex or 1
if data.windy then
SharedState:setEnvironment({ windy = true })
else
SharedState:setEnvironment({ windy = false })
end
if data.background then
self.backgroundSegments = data.background
else
self.backgroundSegments = LevelData.DefaultBackground
end
self.segments = {}
local maxSegment = 0
for index, segment in pairs(data.segments) do
table.insert(self.segments, segment)
if segment > maxSegment then maxSegment = segment end
end
local maxHeight = (SharedState.viewport.height - self:getGroundBaseline()) + (_GRID_SIZE * maxSegment) + 200
self.size = {
width = (table.getn(self.segments) - 1) * _GRID_SIZE,
height = math.max(SharedState.viewport.height, maxHeight),
}
self.ponds = {}
for index, pond in pairs(data.ponds) do
local x = pond.index * _GRID_SIZE
local position = {
x = x,
y = self:_getHeightAtX(x) + 12,
}
table.insert(
self.ponds,
Pond:new({
position = position,
size = {
width = _GRID_SIZE * pond.width,
height = _GRID_SIZE * pond.height,
}
})
)
end
self.collectibles = {}
for index, collectible in pairs(data.collectibles) do
local x, y, shape
if collectible.index then
x = collectible.index * _GRID_SIZE
y = self:_getHeightAtX(x) - 6
if collectible.hover then
y = y - collectible.hover
end
end
if collectible.shape then
shape = collectible.shape
else
shape = Collectible.Shapes.BORING
end
local isCollected = (shape == Collectible.Shapes.BORING and SharedState.isBoostEnabled)
if not isCollected then
table.insert(
self.collectibles,
Collectible:new({
position = {
x = x,
y = y,
},
shape = shape,
})
)
end
end
self.doors = {}
for index, door in pairs(data.doors) do
local doorX, isOpen = 0, false
if door.x > 0 then doorX = door.x else doorX = self.size.width + door.x end
if door.isOpen then isOpen = true end
table.insert(
self.doors,
Door:new({
position = {
x = doorX,
y = door.y,
},
destination = door.destination,
isOpen = door.isOpen,
color = door.color,
})
)
if index == initialDoorIndex then
self.initialPlayerPosition = { x = doorX, y = door.y }
end
end
if data.goal then
local x, y
x = data.goal.index * _GRID_SIZE
y = self:_getHeightAtX(x)
self.goal = Goal:new({
position = {
x = x,
y = y,
},
})
else
self.goal = nil
end
end
return Level