-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreordered.p8
1586 lines (1343 loc) · 59.4 KB
/
reordered.p8
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pico-8 cartridge // http://www.pico-8.com
version 15
__lua__
-- title: reordered
-- version: 0.3
-- github page: https://github.com/hsandt/ggj2018-transmission
-- copyright: long nguyen huu
-- see license for license details
-- terminology
-- position: (x,y) float position
-- location: (i,j) integer position on the 8x6 map of 16x16 tiles
-- precise location: (k,l) integer position on the 16x12 map of 8x8 tiles
-- (precise, sub-) sprite: 8x8 sprite
-- big sprite: 16x16 sprite
-- representative sprite: 8x8 top-left sprite of the big sprite containing a 8x8 sprite
-- flags
-- flag 0: is static (set on all sub-sprites so we can draw layer with map, but logic only uses big sprite flag)
-- flag 1: is toggable (set on all sub-sprites in case we want to draw layer with map or check flag quickly without getting the representative sub-tile)
-- flag 2: should be check for collision (set on all relevant sub-sprites)
static_flag_id = 0
toggable_flag_id = 1
collision_flag_id = 2
-- sprite big id
restart_big_id = 1
exit_big_id = 2
emitter_big_id = 8
receiver_big_id = 9
forwarder_up_big_id = 4
mirror_horizontal_big_id = 16
-- a sprite just below another in the big spritesheet is its toggable variant (if applicable)
toggable_offset = 8
toggable_forwarder_up_big_id = forwarder_up_big_id+toggable_offset
toggable_mirror_horizontal_big_id = mirror_horizontal_big_id+toggable_offset
-- constants
fixed_fps = 30
fixed_delta_time = 1/fixed_fps
-- ui parameters
topbar_height = 16
title_y = 2
level_id_x = 90
level_id_y = 2
start_letters_start_x = 2
start_letters_start_y = 2
goal_letters_start_x = 2
goal_letters_start_y = 9
received_letters_start_x = 51
received_letters_start_y = 9
restart_icon_x = 7*16
exit_icon_x = 7*16
bottombar_height = 16
-- gameplay parameters
level_width = 8
level_height = 6
deadzone_margin = 0
forward_distance_threshold = 4
-- text
title = "-reordered-"
fail_message_wrong_letter_sequence = "letters are out-of-order!\ntry again!"
fail_message_too_short = "some letters are missing!\ntry again!"
fail_message_too_long = "there are too many letters!\ntry again!"
success_message = "you transmitted the word!\nlevel completed!"
finish_message = "last level cleared!\ncongratulations!"
-- gameplay parameters
moving_letter_initial_speed = 50.0 -- original 20.0
-- colors
black = 0
dark_blue = 1
dark_purple = 2
dark_green = 3
brown = 4
dark_gray = 5
light_gray = 6
white = 7
red = 8
orange = 9
yellow = 10
green = 11
blue = 12
indigo = 13
pink = 14
peach = 15
-- gameplay
untoggable_color = orange
toggable_color = green
gamespace_bgcolor = white
active_color = green
inactive_color = dark_gray
-- ui
topbar_bgcolor = black
title_color = white
level_id_color = white
start_letter_color = light_gray
goal_letter_color = orange
received_letter_color = red
remaining_letter_color = indigo
moving_letter_color = red
bottombar_bgcolor = black
bottom_message_color = white
-- basic structs
function make_vector(x,y)
return {x = x, y = y}
end
-- enums
directions = {
up = 1,
down = 2,
left = 3,
right = 4
}
directions_sequence = {
directions.up,
directions.down,
directions.left,
directions.right
}
directions_vector = {
make_vector(0.0, -1.0),
make_vector(0.0, 1.0),
make_vector(-1.0, 0.0),
make_vector(1.0, 0.0)
}
mirror_directions = {
horizontal = 1,
vertical = 2,
slash = 3,
antislash = 4
}
mirror_directions_sequence = {
mirror_directions.horizontal, -- like -
mirror_directions.vertical, -- like |
mirror_directions.slash, -- like / but at 45 degrees
mirror_directions.antislash -- like \ but at 45 degrees
}
-- mirror normals (can be not unit, in any sense)
mirror_directions_normal = {
make_vector(0.0, 1.0),
make_vector(1.0, 0.0),
make_vector(1.0, 1.0), -- convention y down!
make_vector(1.0, -1.0) -- convention y down!
}
effector_types = {
forwarder = "forwarder",
mirror = "mirror"
}
fail_causes = {
wrong_letter_sequence = 1,
too_short = 2,
too_long = 3
}
-- sound enums
sfx_ids = {
emit_letter = 0,
receive_letter = 1,
success = 2,
failure = 3,
finish = 4,
forward = 8,
mirror = 9,
toggle = 16
}
music_ids = {
level_preparation = 2, -- uses sfx 18
level_emission = 3 -- uses sfx 18, 20
}
-- data
levels_data = {
{
id = 1,
start_letters = {"e", "a", "r"},
goal_letters = {"e", "r", "a"},
map_index = 0,
bottom_message = "press ❎ to emit letters\nclick to toggle central arrow"
},
{
id = 2,
start_letters = {"a", "l", "e", "c"},
goal_letters = {"l", "a", "c", "e"},
map_index = 0,
bottom_message = "press ❎ to emit letters\ntry a longer word now!"
},
{
id = 3,
start_letters = {"g", "u", "m"},
goal_letters = {"m", "u", "g"},
map_index = 1,
bottom_message = "you can toggle other objects\nwith a small circle"
},
{
id = 4,
start_letters = {"l", "e", "a", "k"},
goal_letters = {"l", "a", "k", "e"},
map_index = 1,
bottom_message = "try a longer word now!"
},
}
-- level idea: keep word order the same, but one-time objects makes it hard!
-- data cache
current_level_data_cache = {
emitter_location = nil
}
-- state variables
-- input info
input_info = {
mouse_primary = {
down = false,
pressed = false,
released = false
},
mouse_secondary = {
down = false,
pressed = false,
released = false
}
}
-- the current coroutines
coroutines = {}
-- index of the curent level
current_level = 0
-- reference to the current level data in levels data
current_level_data = nil
-- game state (start, menu, ingame)
current_gamestate = "start"
-- sub state (while playing: preparation, emission, success, failure)
current_substate = nil
-- is a new level about to be loaded?
is_about_to_load = false
-- sequence of letters remaining to emit from the emitter
remaining_letters_to_emit = {}
-- sequence of letters already received by the receiver
received_letters = {}
-- letters already emitted but not yet received
moving_letters = {}
-- map of all dynamic tiles, by linear index (unwrapping big tile coordinates in 1d)
toggable_actors_linear_map = {}
-- ui: hint message at the bottom
bottom_message = ""
-- helpers
-- clear a table
function clear_table(t)
for k in pairs(t) do
t[k] = nil
end
end
-- yield as many times as needed so that, assuming you coresume the calling
-- coroutine each frame at 30 fps, the coroutine resumes after [time] seconds
function yield_delay(time)
local nb_frames = fixed_fps*time
for frame=1,nb_frames do -- still works if nb_frames is not integer (~flr(nb_frames))
yield()
end
end
-- create and register coroutine
function add_coroutine(async_function)
coroutine = cocreate(async_function)
add(coroutines, coroutine)
end
-- conversion helpers
-- convert 16x16 spritesheet coordinates to 8x8 sprite id
-- [non-surjective]
function indices_to_spritesheet_sprite_id(i, j)
return 32*j+2*i
end
-- convert 8x8 sprite id to 16x16 spritesheet coordinates
-- [non-injective]
function sprite_id_to_spritesheet_indices(sprite_id)
-- %16 instead of %32 allows support for other sub-sprite than the top-left (representative)
return {i = flr((sprite_id%16)/2), j = flr(sprite_id/32)}
end
-- convert 8x8 sprite id to 16x16 fictive sprite id (with pages of 8 big sprites per line)
-- [non-injective]
function sprite_id_to_big_sprite_id(sprite_id)
indices = sprite_id_to_spritesheet_indices(sprite_id)
return 8*indices.j+indices.i
end
-- convert 16x16 fictive sprite id to 8x8 sprite id (with pages of 8 big sprites per line)
-- [non-surjective]
function big_sprite_id_to_sprite_id(big_sprite_id)
return 32*flr(big_sprite_id/8)+2*(big_sprite_id%8)
end
-- return the (x,y) position corresponding to an (i,j) location for 16x16 tiles
-- [non-surjective]
function location_to_position(location)
return make_vector(16*location.i,16*location.j)
end
-- return the (i,j) location corresponding to an (x,y) position for 16x16 tiles
-- [non-injective]
function position_to_location(position)
return {i = flr(position.x/16), j = flr(position.y/16)}
end
-- return the (k,l) precise location corresponding to an (x,y) position for 8x8 tiles
-- [non-injective]
function position_to_precise_location(position)
return {k = flr(position.x/8), l = flr(position.y/8)}
end
-- return the (i,j) location corresponding to a (k,l) precise location for 8x8->16x16 tiles
-- [non-injective]
function precise_location_to_location(precise_location)
return {i = flr(precise_location.k/2), j = flr(precise_location.l/2)}
end
-- return the (k,l) previse location of the top-left sub-tile of the big tile containing a given sub-tile
-- [non-injective]
function to_representative_location(precise_location)
local location = precise_location_to_location(precise_location)
return {k = 2*location.i, l = 2*location.j}
end
-- convert map (i,j) location to a linear index, starting at 0, incremented by moving to
-- the right then down to the next line, etc.
-- [bijective]
function location_to_map_linear_index(location)
return level_width*location.j+location.i
end
-- convert map linear index to (i,j) location
-- [bijective]
function map_linear_index_to_location(linear_index)
return {i = linear_index%level_width, j = flr(linear_index/level_width)}
end
-- convert screen position to gamespace location
function screen_position_to_gamespace_location(screen_position)
-- subtract top bar height
local gamespace_position = make_vector(screen_position.x, screen_position.y-topbar_height)
return position_to_location(gamespace_position)
end
-- string helpers
function join(sequence, separator)
local final_string = ""
local i = 0 -- increment at beginning of loop, so start at 1
for v in all(sequence) do -- use all to guarantee order
i += 1
final_string = final_string..tostr(v)
if separator and i < #sequence then
final_string = final_string..separator
end
end
return final_string
end
-- input helpers
mouse_devkit_address = 0x5f2d
function toggle_mouse(active)
value = active and 1 or 0
poke(mouse_devkit_address, value)
end
function get_mouse_screen_position()
assert(peek(mouse_devkit_address) == 1)
return make_vector(stat(32), stat(33))
end
function get_mouse_button_bitmask()
assert(peek(mouse_devkit_address) == 1)
return stat(34)
end
-- return true is primary button is currently down
function get_mouse_primary()
return band(get_mouse_button_bitmask(), 0x1) == 0x1
end
-- return true is secondary button is currently down
function get_mouse_secondary()
return band(get_mouse_button_bitmask(), 0x2) == 0x2
end
-- return true if button is down according to system
-- for usage in process_input
function query_down(button_name)
if button_name == "mouse_primary" then
return get_mouse_primary()
elseif button_name == "mouse_secondary" then
return get_mouse_secondary()
else
printh("error: unknown button name: "..button_name)
end
end
-- return true if button is down according to the input manager
-- for usage after process_input
function is_down(button_name)
return input_info[button_name].down
end
-- return true if button has just been pressed according to the input manager
-- for usage after process_input
function is_pressed(button_name)
return input_info[button_name].pressed
end
-- return true if button has just been released according to the input manager
-- for usage after process_input
function is_released(button_name)
return input_info[button_name].released
end
-- math helpers
function location_to_string(location)
return "("..location.i..","..location.j..")"
end
v8 = make_vector(8,8)
v16 = make_vector(16, 16)
function vector_copy(v, w)
v.x = w.x
v.y = w.y
end
function vector_to_string(v)
return "("..v.x..","..v.y..")"
end
function vector_length(v)
return sqrt(v.x^2+v.y^2)
end
function vector_sqrlength(v)
return v.x^2+v.y^2
end
function vector_oppose(v)
return make_vector(-v.x,-v.y)
end
function rotatecw90(v)
return make_vector(v.y,-v.x)
end
function vector_add(v, w)
return make_vector(v.x+w.x,v.y+w.y)
end
function vector_sub(v, w)
return make_vector(v.x-w.x,v.y-w.y)
end
function vector_mult(scalar, v)
return make_vector(scalar*v.x,scalar*v.y)
end
function normalized(v)
return vector_mult(1/vector_length(v),v)
end
function manhattan_distance(v, w)
return abs(v.x-w.x)+abs(v.y-w.y)
end
function distance(v, w)
return vector_length(vector_sub(v,w))
end
function dot(v, w)
return v.x*w.x+v.y*w.y
end
function mixed_prod(v, w)
return v.x*w.y-v.y*w.x
end
function project_parallel(v, w)
local projected_abs = dot(v,w)/vector_sqrlength(w)
return make_vector(projected_abs*w.x,projected_abs*w.y)
end
function project_ortho(v, w)
local projected_parallel = project_parallel(v,w)
return vector_sub(v,projected_parallel)
end
function reflect(v,w)
local projected_parallel = project_parallel(v,w)
local projected_ortho = vector_sub(v,projected_parallel)
return vector_sub(projected_parallel,projected_ortho)
end
-- draw helpers
-- draw "big" sprite 16x16 of big sprite id at coords (x,y), optionally flipped in x/y
function bigspr(big_sprite_id, x, y, flip_x, flip_y)
spr(big_sprite_id_to_sprite_id(big_sprite_id), x, y, 2, 2, flip_x, flip_y)
end
-- draw "big" sprite 16x16 of big sprite id at location (i,j), optionally flipped in x/y
function bigtile(big_sprite_id, location, active, flip_x, flip_y)
-- default (nil is not false!!)
if active == nil then
active = true
end
local sprite_id = big_sprite_id_to_sprite_id(big_sprite_id)
local position = location_to_position(location)
if not active then
-- inactive objects switch palette to inactive
pal(active_color, inactive_color)
end
spr(sprite_id, position.x, position.y, 2, 2, flip_x, flip_y)
if not active then
pal()
end
end
-- factory
function make_toggable(big_sprite_id)
local effector_type = nil
local direction = nil
if big_sprite_id >= toggable_forwarder_up_big_id and big_sprite_id < toggable_forwarder_up_big_id+4 then
effector_type = effector_types.forwarder
direction = directions_sequence[big_sprite_id-(toggable_forwarder_up_big_id)+1] -- sequence starts at 1
elseif big_sprite_id >= mirror_horizontal_big_id+toggable_offset and big_sprite_id < mirror_horizontal_big_id+toggable_offset+4 then
effector_type = effector_types.mirror
direction = mirror_directions_sequence[big_sprite_id-(toggable_mirror_horizontal_big_id)+1] -- sequence starts at 1
end
if not effector_type then
printh("error: unknown effector type")
return nil
end
local toggable_actor = {
big_sprite_id = big_sprite_id,
effector_type = effector_type,
direction = direction,
active = true
}
return toggable_actor
end
function make_moving_letter(letter, x, y, vx, vy)
local moving_letter = {
letter = letter,
position = {
x = x,
y = y
},
last_position = {
x = x,
y = y
},
velocity = {
x = vx,
y = vy
},
active = true
}
return moving_letter
end
-- game loop
function _init()
run_unit_tests()
-- activate mouse devkit (for mouse input support)
toggle_mouse(true)
load_level(1)
end
function _update()
process_input()
handle_input()
if current_gamestate == "ingame" and current_substate == "emission" then
update_moving_letters()
end
update_coroutines()
end
function _draw()
if current_gamestate == "ingame" then
draw_gamespace()
draw_topbar()
draw_bottombar()
end
draw_cursor()
end
-- input
function process_input()
for button_name, button_state in pairs(input_info) do
-- release any previous pressed/release
button_state.pressed = false
button_state.released = false
-- check previous mouse button state to decide if button has just been pressed
-- also update the mouse button current state
if not button_state.down and query_down(button_name) then
button_state.down = true
button_state.pressed = true
elseif button_state.down and not query_down(button_name) then
button_state.down = false
button_state.released = true
end
end
end
function handle_input()
if current_gamestate == "ingame" then
if current_substate == "preparation" then
-- start emission
if btnp(❎) then
start_emit_letters()
end
end
-- switch to previous/next level
if btnp(⬅️) then
load_previous_level_cycle()
end
if btnp(➡️) then
load_next_level_cycle()
end
-- mouse click
if is_pressed("mouse_primary") then
local click_position = get_mouse_screen_position()
if click_position.y < topbar_height then
-- restart?
if click_position.x >= restart_icon_x and click_position.x < restart_icon_x+16 then
reload_current_level_immediate()
elseif click_position.x >= exit_icon_x then
printh("exit (not implemented)")
end
elseif click_position.y >= topbar_height+16*level_height then
-- bottombar
printh("click on bottombar (does nothing)")
else
-- toggle?
local location = screen_position_to_gamespace_location(click_position)
local toggable_actor = get_toggable_actor_at_location(location)
if toggable_actor then
toggle(toggable_actor)
end
end -- click in different areas
end -- primary
end -- playing
end
-- game flow
-- setup level state by level index
function setup_current_level()
printh("setup_current_level: "..current_level)
current_level_data = levels_data[current_level]
assert(current_level_data)
-- find emitter and toggable elements (will set emitter_location and toggable_actors_linear_map)
register_dynamic_tiles(current_level_data.map_index)
if not current_level_data_cache.emitter_location then
printh("error: emitter could not be found on this map")
end
-- setup game state
current_gamestate = "ingame"
current_substate = "preparation"
is_about_to_load = false
-- copy start letters sequence
clear_table(remaining_letters_to_emit)
for letter in all(current_level_data.start_letters) do
add(remaining_letters_to_emit, letter)
end
-- no received nor moving letters at the beginning
clear_table(received_letters)
clear_table(moving_letters)
-- stop and clear the emit coroutine if needed (coroutines must be resumed to continue;
-- if they aren't and references to them disappear, they will be gc-ed, effectively being stopped)
clear_table(coroutines)
-- ui
bottom_message = current_level_data.bottom_message
-- music
music(music_ids.level_preparation)
end
function load_level(level_index)
current_level = level_index
setup_current_level()
end
function load_previous_level_cycle()
load_level(get_level_index_cycle(current_level-1))
end
function load_next_level_cycle()
load_level(get_level_index_cycle(current_level+1))
end
function get_level_index_cycle(level_index)
-- modulo op must be applied to index starting at 0, we use that as intermediary value
local level_index0 = level_index - 1
local level_index0_cycle = level_index0 % #levels_data
return level_index0_cycle + 1
end
function fail_current_level(fail_cause)
current_substate = "failure"
if fail_cause == fail_causes.wrong_letter_sequence then
bottom_message = fail_message_wrong_letter_sequence
elseif fail_cause == fail_causes.too_short then
bottom_message = fail_message_too_short
elseif fail_cause == fail_causes.too_long then
bottom_message = fail_message_too_long
else
bottom_message = "failed for unknown reason"
end
if not is_about_to_load then
is_about_to_load = true
add_coroutine(confirm_fail_async)
end
end
function succeed_current_level()
current_substate = "success"
bottom_message = success_message
if not is_about_to_load then
is_about_to_load = true
if current_level == #levels_data then
-- last level succeeded!
add_coroutine(finish_game_async)
else
add_coroutine(confirm_success_async)
end
end
end
function confirm_fail_async()
yield_delay(1.0)
music(-1)
sfx(sfx_ids.failure)
add_coroutine(reload_current_level_async)
end
function confirm_success_async()
yield_delay(1.0)
music(-1)
sfx(sfx_ids.success)
add_coroutine(load_next_level_async)
end
function reload_current_level_immediate()
-- don't care if we were about to load, let the player switch level immediately,
-- coroutines (and is_about_to_load) will be cleared on setup_current_level anyway
setup_current_level()
end
function reload_current_level_async()
yield_delay(1.0)
setup_current_level()
end
function load_next_level_async()
assert(current_level < #levels_data)
yield_delay(1.0)
load_level(current_level + 1)
end
function finish_game_async()
yield_delay(1.0)
bottom_message = finish_message
music(-1)
sfx(sfx_ids.finish)
yield_delay(2.5) -- wait more to let finish jingle end
load_level(1)
end
-- map
-- return the location of dynamic tiles (also the emitter for letter display)
function register_dynamic_tiles(map_index)
-- reset any previous data to make sure they don't stack when loading a new level
current_level_data_cache.emitter_location = nil
clear_table(toggable_actors_linear_map)
local celx, cely = get_map_topleft(map_index)
for i=0,level_width-1 do
for j=0,level_height-1 do
local sprite_id = mget(celx+2*i,cely+2*j) -- map uses precise location, hence double
local big_sprite_id = sprite_id_to_big_sprite_id(sprite_id)
if big_sprite_id == emitter_big_id then
current_level_data_cache.emitter_location = {i = i, j = j}
elseif is_toggable(big_sprite_id) then
local linear_index = location_to_map_linear_index({i = i, j = j})
local toggable_actor = make_toggable(big_sprite_id)
if toggable_actor then
toggable_actors_linear_map[linear_index] = toggable_actor
printh("added toggable "..toggable_actor.effector_type.." at "..linear_index)
else
printh("->error: could not make toggable actor")
end
end
end
end
end
function is_toggable(big_sprite_id)
local sprite_id = big_sprite_id_to_sprite_id(big_sprite_id)
return fget(sprite_id, toggable_flag_id)
end
function get_toggable_actor_at_location(location)
return toggable_actors_linear_map[location_to_map_linear_index(location)]
end
-- gameplay
-- start and register coroutine to emit letters at regular intervals from now on
function start_emit_letters()
add_coroutine(emit_letters_async)
current_substate = "emission"
music(3)
end
function emit_letters_async()
while true do
emit_next_letter()
if #remaining_letters_to_emit == 0 then
return
end
yield_delay(1.0)
end
end
-- emit the next letter from the emitter
function emit_next_letter()
-- get next letter to emit
local next_letter = remaining_letters_to_emit[1]
-- create and emit letter with default velocity
local emit_position = location_to_position(current_level_data_cache.emitter_location)
local moving_letter = make_moving_letter(next_letter,emit_position.x+8,emit_position.y+10,0,-moving_letter_initial_speed)
-- remove letter from sequence of remaining letters
del(remaining_letters_to_emit,next_letter)
-- add letter to sequence of moving letters
add(moving_letters, moving_letter)
-- sfx
sfx(sfx_ids.emit_letter)
end
-- update emit coroutine if active, remove if dead
function update_coroutines()
for coroutine in all(coroutines) do
local status = costatus(coroutine)
if status == "suspended" then
assert(coresume(coroutine))
elseif status == "dead" then
coroutine = nil
else -- status == "running"
printh("warning: coroutine should not be running outside its body")
end
end
end
-- toggle a toggable actor
function toggle(toggable_actor)
printh("toggle actor: "..toggable_actor.effector_type)
toggable_actor.active = not toggable_actor.active
sfx(sfx_ids.toggle)
end
-- physics
function update_moving_letters()
for moving_letter in all(moving_letters) do
vector_copy(moving_letter.last_position,moving_letter.position)
update_position(moving_letter)
check_out_of_bounds(moving_letter)
-- the previous check may have killed the moving letter
if moving_letter.active then
check_collision(moving_letter)
end
end
end
-- apply current velocity to moving letter
function update_position(moving_letter)
moving_letter.position.x += moving_letter.velocity.x*fixed_delta_time
moving_letter.position.y += moving_letter.velocity.y*fixed_delta_time
end
-- check if moving letter entered a tile with a special effect, and apply it if so
function check_collision(moving_letter)
local celx, cely = get_map_topleft(current_level_data.map_index)
-- since some object colliders occupy precise tiles (e.g. the mirror), we need precise location
-- to check for collision flag
local precise_location = position_to_precise_location(moving_letter.position)
local precise_sprite_id = mget(celx+precise_location.k,cely+precise_location.l)
if precise_sprite_id != 0 then
local collision_flag = fget(precise_sprite_id,collision_flag_id)
if collision_flag then
-- for colliding sprite id, we use the location of the big tile since it's easier to compare
-- with just the top-left tile id than with all 4 sub-tile ids (it's the same 1 time out of 4)
local big_sprite_id = sprite_id_to_big_sprite_id(precise_sprite_id)
local toggable_flag = fget(precise_sprite_id,toggable_flag_id) -- caution: this requires to set toggable flag on all sub-tiles
local location = precise_location_to_location(precise_location)
if big_sprite_id == receiver_big_id then
receive(moving_letter)
else
local out_info = {}