This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
523 lines (456 loc) · 15.1 KB
/
main.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
require("model")
require("sound")
require("donut")
require("state")
require("entity")
FIELD_SIZE = 25
musicChannel = nil
paused = false
gravity = {
x= 0,
y= 0.4
}
level = nil
player = Entity.create("player")
player.dx=1
player.speed=0.4
player.maxSpeed=0.1
player.maxSpeedY = 0.05
game = {
state = State.create("running"),
keys = 0,
debug = true,
currentLevel = 1
}
entities = {}
cam = {x=0, y=0}
sprites = nil
musicDelay = 4
musicPaused = false
function loadSprites()
sprites = {}
spritesImage = love.graphics.newImage("entities.png")
sprites.playerRight = love.graphics.newQuad(0, 0, FIELD_SIZE, FIELD_SIZE, spritesImage:getWidth(), spritesImage:getHeight())
sprites.playerLeft = love.graphics.newQuad(FIELD_SIZE, 0, FIELD_SIZE, FIELD_SIZE, spritesImage:getWidth(), spritesImage:getHeight())
sprites.playerUp = love.graphics.newQuad(FIELD_SIZE * 2, 0, FIELD_SIZE, FIELD_SIZE, spritesImage:getWidth(), spritesImage:getHeight())
sprites.playerDead = love.graphics.newQuad(FIELD_SIZE * 3, 0, FIELD_SIZE, FIELD_SIZE, spritesImage:getWidth(), spritesImage:getHeight())
sprites.monsterRight = love.graphics.newQuad(0, FIELD_SIZE, FIELD_SIZE, FIELD_SIZE, spritesImage:getWidth(), spritesImage:getHeight())
sprites.monsterLeft = love.graphics.newQuad(FIELD_SIZE, FIELD_SIZE, FIELD_SIZE, FIELD_SIZE, spritesImage:getWidth(), spritesImage:getHeight())
end
function love.load()
--game.currentLevel = 1
debug = Donut.init(10, 10)
debug.toggle()
fps = debug.add("FPS")
--random = debug.add("Random")
debug_currentTile = debug.add("Current tile")
love.mouse.setVisible(false)
loadSprites()
loadSounds()
loadMusic()
debug_player_x = debug.add("Player.x")
debug_player_y = debug.add("Player.y")
debug_player_vx = debug.add("Player.vx")
debug_player_vy = debug.add("Player.vy")
debug_player_state = debug.add("Player.state")
debug_game_state = debug.add("Game.state")
debug_keypressed = debug.add("keypressed")
debug_sometext = debug.add("debugsometext")
debug_keys = debug.add("keys")
musicChannel = playMusic(musics.main)
musicChannel:setVolume(0.0)
start()
end
function start()
loadLevel(game.currentLevel)
z = TiledMap_GetLayerZByName("blocks")
game.state:setState("running")
player.state:setState("start", 1, "stand")
end
function getBoundingBox(entity)
-- entity coords always based on feet
return {
x1 = entity.x - 0.4,
y1 = entity.y + 0.1,
x2 = entity.x + 0.4,
y2 = entity.y + 1.0
}
end
function checkRectCollision(rect1, rect2)
left1 = rect1.x1
left2 = rect2.x1
top1 = rect1.y1
top2 = rect2.y1
right1 = rect1.x2
right2 = rect2.x2
bottom1 = rect1.y2
bottom2 = rect2.y2
if bottom1 <= top2 then
return false
end
if top1 >= bottom2 then
return false
end
if right1 <= left2 then
return false
end
if left1 >= right2 then
return false
end
return true
end
function updateEntity(entity, dt)
local dirX = 0
local dirY = 0
entity.state:step(dt)
if entity.state.name == "dead" or entity.state.name == "exit" then
return
end
-- gravity
if entity.state.name ~= "climb" then
entity.vy = entity.vy + gravity.y * dt
end
if entity.vx ~= 0 then
dirX = entity.vx / math.abs(entity.vx)
end
if entity.vy ~= 0 then
dirY = entity.vy / math.abs(entity.vy)
end
if math.abs(entity.vx) > entity.maxSpeed then
entity.vx = entity.maxSpeed * dirX
end
if entity.state.name == "climb" and math.abs(entity.vy) > entity.maxSpeedY then
entity.vy = entity.maxSpeedY * dirY
end
target = {
x = entity.x + entity.vx,
y = entity.y + entity.vy
}
-- collision detection
if dirX ~= 0 then
local bb = getBoundingBox(entity)
local mapX = math.floor(target.x) + dirX
bb.x1 = bb.x1 + entity.vx
bb.x2 = bb.x2 + entity.vx
local mapY1 = math.floor(bb.y1) - 1
local mapY2 = math.floor(bb.y2) + 1
local tiles = getMapRange(mapX, mapY1, mapX, mapY2)
local hasCollision = false
for i,tile in ipairs(tiles) do
local tileProps = TiledMap_GetTileProps(tile)
if tileProps and tileProps.type == "wall" then
local wallBB = {
x1 = mapX,
y1 = mapY1 + i - 1,
x2 = mapX + 1,
y2 = mapY1 + i
}
if checkRectCollision(bb, wallBB) then
hasCollision = true
break
end
end
end
if hasCollision then
--target.x = entity.x
if dirX > 0 then
target.x = math.floor(mapX) - 0.5
else
target.x = math.floor(mapX) + 1.5
end
entity.vx = 0
end
end
if dirY ~= 0 then
local bb = getBoundingBox(entity)
local mapY = math.floor(target.y + 0.5) + dirY
bb.y1 = bb.y1 + entity.vy
bb.y2 = bb.y2 + entity.vy
local mapX1 = math.floor(bb.x1) - 1
local mapX2 = math.floor(bb.x2) + 1
local tiles = getMapRange(mapX1, mapY, mapX2, mapY)
local hasCollision = false
for i,tile in ipairs(tiles) do
local tileProps = TiledMap_GetTileProps(tile)
if tileProps and (tileProps.type == "wall" or ( tileProps.type == "ladder" and entity.state.name ~= "climb" and dirY > 0 )) then
local wallBB = {
x1 = mapX1 + i - 1,
y1 = mapY,
x2 = mapX1 + i,
y2 = mapY + 1
}
if checkRectCollision(bb, wallBB) then
hasCollision = true
break
end
end
end
if hasCollision then
target.y = entity.y
--if dirY > 0 then
-- target.y = math.floor(mapY) - 0.5
-- else
-- target.y = math.floor(mapY) + 1.5
-- end
if math.abs(entity.vy) > 0 and math.abs(entity.y - entity.lastY) > 0.05 then
-- was falling
playSound(sounds.floor)
end
entity.vy = 0
if entity.state.name == "jump" and dirY > 0 then
entity.state:setState("stand")
end
end
end
entity.lastX = entity.x
entity.lastY = entity.y
entity.x = target.x
entity.y = target.y
end
function updatePlayer(player, dt)
updateEntity(player, dt)
if player.state.name ~= "dead" then
local bb = getBoundingBox(player)
-- check collision with monsters
for i, entity in ipairs(entities) do
if entity.type == "monster" then
local bb2 = getBoundingBox(entity)
if checkRectCollision(bb, bb2) then
playSound(sounds.dead)
player.state:setState("dead", 2, "stand")
game.state:setState("dead", 2, "restart")
break
end
end
end
end
end
function updateMonster(entity, dt)
local dirX = entity.dx
entity.vx = dirX * entity.maxSpeed
updateEntity(entity, dt)
-- couldn't move ?
if ( entity.vx == 0 ) then
-- change direction
entity.dx = -entity.dx
end
end
function love.update(dt)
if paused then
return
end
if musicDelay > 0 then
musicDelay = musicDelay - dt
-- HACK
musicChannel:setVolume(1 - musicDelay / 4)
if musicDelay <= 0 then
end
end
if game.state.name == "restart" then
start()
elseif game.state.name == "nextlevel" then
game.currentLevel = game.currentLevel + 1
start()
end
game.state:step(dt)
if dt < 1/60 then
love.timer.sleep((1/60 - dt))
end
local moveX = 0
local moveY = 0
local dirX = 0
local dirY = 0
if love.keyboard.isDown("right") then
moveX = 1
player.dx = 1
elseif love.keyboard.isDown("left") then
moveX = -1
player.dx = -1
end
if player.state.name ~= "dead" and player.state.name ~= "exit" then
local x = math.floor(player.x)
local y = math.floor(player.y + 0.5)
currentTile = TiledMap_GetMapTile(x, y, z)
currentTileProps = TiledMap_GetTileProps(currentTile) or {};
currentTileType = currentTileProps.type or currentTile
currentTileBelow = TiledMap_GetMapTile(x, y + 1, z)
currentTileBelowProps = TiledMap_GetTileProps(currentTileBelow) or {};
currentTileBelowType = currentTileBelowProps.type or currentTileBelow
debug.update(debug_sometext, x .. "," .. y .. " " .. currentTileType .. " (" .. currentTile .. ")")
if currentTileType == "key" then
playSound(sounds.collect)
TiledMap_SetMapTile(x, y, z, 0)
game.keys = game.keys - 1
if game.keys <= 0 then
playSound(sounds.doorOpen)
openExitDoors()
end
end
if player.state.name == "climb" and currentTileType ~= "ladder" and currentTileBelowType ~= "ladder" then
player.state:setState("stand")
end
if love.keyboard.isDown("up") or love.keyboard.isDown("down") then
--debug.update(debug_sometext, "step1")
if currentTileType == "ladder" or currentTileBelowType == "ladder" then
--debug.update(debug_sometext, "step2")
if love.keyboard.isDown("up") then
moveY = -1
else
moveY = 1
end
player.state:setState("climb")
-- else
--player.vy = 0
-- player.state = "stand"
elseif currentTileType == "exit" then
playSound(sounds.exit)
player.state:setState("exit", 1.5, "hidden")
game.state:setState("exit", 1.5, "nextlevel")
end
end
if player.state.name == "stand" and math.abs(player.vy) < 0.001 and
(love.keyboard.isDown(" ") or love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl")) then
player.state:setState("jump")
playSound(sounds.jump)
moveY = -1
player.vy = -0.15
end
else
moveX = 0
end
if moveX ~= 0 then
player.vx = player.vx + moveX * player.speed * dt
else
-- deccelerate
player.vx = player.vx * 0.9
end
if moveY ~= 0 then
player.vy = player.vy + moveY * player.speed * dt
else
-- deccelerate
if player.state.name == "climb" then
player.vy = player.vy * 0.9
end
end
if math.abs(player.vx) < 0.0001 then
player.vx = 0
end
if math.abs(player.vy) < 0.0001 then
player.vy = 0
end
for i, entity in ipairs(entities) do
if entity.type == "player" then
updatePlayer(entity, dt)
end
if entity.type == "monster" then
updateMonster(entity, dt)
end
end
if player.state.name ~= "dead" then
cam.x = player.x * FIELD_SIZE
cam.y = player.y * FIELD_SIZE
end
debug.update(fps, love.timer.getFPS())
tile = TiledMap_GetMapTile(math.floor(player.x), math.floor(player.y), z)
tileProps = TiledMap_GetTileProps(tile) or {};
tileType = tileProps.type or tile
debug.update(debug_currentTile, tileType)
debug.update(debug_player_x, player.x)
debug.update(debug_player_y, player.y)
debug.update(debug_player_vx, player.vx)
debug.update(debug_player_vy, player.vy)
debug.update(debug_player_state,player.state.name .. " (" .. player.state:getProgress() .. " )")
debug.update(debug_game_state,game.state.name .. " (" .. game.state:getProgress() .. " )")
debug.update(debug_keys,game.keys)
end
function love.keypressed(key, unicode)
if key == "s" then -- show/hide with "s"
debug.toggle()
end
debug.update(debug_keypressed, key)
if key == "escape" then
love.event.push("quit")
elseif key == "p" then
paused = not paused
if musicChannel ~= nil then
if paused then
musicChannel:pause()
else
if not musicPaused then
musicChannel:resume()
end
end
end
elseif key == "m" then
musicPaused = not musicPaused
if musicPaused then
musicChannel:pause()
else
musicChannel:resume()
end
end
end
function love.keyreleased( key, unicode )
end
function love.draw()
love.graphics.setColor(255, 255, 255, 255)
-- render map
TiledMap_DrawNearCam(cam.x,cam.y)
-- render player
local mapOffsetX = love.graphics.getWidth() / 2 - cam.x
local mapOffsetY = love.graphics.getHeight() / 2 - cam.y
for i, entity in ipairs(entities) do
local offsetX = mapOffsetX + entity.x * FIELD_SIZE
local offsetY = mapOffsetY + (entity.y + 0.5) * FIELD_SIZE
local alpha = 255
local sprite = nil
local angle = nil
local scale = nil
local shearX = nil
local shearY = nil
local originX = 12.5
local originY = 12.5
local stateName = entity.state.name
if entity.type == "player" then
if player.dx < 0 then
sprite = sprites.playerLeft
else
sprite = sprites.playerRight
end
if stateName == "start" then
alpha = math.floor(player.state:getProgress() * 255)
elseif stateName == "exit" then
sprite = sprites.playerUp
alpha = math.floor((1.0 - player.state:getProgress()) * 255)
elseif stateName == "climb" then
sprite = sprites.playerUp
elseif stateName == "dead" then
sprite = sprites.playerDead
angle = math.pi * 4.0 * entity.state:getProgress()
scale = 1.0 + math.sin(angle * 2.0) * 0.5
end
elseif entity.type == "monster" then
if entity.dx < 0 then
sprite = sprites.monsterLeft
else
sprite = sprites.monsterRight
end
shearX = math.sin(entity.x * 2.0) * 0.15
originY = 25
offsetY = offsetY + 12.5
end
if stateName ~= "hidden" then
love.graphics.setColor(255, 255, 255, alpha)
love.graphics.drawq(spritesImage, sprite, offsetX, offsetY, angle, scale, scale, originX, originY, shearX, shearY)
end
end
if debug.__debugMode then
love.graphics.setColor(255, 0, 0, 64)
local offsetX = math.floor(player.x) * FIELD_SIZE
local offsetY = math.floor(player.y + 0.5) * FIELD_SIZE
love.graphics.rectangle("line", mapOffsetX + offsetX, mapOffsetY + offsetY, FIELD_SIZE, FIELD_SIZE)
end
--love.graphics.rectangle("line", 0, 0, FIELD_SIZE, FIELD_SIZE)
-- debug
debug.draw()
end