-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
802 lines (683 loc) · 22.2 KB
/
main.lua
File metadata and controls
802 lines (683 loc) · 22.2 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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
require "levels"
require "dingoo_font"
require "util"
require "control_conf"
require "leveleditor"
game = {}
game.mode = 0 -- 0=start,1=game,2=pause
-- start screen data
game.startMode = {}
game.startMode.backGround = love.graphics.newImage("img/StartScreen.png")
game.startMode.timer = 0
game.startMode.show_start = true
-- game mode
game.level = 0
game.gameMode = {}
game.game_timer = 0
-- game mode
gameMode = {}
gameMode.pause = -1
gameMode.start = 0
gameMode.main = 1
gameMode.editor = 2
gameMode.score = 3
gameMode.settings = 4
gameMode.leveleditor = 5
-- ingame mode
mainGameMode = {}
mainGameMode.help = 1
mainGameMode.main = 2
mainGameMode.complete = 3
mainGameMode.try = 4
-- mode during game play
game.gameMode.main_game_mode = mainGameMode.help
-- help mode
game.gameMode.help_timer_max = 5
game.gameMode.help_timer = 0
-- level complete
game.gameMode.complete_timer_max = 5
game.gameMode.complete_timer = 0
-- reset mode
game.gameMode.reset_timer = 5
-- actual game
game.gameMode.normal_block = love.graphics.newImage("img/Box/Normal.png")
game.gameMode.normal_block_inv = love.graphics.newImage("img/Box/Normal_inv.png")
-- green randomly give a block
game.gameMode.green_block = love.graphics.newImage("img/Box/green.png")
game.gameMode.green_block_inv = love.graphics.newImage("img/Box/green_inv.png")
-- red randomly take a block
game.gameMode.red_block = love.graphics.newImage("img/Box/red.png")
game.gameMode.red_block_inv = love.graphics.newImage("img/Box/red_inv.png")
-- pause mode
game.pauseMode = {}
game.pauseMode.choice = 0 -- 0=pause, 1=help, 2=instr, 3=reset, 4=exit
-- settings mode
game.settingsMode = {}
game.settingsMode.choice = 0
-- player
player = {}
player.image = love.graphics.newImage("img/Player/normal.png");
player.x = 150
player.y = 200
player.speed = 40
player.orig_speed = 60
player.speed_inc = 50
player.give_blocks = {}
player.take_blocks = {}
player.give_shot = love.graphics.newImage("img/Player/shot_give.png")
player.take_shot = love.graphics.newImage("img/Player/shot_take.png")
--------------------
-- load functions --
--------------------
function love.load()
love.mouse.setVisible(false)
local success = love.graphics.setMode(320, 240, false, 0)
-- quit if not successful
if (not success) then
love.quit()
end
love.graphics.setCaption("Code Copy")
-- set fonts
font_data = love.image.newImageData("img/Fonts/font_complete.png")
font_normal = dingoo_font.setFontImage(font_data, " ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?[]", 12, 4)
font_data = love.image.newImageData("img/Fonts/font_white_small.png")
font_white_small = dingoo_font.setFontImage(font_data, " ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?[]", 6, 2)
font_data = love.image.newImageData("img/Fonts/font_red_small.png")
font_red_small = dingoo_font.setFontImage(font_data, " ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?[]", 6, 2)
font_data = love.image.newImageData("img/Fonts/font_complete_small.png")
font_normal_small = dingoo_font.setFontImage(font_data, " ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?[]", 6, 2)
end
----------------------
-- update functions --
----------------------
function love.update(dt)
if (game.mode == gameMode.pause) then
updatePause(dt)
elseif (game.mode == gameMode.start) then
updateStart(dt)
elseif (game.mode == gameMode.main) then
updateGame(dt)
elseif (game.mode == gameMode.settings) then
updateSettings(dt)
elseif (game.mode == gameMode.leveleditor) then
leveleditor.update(dt)
end
end
-- update start values
function updateStart(dt)
-- flash start button
game.startMode.timer = game.startMode.timer + dt
if (game.startMode.timer > 1) then
game.startMode.show_start = not game.startMode.show_start
game.startMode.timer = 0
end
end
-- update pause values
function updatePause(dt)
end
-- update the settings value
function updateSettings(dt)
end
-- update main game methods
function updateGame(dt)
if (game.gameMode.main_game_mode == mainGameMode.help) then
updateGameHelp(dt)
elseif (game.gameMode.main_game_mode == mainGameMode.main) then
updateGameMain(dt)
elseif (game.gameMode.main_game_mode == mainGameMode.complete) then
updateGameComplete(dt)
elseif (game.gameMode.main_game_mode == mainGameMode.try) then
updateRetryLevel(dt)
end
end
-- update the help screen at the start of a level
function updateGameHelp(dt)
game.gameMode.help_timer = game.gameMode.help_timer + dt
if (game.gameMode.help_timer > game.gameMode.help_timer_max) then
game.gameMode.main_game_mode = mainGameMode.main
end
end
-- update the main game mode
function updateGameMain(dt)
if (love.keyboard.isDown(control.left)) then
-- check limit
if (player.x <= 20) then
player.x = 20
else
-- move left
player.x = player.x - (player.speed*dt)
-- increase the speed
player.speed = player.speed + (dt*player.speed_inc)
end
elseif (love.keyboard.isDown(control.right)) then
-- check limit
if (player.x >= 280) then
player.x = 280
else
-- move right
player.x = player.x + (player.speed*dt)
-- increase the speed
player.speed = player.speed + (dt*player.speed_inc)
end
end
local remGiveShots = {}
local remTakeShots = {}
-- move the give shots
for i,v in ipairs(player.give_blocks) do
v.y = v.y - (dt*100)
-- check if off screen
if (v.y < 0) then
table.insert(remGiveShots, i)
end
end
-- move the take shots
for i,v in ipairs(player.take_blocks) do
v.y = v.y - (dt*100)
-- check if off screen
if (v.y < 0) then
table.insert(remTakeShots, i)
end
end
-- remove shots
for i,v in ipairs(remGiveShots) do
table.remove(player.give_blocks, v)
end
for i,v in ipairs(remTakeShots) do
table.remove(player.take_blocks, v)
end
-- update time
levels.levels[game.level].timer_max = levels.levels[game.level].timer_max - dt
-- out of time, show retry screen
local timer = math.floor(levels.levels[game.level].timer_max)
if (timer == -1) then
game.gameMode.main_game_mode = mainGameMode.try
end
end
-- update the complete screen at the mode
function updateGameComplete(dt)
game.gameMode.complete_timer = game.gameMode.complete_timer + dt
-- move to the next level
if (game.gameMode.complete_timer > game.gameMode.complete_timer_max) then
nextLevel()
end
end
-- update the retry level screen
function updateRetryLevel(dt)
game.gameMode.reset_timer = game.gameMode.reset_timer - dt
local timer = math.floor(game.gameMode.reset_timer)
if (timer == -1) then
retryLevel()
end
end
--------------------
-- draw functions --
--------------------
function love.draw()
if (game.mode == gameMode.pause) then
drawPause()
elseif (game.mode == gameMode.start) then
drawStart()
elseif (game.mode == gameMode.main) then
drawGame()
elseif (game.mode == gameMode.settings) then
drawSettings()
elseif (game.mode == gameMode.leveleditor) then
leveleditor.draw();
end
end
-- draw start screen
function drawStart()
love.graphics.clear()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(game.startMode.backGround, 0, 0, 0, 1, 1, 0)
if (game.startMode.show_start) then
dingoo_font.dingPrint(font_normal, 'PRESS START', 75, 178)
end
dingoo_font.dingPrint(font_normal_small, 'OR SELECT FOR SETTINGS', 75, 220)
end
-- draw pause screen
function drawPause()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.rectangle("fill", 90, 20, 140, 200)
dingoo_font.dingPrint(font_normal, 'PAUSE', 123, 35)
dingoo_font.dingPrint(font_normal, 'HELP', 123, 69)
dingoo_font.dingPrint(font_normal, 'INSTR', 123, 103)
dingoo_font.dingPrint(font_normal, 'RESET', 123, 137)
dingoo_font.dingPrint(font_normal, 'EXIT', 123, 171)
-- draw the select key
love.graphics.setColor(0, 0, 0, 255)
-- display updated choice
if (game.pauseMode.choice == 0) then
love.graphics.rectangle('fill', 103, 42, 10, 10)
elseif (game.pauseMode.choice == 1) then
love.graphics.rectangle('fill', 103, 76, 10, 10)
elseif (game.pauseMode.choice == 2) then
love.graphics.rectangle('fill', 103, 110, 10, 10)
elseif (game.pauseMode.choice == 3) then
love.graphics.rectangle('fill', 103, 144, 10, 10)
elseif (game.pauseMode.choice == 4) then
love.graphics.rectangle('fill', 103, 178, 10, 10)
end
end
-- draw settings screen
function drawSettings()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.rectangle("fill", 90, 20, 140, 200)
dingoo_font.dingPrint(font_normal, 'EDITOR', 123, 35)
dingoo_font.dingPrint(font_normal, 'SCORE', 123, 69)
dingoo_font.dingPrint(font_normal, 'INSTR', 123, 103)
dingoo_font.dingPrint(font_normal, 'RETURN', 123, 137)
dingoo_font.dingPrint(font_normal, 'EXIT', 123, 171)
-- draw the select key
love.graphics.setColor(0, 0, 0, 255)
if (game.settingsMode.choice == 0) then
love.graphics.rectangle('fill', 103, 42, 10, 10)
elseif (game.settingsMode.choice == 1) then
love.graphics.rectangle('fill', 103, 76, 10, 10)
elseif (game.settingsMode.choice == 2) then
love.graphics.rectangle('fill', 103, 110, 10, 10)
elseif (game.settingsMode.choice == 3) then
love.graphics.rectangle('fill', 103, 144, 10, 10)
elseif (game.settingsMode.choice == 4) then
love.graphics.rectangle('fill', 103, 178, 10, 10)
end
end
-- draw main game screen
function drawGame()
love.graphics.clear()
-- help mode
if (game.gameMode.main_game_mode == mainGameMode.help) then
drawGameHelp()
elseif (game.gameMode.main_game_mode == mainGameMode.main) then
drawGameMain()
elseif (game.gameMode.main_game_mode == mainGameMode.complete) then
drawGameComplete()
elseif (game.gameMode.main_game_mode == mainGameMode.try) then
drawRetryLevel()
end
end
-- draws the help game screen
function drawGameHelp()
love.graphics.clear()
-- display the blocks to remove
for i=0, 5 do
for j=1, 14 do
if (levels.levels[game.level].maze[i*14+j] == 1) then
love.graphics.draw(game.gameMode.normal_block, (j*20), (i*20)+20, 0, 1, 1, 0)
elseif (levels.levels[game.level].maze[i*14+j] == 2) then
love.graphics.draw(game.gameMode.green_block, (j*20), (i*20)+20, 0, 1, 1, 0)
elseif (levels.levels[game.level].maze[i*14+j] == 3) then
love.graphics.draw(game.gameMode.red_block, (j*20), (i*20)+20, 0, 1, 1, 0)
end
end
end
-- display help text
love.graphics.setColor(255, 255, 255, 255)
love.graphics.rectangle('fill', 0, 170, 320, 30)
dingoo_font.dingPrint(font_normal, levels.levels[game.level].level_name, 80, 173)
local time_diff = (game.gameMode.help_timer_max - math.floor(game.gameMode.help_timer))
dingoo_font.dingPrint(font_normal, tostring(time_diff), 260, 173)
end
-- draws the main game
function drawGameMain()
love.graphics.setColor(255, 255, 255, 255)
-- display game blocks
for i=0, 5 do
for j=1, 14 do
if (levels.levels[game.level].start[i*14+j] == 1) then
love.graphics.draw(game.gameMode.normal_block, (j*20), (i*20)+20, 0, 1, 1, 0)
elseif (levels.levels[game.level].start[i*14+j] == 2) then
love.graphics.draw(game.gameMode.green_block, (j*20), (i*20)+20, 0, 1, 1, 0)
elseif (levels.levels[game.level].start[i*14+j] == 3) then
love.graphics.draw(game.gameMode.red_block, (j*20), (i*20)+20, 0, 1, 1, 0)
end
end
end
love.graphics.draw(player.image, player.x, player.y, 0, 1, 1, 0)
--display shots (give)
for i,v in ipairs(player.give_blocks) do
love.graphics.draw(player.give_shot, v.x, v.y, 0, 1, 1, 0)
end
-- display shots (take)
for i,v in ipairs(player.take_blocks) do
love.graphics.draw(player.take_shot, v.x, v.y, 0, 1, 1, 0)
end
local timer = math.floor(levels.levels[game.level].timer_max)
-- if less than 10 seconds, show red
if (timer > 10) then
dingoo_font.dingPrint(font_white_small, tostring(levels.levels[game.level].timer_max), 280, 220)
else
dingoo_font.dingPrint(font_red_small, tostring(levels.levels[game.level].timer_max), 280, 220)
end
end
-- draw the level complete screen
function drawGameComplete()
local rounded_time = math.floor(game.gameMode.complete_timer)
local mod = rounded_time % 2
-- switch between inverted and normal maze colourss
if (mod == 0) then
for i=0, 5 do
for j=1, 14 do
if (levels.levels[game.level].start[i*14+j] == 1) then
love.graphics.draw(game.gameMode.normal_block, (j*20), (i*20)+20, 0, 1, 1, 0)
elseif (levels.levels[game.level].start[i*14+j] == 2) then
love.graphics.draw(game.gameMode.green_block, (j*20), (i*20)+20, 0, 1, 1, 0)
elseif (levels.levels[game.level].start[i*14+j] == 3) then
love.graphics.draw(game.gameMode.red_block, (j*20), (i*20)+20, 0, 1, 1, 0)
end
end
end
else
for i=0, 5 do
for j=1, 14 do
if (levels.levels[game.level].start[i*14+j] == 1) then
love.graphics.draw(game.gameMode.normal_block_inv, (j*20), (i*20)+20, 0, 1, 1, 0)
elseif (levels.levels[game.level].start[i*14+j] == 2) then
love.graphics.draw(game.gameMode.green_block_inv, (j*20), (i*20)+20, 0, 1, 1, 0)
elseif (levels.levels[game.level].start[i*14+j] == 3) then
love.graphics.draw(game.gameMode.red_block_inv, (j*20), (i*20)+20, 0, 1, 1, 0)
end
end
end
end
end
function drawRetryLevel()
-- display white block
love.graphics.setColor(255, 255, 255, 255)
love.graphics.rectangle('fill', 0, 170, 320, 30)
love.graphics.rectangle('fill', 0, 50, 320, 30)
dingoo_font.dingPrint(font_normal, "FAILED", 80, 53)
-- display time to reset
local timer = math.floor(game.gameMode.reset_timer)
dingoo_font.dingPrint(font_normal, "RETRY IN "..tostring(timer), 80, 173)
end
------------------------
-- keyboard functions --
------------------------
-- for when using computer
function love.keyreleased(key)
-- start game
if (game.mode == gameMode.start) then
-- start the game
startKeyReleased(key)
-- game mode
elseif (game.mode == gameMode.main) then
gameKeyReleased(key)
-- pause mode
elseif (game.mode == gameMode.pause) then
pauseKeyReleased(key)
-- settings mode
elseif (game.mode == gameMode.settings) then
settingsKeyReleased(key)
elseif (game.mode == gameMode.leveleditor) then
leveleditor.keyReleased(key)
end
end
-- process key releases during the start screen
function startKeyReleased(key)
if (key == control.start) then
game.mode = gameMode.main
elseif (key == control.select) then
game.mode = gameMode.settings
end
end
-- process key releases during the game screen
function gameKeyReleased(key)
if (key == control.left or key == control.right) then
-- reset the speed
player.speed = player.orig_speed
elseif (key == control.L) then
-- give a block
fireGiveBlock()
elseif (key == control.R) then
-- take a block
fireTakeBlock()
elseif (key == control.start) then
-- pause the game
game.mode = gameMode.pause
elseif (key == control.Y) then
-- activate the top give shot
if (table.getn(player.give_blocks) > 0) then
local bx = player.give_blocks[1].x
local by = player.give_blocks[1].y
local block_index = checkBlockCollision(bx, by)
if (block_index > 0) then
local block_type = levels.levels[game.level].start[block_index]
-- execute specific block actions
if (block_type == 2) then
activateGreenBlock(block_index)
elseif (block_type == 3) then
activateRedBlock(block_index)
end
-- replace block with normal block
levels.levels[game.level].start[block_index] = 1
end
table.remove(player.give_blocks, 1)
-- check if the current level is complete
if (levels.levelComplete(game.level)) then
game.gameMode.main_game_mode = mainGameMode.complete
end
end
if (table.getn(player.take_blocks) > 0) then
local bx = player.take_blocks[1].x
local by = player.take_blocks[1].y
local block_index = checkBlockCollision(bx, by)
if (block_index > 0) then
local block_type = levels.levels[game.level].start[block_index]
-- execute specific block actions
if (block_type == 2) then
activateGreenBlock(block_index)
elseif (block_type == 3) then
activateRedBlock(block_index)
end
levels.levels[game.level].start[block_index] = 0
end
table.remove(player.take_blocks, 1)
-- check if the current level is complete
if (levels.levelComplete(game.level)) then
game.gameMode.main_game_mode = mainGameMode.complete
end
end
elseif (key == control.A) then
-- activate the top take shot
end
end
-- process key releases during the pause screen
function pauseKeyReleased(key)
-- move the select menu
if (key == control.up) then
if (game.pauseMode.choice == 0) then
return;
else
game.pauseMode.choice = game.pauseMode.choice - 1
end
elseif (key == control.down) then
if (game.pauseMode.choice == 4) then
return;
else
game.pauseMode.choice = game.pauseMode.choice + 1
end
end
-- start, select menu option
if (key == control.start) then
if (game.pauseMode.choice == 0) then
-- return to the main game
game.mode = gameMode.main
elseif (game.pauseMode.choice == 1) then
help()
elseif (game.pauseMode.choice == 2) then
elseif (game.pauseMode.choice == 3) then
reset()
elseif (game.pauseMode.choice == 4) then
love.event.push('q')
end
end
-- B, return to main game
if (key == control.B) then
game.mode = 1
end
end
-- process key releases during the settings screen
function settingsKeyReleased(key)
if (key == control.up) then
if (game.settingsMode.choice == 0) then
return;
else
game.settingsMode.choice = game.settingsMode.choice - 1
end
elseif (key == control.down) then
if (game.settingsMode.choice == 4) then
return;
else
game.settingsMode.choice = game.settingsMode.choice + 1
end
end
if (key == control.start) then
if (game.settingsMode.choice == 0) then
game.mode = gameMode.leveleditor
end
end
end
-- dingoo controls
function love.joystickreleased(joystick, button)
if (game.mode == 0) then
if (button == control.start) then
game.mode = gameMode.main
end
end
end
-----------------
-- fire blocks --
-----------------
function fireGiveBlock()
local give = {}
give.x = player.x + (player.image:getWidth()/4)
give.y = player.y - 10
table.insert(player.give_blocks, give)
end
function fireTakeBlock()
local take = {}
take.x = player.x + (player.image:getWidth()/4)
take.y = player.y - 10
table.insert(player.take_blocks, take)
end
-- sets all of the blocks up/down/left/right of the index to 0
function activateGreenBlock(block_index)
-- up
if (levels.levels[game.level].start[block_index-14] ~= nil) then
levels.levels[game.level].start[block_index-14] = 0
end
-- down
if (levels.levels[game.level].start[block_index+14] ~= nil) then
levels.levels[game.level].start[block_index+14] = 0
end
-- left
if (levels.levels[game.level].start[block_index-1] ~= nil) then
levels.levels[game.level].start[block_index-1] = 0
end
-- right
if (levels.levels[game.level].start[block_index+1] ~= nil) then
levels.levels[game.level].start[block_index+1] = 0
end
end
-- sets all of the blocks up/down/left/right of the index to 1
function activateRedBlock(block_index)
-- up
if (levels.levels[game.level].start[block_index-14] ~= nil) then
levels.levels[game.level].start[block_index-14] = 1
end
-- down
if (levels.levels[game.level].start[block_index+14] ~= nil) then
levels.levels[game.level].start[block_index+14] = 1
end
-- left
if (levels.levels[game.level].start[block_index-1] ~= nil) then
levels.levels[game.level].start[block_index-1] = 1
end
-- right
if (levels.levels[game.level].start[block_index+1] ~= nil) then
levels.levels[game.level].start[block_index+1] = 1
end
end
--------------------
-- game functions --
--------------------
-- check collision with blocks
-- returns index of block if collision, 0 if no collision
function checkBlockCollision(bx, by)
bx = math.floor(bx)
by = math.floor(by)
-- get the x coords of the hit block
local modx = bx % 10
bx = bx - modx
bx = bx - 20
modx = bx % 20
bx = bx - modx
xr = bx / 20
xr = xr + 1
-- get the y coords of the hit block
local mody = by % 10
by = by - mody
mody = by % 20
by = by - mody
yr = by / 20
yr = yr + 1
-- get index
local pos_index = ((yr - 1) * 14) + xr
if (pos_index <= table.getn(levels.levels[game.level].start)) then
return pos_index
else
return 0
end
end
-- reset the game to start
function reset()
game.mode = gameMode.start
game.level = 0
game.startMode.timer = 0
game.startMode.show_start = true
game.gameMode.main_game_mode = mainGameMode.help
game.gameMode.help_timer = 0
-- reset all the levels
for i=0,100 do
if (levels.levels[i] ~= nil) then
levels.levels[i].start = util.copyTable(levels.levels[i].reset)
end
end
end
-- move onto the next level
function nextLevel()
game.level = game.level + 1
game.gameMode.main_game_mode = mainGameMode.help
game.gameMode.help_timer = 0
game.gameMode.complete_timer = 0
if (levels.levels[game.level] ~= nil) then
levels.levels[game.level].start = util.copyTable(levels.levels[game.level].reset)
else
reset()
end
end
-- retry the current level
function retryLevel()
game.gameMode.main_game_mode = mainGameMode.help
game.gameMode.help_timer = 0
game.gameMode.complete_timer = 0
levels.levels[game.level].start = util.copyTable(levels.levels[game.level].reset)
-- reset the level timer
levels.levels[game.level].timer_max = levels.levels[game.level].reset_timer
end
function help()
-- want to return to the main game
game.mode = gameMode.main
-- display the help screen for 5 seconds
game.gameMode.help_timer = 0
game.gameMode.main_game_mode = mainGameMode.help
levels.levels[game.level].timer_max = levels.levels[game.level].timer_max - 5
end
----------------------
-- useful functions --
----------------------
-- tidy up when quiting
function love.quit()
--os.exit(0)
end