-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.wat
More file actions
1828 lines (1582 loc) · 69 KB
/
Copy pathsnake.wat
File metadata and controls
1828 lines (1582 loc) · 69 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
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
(module
;; Imports
;; ----------------------------------------------------------------------
(func $extern_log (import "imports" "logFromNBytesOfMemory") (param i32))
;; Memory
;; ----------------------------------------------------------------------
(memory $memory (export "memory") 1 64)
(global $memory_region_log_bytes_offset i32 (i32.const 0))
(global $memory_region_log_bytes_n i32 (i32.const 2048))
(global $memory_region_snake_buf_offset i32 (i32.const 2048))
(global $memory_region_snake_buf_bytes_n i32 (i32.const 63488))
(global $memory_region_canvas_bytes_offset i32 (i32.const 65536))
(global $memory_region_canvas_bytes_n (mut i32) (i32.const 0))
(global $memory_current_pages_n (mut i32) (i32.const 1))
(export "memoryRegionLogBytesOffset" (global $memory_region_log_bytes_offset ))
(export "memoryRegionLogBytesN" (global $memory_region_log_bytes_n ))
(export "memoryRegionCanvasBytesOffset" (global $memory_region_canvas_bytes_offset))
(export "memoryRegionCanvasBytesN" (global $memory_region_canvas_bytes_n ))
;; Game Logic
;; ----------------------------------------------------------------------
(global $INPUT_START (export "INPUT_START" ) i32 (i32.const 0))
(global $INPUT_UPDATE (export "INPUT_UPDATE") i32 (i32.const 1))
(global $INPUT_END (export "INPUT_END" ) i32 (i32.const 2))
;; We export the character codes (integers) that can be used as inputs to
;; change the snake's heading (direction). Letter keys are assigned to their
;; ASCII character codes, making it easier to implement platform specific
;; bindings based on keyboard input.
;; Arrow keys are given codes that lie in the Latin1 0x80-0x9F control codes,
;; as they are unlikely to occur during keyboard input. Additionally,
;; determining the heading is arithmetically easy because the correspoding
;; groups containing a lower case letter, uppercase letter and arrow key
;; share the same lower 5-bits.
(global $KEY_SPACE (export "KEY_SPACE" ) i32 (i32.const 32))
(global $KEY_W (export "KEY_W" ) i32 (i32.const 0x57)) ;;---+ 0b xxx1 0111
(global $KEY_w (export "KEY_w" ) i32 (i32.const 0x77)) ;;--/
(global $ARROW_UP (export "ARROW_UP" ) i32 (i32.const 0x97)) ;;-/
(global $KEY_A (export "KEY_A" ) i32 (i32.const 0x41)) ;;---+ 0b xxx0 0001
(global $KEY_a (export "KEY_a" ) i32 (i32.const 0x61)) ;;--/
(global $ARROW_LEFT (export "ARROW_LEFT" ) i32 (i32.const 0x81)) ;;-/
(global $KEY_S (export "KEY_S" ) i32 (i32.const 0x53)) ;;---+ 0b xxx1 0011
(global $KEY_s (export "KEY_s" ) i32 (i32.const 0x73)) ;;--/
(global $ARROW_DOWN (export "ARROW_DOWN" ) i32 (i32.const 0x93)) ;;-/
(global $KEY_D (export "KEY_D" ) i32 (i32.const 0x44)) ;;---+ 0b xxx0 0100
(global $KEY_d (export "KEY_d" ) i32 (i32.const 0x64)) ;;--/
(global $ARROW_RIGHT (export "ARROW_RIGHT") i32 (i32.const 0x84)) ;;-/
(global $KEY_IS_DOWN_RIGHT i32 (i32.const 1))
(global $KEY_IS_DOWN_LEFT i32 (i32.const 2))
(global $KEY_IS_DOWN_UP i32 (i32.const 4))
(global $KEY_IS_DOWN_DOWN i32 (i32.const 8))
(global $KEYS_ARE_DOWN_LEFT_RIGHT_MASK i32 (i32.const 3))
(global $KEYS_ARE_DOWN_UP_DOWN_MASK i32 (i32.const 12))
(global $SNAKE_MOVEMENT_PX_PER_S f32 (f32.const 300))
(global $SNAKE_RADIUS f32 (f32.const 12))
(global $SNAKE_LERP_INC f32 (f32.const 1.0))
(global $ORB_RADIUS f32 (f32.const 8))
(global $GAME_STATE_FIRST_UPDATE i32 (i32.const 0))
(global $GAME_STATE_START_SCREEN i32 (i32.const 1))
(global $GAME_STATE_PLAYING i32 (i32.const 2))
(global $GAME_STATE_END_SCREEN i32 (i32.const 3))
;; Read right to left because little edian :(
(global $SNAKE_HEAD_COLOR i32 (i32.const 0xFF11DD11))
(global $SNAKE_TAIL_COLOR i32 (i32.const 0xFF117711))
(global $ORB_COLOR i32 (i32.const 0xFFFF00EE))
(global $ORB_HIGHLIGHT_COLOR i32 (i32.const 0xFFFFFFFF))
(global $TEXT_COLOR i32 (i32.const 0xFFFFAAFF))
(global $BACKGROUND_COLOR i32 (i32.const 0xFF000000))
(global $INVERSE_SQRT_2 f32 (f32.const 0.7071067811865475))
;; Unintialized values (-1) are initialized in `$reset_game` and `$resize_canvas`
(global $canvas_width (mut i32) (i32.const 0))
(global $canvas_height (mut i32) (i32.const 0))
(global $canvas_widthf (mut f32) (f32.const -1))
(global $canvas_heightf (mut f32) (f32.const -1))
(global $game_state (mut i32) (i32.const 0))
(global $timestamp_update_call (mut f32) (f32.const -1))
(global $timestamp_last_update_call (mut f32) (f32.const -1))
(global $keys_are_down_mask (mut i32) (i32.const 0))
(global $snake_unit_heading_x (mut f32) (f32.const 1))
(global $snake_unit_heading_y (mut f32) (f32.const 0))
(global $snake_length (mut f32) (f32.const 0))
(global $snake_target_length (mut f32) (f32.const -1))
(global $score_100s_digit (mut i32) (i32.const 0))
(global $score_10s_digit (mut i32) (i32.const 0))
(global $score_1s_digit (mut i32) (i32.const 0))
(global $orb_x_as_canvas_width_frac (mut f32) (f32.const -1))
(global $orb_y_as_canvas_height_frac (mut f32) (f32.const -1))
(func (export "update") (param $timestamp f32)
(if (i32.eq (global.get $game_state) (global.get $GAME_STATE_FIRST_UPDATE)) (then
call $reset_game
(global.set $game_state (global.get $GAME_STATE_START_SCREEN))
return
))
(global.set $timestamp_last_update_call (global.get $timestamp_update_call))
(global.set $timestamp_update_call (local.get $timestamp))
(if (i32.eq (global.get $game_state) (global.get $GAME_STATE_START_SCREEN)) (then
(call $draw_start_screen_text)
return
))
(if (i32.eq (global.get $game_state) (global.get $GAME_STATE_PLAYING)) (then
call $snake_update
(if (call $snake_check_circle_collides_with_head
(call $orb_calc_x) (call $orb_calc_y)
(global.get $ORB_RADIUS)
) (then
call $score_inc
call $orb_update
call $snake_target_length_inc
;; We update the snake again so it's drawn over the old orb
call $snake_update
))
call $score_draw
(if (call $snake_check_collision) (then
(global.set $game_state (global.get $GAME_STATE_END_SCREEN))
))
return
))
(if (i32.eq (global.get $game_state) (global.get $GAME_STATE_END_SCREEN)) (then
call $draw_end_screen_text
return
))
)
(func (export "input_key") (param $action i32) (param $key i32)
(local $latin1_32_byte_range_idx i32)
(local $arrow_key i32)
(local $key_down_mask i32)
;; Special handling of key presses while in the start and end screen
(if (i32.or
(i32.eq
(global.get $GAME_STATE_START_SCREEN)
(global.get $game_state))
(i32.and
(i32.eq
(global.get $GAME_STATE_END_SCREEN)
(global.get $game_state))
(i32.eq (local.get $key) (global.get $KEY_SPACE)))
) (then
(call $reset_game)
(global.set $game_state (global.get $GAME_STATE_PLAYING))
call $orb_update
))
;; == Handle keys during play ==
;; Ignore keys that are not the 32-byte ranges with index
;; 2 (0x40-0x5F) for upper case letters
;; 3 (0x60-0x6F) for lower case letters
;; 4 (0x80-0x9F) for latin1 control codes -- we're using the for arrow keys
(local.set $latin1_32_byte_range_idx
(i32.shr_u (local.get $key) (i32.const 5)))
(if (i32.lt_u (local.get $latin1_32_byte_range_idx) (i32.const 2)) (then
return
))
(if (i32.gt_u (local.get $latin1_32_byte_range_idx) (i32.const 4)) (then
return
))
;; Calculate the arrow key code by masking the lower 5 bits of the
;; key code and setting the upper bits to those of the latin1
;; control codes (0x80).
(local.set $arrow_key
(i32.or
(i32.and (local.get $key) (i32.const 0x1F))
(i32.const 0x80)))
;; Determine the bit mask that signifies that a key is pressed down
(if (i32.eq (local.get $arrow_key) (global.get $ARROW_UP)) (then
(local.set $key_down_mask (global.get $KEY_IS_DOWN_UP))
))
(if (i32.eq (local.get $arrow_key) (global.get $ARROW_LEFT)) (then
(local.set $key_down_mask (global.get $KEY_IS_DOWN_LEFT))
))
(if (i32.eq (local.get $arrow_key) (global.get $ARROW_DOWN)) (then
(local.set $key_down_mask (global.get $KEY_IS_DOWN_DOWN))
))
(if (i32.eq (local.get $arrow_key) (global.get $ARROW_RIGHT)) (then
(local.set $key_down_mask (global.get $KEY_IS_DOWN_RIGHT))
))
;; Update the `$keys_are_down_mask` based on the bit mask for the key
(if (i32.eq (local.get $action) (global.get $INPUT_UPDATE)) (then
;; If the is a key press action the we can just directly
;; set the aggregate bit mask to that of the individual key
(global.set $keys_are_down_mask (local.get $key_down_mask))
))
(if (i32.eq (local.get $action) (global.get $INPUT_START)) (then
;; If the is a key down action then we
;; a) set the "left" and "right" bits to 0 if left or right are down
;; OR the "up" and "down" bits to 0 if up or down are down,
;; to make sure that the opposite key's bit is set to 0,
;; meaning that left will be registered even if right is down, etc.
(if (i32.and
(global.get $KEYS_ARE_DOWN_LEFT_RIGHT_MASK)
(local.get $key_down_mask)
)(then
(global.set $keys_are_down_mask
(i32.and
(global.get $keys_are_down_mask)
(global.get $KEYS_ARE_DOWN_UP_DOWN_MASK)))
))
(if (i32.and
(global.get $KEYS_ARE_DOWN_UP_DOWN_MASK)
(local.get $key_down_mask)
) (then
(global.set $keys_are_down_mask
(i32.and
(global.get $keys_are_down_mask)
(global.get $KEYS_ARE_DOWN_LEFT_RIGHT_MASK)))
))
;; a) add the bit mask of the new key to the aggregate bit mask
(global.set $keys_are_down_mask
(i32.or
(global.get $keys_are_down_mask)
(local.get $key_down_mask)))
))
(if (i32.eq (local.get $action) (global.get $INPUT_END)) (then
;; If the is a key up action then we set unset the key's
;; bit in the aggregate bit mask
(global.set $keys_are_down_mask
(i32.and
(global.get $keys_are_down_mask)
(i32.xor
(i32.const 0xF)
(local.get $key_down_mask))))
))
;; Update the coordinates of the snake's heading unit vector, based on the
;; aggregate bit mask
(if (i32.and
(global.get $keys_are_down_mask)
(global.get $KEY_IS_DOWN_UP)
) (then
(if (i32.and
(global.get $KEY_IS_DOWN_LEFT)
(global.get $keys_are_down_mask)
) (then
(global.set $snake_unit_heading_x
(f32.mul (f32.const -1) (global.get $INVERSE_SQRT_2)))
(global.set $snake_unit_heading_y
(f32.mul (f32.const -1) (global.get $INVERSE_SQRT_2)))
return
))
(if (i32.and
(global.get $KEY_IS_DOWN_RIGHT)
(global.get $keys_are_down_mask)
) (then
(global.set $snake_unit_heading_x
(f32.mul (f32.const 1) (global.get $INVERSE_SQRT_2)))
(global.set $snake_unit_heading_y
(f32.mul (f32.const -1) (global.get $INVERSE_SQRT_2)))
return
))
(global.set $snake_unit_heading_x (f32.const 0))
(global.set $snake_unit_heading_y (f32.const -1))
return
))
(if (i32.and
(global.get $KEY_IS_DOWN_DOWN)
(global.get $keys_are_down_mask)
) (then
(if (i32.and
(global.get $KEY_IS_DOWN_LEFT)
(global.get $keys_are_down_mask)
) (then
(global.set $snake_unit_heading_x
(f32.mul (f32.const -1) (global.get $INVERSE_SQRT_2)))
(global.set $snake_unit_heading_y
(f32.mul (f32.const 1) (global.get $INVERSE_SQRT_2)))
return
))
(if (i32.and
(global.get $KEY_IS_DOWN_RIGHT)
(global.get $keys_are_down_mask)
) (then
(global.set $snake_unit_heading_x
(f32.mul (f32.const 1) (global.get $INVERSE_SQRT_2)))
(global.set $snake_unit_heading_y
(f32.mul (f32.const 1) (global.get $INVERSE_SQRT_2)))
return
))
(global.set $snake_unit_heading_x (f32.const 0))
(global.set $snake_unit_heading_y (f32.const 1))
return
))
(if (i32.and
(global.get $KEY_IS_DOWN_LEFT)
(global.get $keys_are_down_mask)
) (then
(global.set $snake_unit_heading_x (f32.const -1))
(global.set $snake_unit_heading_y (f32.const 0))
return
))
(if (i32.and
(global.get $KEY_IS_DOWN_RIGHT)
(global.get $keys_are_down_mask)
) (then
(global.set $snake_unit_heading_x (f32.const 1))
(global.set $snake_unit_heading_y (f32.const 0))
return
))
)
(func (export "input_touch")
(param $action i32) (param $x f32) (param $y f32)
(local $dx f32)
(local $dy f32)
(local $abs_dx f32)
(local $abs_dy f32)
(local $denom f32)
;; Special handling of touch down while in the start and end screen
(if (i32.and
(i32.eq (global.get $INPUT_START) (local.get $action))
(i32.or
(i32.eq
(global.get $GAME_STATE_START_SCREEN)
(global.get $game_state))
(i32.eq
(global.get $GAME_STATE_END_SCREEN)
(global.get $game_state)))
) (then
(call $reset_game)
(global.set $game_state (global.get $GAME_STATE_PLAYING))
call $orb_update
return
))
(local.set $dx (f32.sub (local.get $x) (call $snake_buf_read_head_cx)))
(local.set $dy (f32.sub (local.get $y) (call $snake_buf_read_head_cy)))
(local.set $abs_dx (f32.abs (local.get $dx)))
(local.set $abs_dy (f32.abs (local.get $dy)))
(local.set $denom (f32.add (local.get $abs_dx) (local.get $abs_dy)))
;; 1. Calculate the unit heading based by deviding the change in
;; x `$dx` and y `$dy` by a common denominator `$denom`.
;; 2. Gradually shrink `$denom` until the square root of the unit
;; heading greater than or equal to 1.
(loop $lp
(global.set $snake_unit_heading_x
(f32.div (local.get $dx) (local.get $denom)))
(global.set $snake_unit_heading_y
(f32.div (local.get $dy) (local.get $denom)))
(local.set $denom (f32.mul (local.get $denom) (f32.const 0.99)))
(f32.lt
(f32.add
(f32.mul
(global.get $snake_unit_heading_x)
(global.get $snake_unit_heading_x))
(f32.mul
(global.get $snake_unit_heading_y)
(global.get $snake_unit_heading_y)))
(f32.const 1.0))
br_if $lp
)
)
(func $resize_canvas (export "resize") (param $width i32) (param $height i32)
(local $pages_n i32)
(global.set $canvas_width (local.get $width))
(global.set $canvas_height (local.get $height))
(global.set $canvas_widthf (f32.convert_i32_s (local.get $width)))
(global.set $canvas_heightf (f32.convert_i32_s (local.get $height)))
;; Calculate the number of bytes required to store the canvas RGBA data
(global.set $memory_region_canvas_bytes_n
(i32.mul
(i32.mul (local.get $width) (local.get $height))
(i32.const 4)))
(local.set $pages_n
(i32.sub
(i32.add
(i32.div_u
(global.get $memory_region_canvas_bytes_n)
(i32.const 65536))
(i32.const 2))
(memory.size)))
(if (i32.gt_s (local.get $pages_n) (i32.const 0)) (then
;; Grow the linear memory to fit the RGBA data
(memory.grow (local.get $pages_n)) drop
))
(call $clear_canvas)
(if (i32.eq (global.get $game_state) (global.get $GAME_STATE_PLAYING)) (then
call $orb_draw
))
)
(func $reset_game
(call $resize_canvas (global.get $canvas_width) (global.get $canvas_height))
(call $snake_buf_reset)
(global.set $snake_length (f32.const 0))
(global.set $snake_target_length (f32.const 0))
call $snake_target_length_inc
(call $snake_buf_head_set
;; +0.5 because we want to center snake on the middle of a pixel
;; to avoid visible aliasing
(f32.add
(f32.floor
(f32.div
(f32.convert_i32_s (global.get $canvas_width))
(f32.const 2)))
(f32.const 0.5))
(f32.add
(f32.floor
(f32.div
(f32.convert_i32_s (global.get $canvas_height))
(f32.const 2)))
(f32.const 0.5))
(f32.const 0))
(global.set $snake_unit_heading_x (f32.const 1))
(global.set $snake_unit_heading_y (f32.const 0))
(global.set $score_100s_digit (i32.const 0))
(global.set $score_10s_digit (i32.const 0))
(global.set $score_1s_digit (i32.const 0))
(global.set $orb_x_as_canvas_width_frac (f32.const -1))
(global.set $orb_y_as_canvas_height_frac (f32.const -1))
(call $orb_prng_seed (i32.const 0))
)
(func $snake_update
(local $length_delta f32)
(local $snake_old_head_x f32)
(local $snake_old_head_y f32)
(local $snake_head_x f32)
(local $snake_head_y f32)
(local $snake_lerp_head_x f32)
(local $snake_lerp_head_y f32)
(local $lerp_dist f32)
(local $lerp_t f32)
(local.set $snake_old_head_x (call $snake_buf_read_head_cx))
(local.set $snake_old_head_y (call $snake_buf_read_head_cy))
;; Calculate the change in movement based on the speed in px/s
;; and the time between updates
(local.set $length_delta
(f32.mul
(global.get $SNAKE_MOVEMENT_PX_PER_S)
(f32.mul
(f32.sub
(global.get $timestamp_update_call)
(global.get $timestamp_last_update_call))
(f32.const 0.001))))
;; Calculate the current position of the head based on
;; the last position and the "change in movement"
(local.set $snake_head_x
(f32.add
(local.get $snake_old_head_x)
(f32.mul
(global.get $snake_unit_heading_x)
(local.get $length_delta))))
(local.set $snake_head_y
(f32.add
(local.get $snake_old_head_y)
(f32.mul
(global.get $snake_unit_heading_y)
(local.get $length_delta))))
(block $while (loop $lp
(f32.lt (global.get $snake_length) (global.get $snake_target_length))
br_if $while
;; Overdraw old tail circle with background color to visually
;; remove it from the canvas
(call $fill_circle_f32
(call $snake_buf_read_tail_cx)
(call $snake_buf_read_tail_cy)
(global.get $SNAKE_RADIUS)
(global.get $BACKGROUND_COLOR))
(call $snake_buf_tail_drop)
(global.set $snake_length (f32.sub
(global.get $snake_length)
(call $snake_buf_read_tail_length_delta)))
br $lp
))
;; Redraw the new last tail circle in the snake tail color, to
;; avoid a cutout in the snake left by the overdrawing during the
;; above loop
(call $fill_circle_f32
(call $snake_buf_read_tail_cx)
(call $snake_buf_read_tail_cy)
(global.get $SNAKE_RADIUS)
(global.get $SNAKE_TAIL_COLOR))
;; Add movement delta to length
(global.set $snake_length
(f32.add (global.get $snake_length) (local.get $length_delta)))
(local.set $lerp_dist (f32.const 0))
(loop $lerp_lp
(local.set $lerp_t
(f32.div (local.get $lerp_dist) (local.get $length_delta)))
(local.set $snake_lerp_head_x
(call $mod_f32
(f32.add
(f32.mul (local.get $lerp_t) (local.get $snake_head_x))
(f32.mul
(f32.sub (f32.const 1.0) (local.get $lerp_t))
(local.get $snake_old_head_x)))
(global.get $canvas_widthf)))
(local.set $snake_lerp_head_y
(call $mod_f32
(f32.add
(f32.mul (local.get $lerp_t) (local.get $snake_head_y))
(f32.mul
(f32.sub (f32.const 1.0) (local.get $lerp_t))
(local.get $snake_old_head_y)))
(global.get $canvas_heightf)))
(call $snake_buf_head_push
(local.get $snake_lerp_head_x)
(local.get $snake_lerp_head_y)
(global.get $SNAKE_LERP_INC))
(call $fill_circle_f32
(local.get $snake_lerp_head_x)
(local.get $snake_lerp_head_y)
(global.get $SNAKE_RADIUS)
(global.get $SNAKE_TAIL_COLOR))
(local.set $lerp_dist
(f32.add (local.get $lerp_dist) (global.get $SNAKE_LERP_INC)))
(f32.lt
(f32.add (local.get $lerp_dist) (global.get $SNAKE_LERP_INC))
(local.get $length_delta))
br_if $lerp_lp
)
(local.set $snake_head_x
(call $mod_f32
(local.get $snake_head_x)
(global.get $canvas_widthf)))
(local.set $snake_head_y
(call $mod_f32
(local.get $snake_head_y)
(global.get $canvas_heightf)))
(call $snake_buf_head_push
(local.get $snake_head_x)
(local.get $snake_head_y)
(f32.sub (local.get $length_delta) (local.get $lerp_dist)))
;; Draw the new snake head in brighter color so collisions are obvious
(call $fill_circle_f32
(local.get $snake_head_x)
(local.get $snake_head_y)
(global.get $SNAKE_RADIUS)
(global.get $SNAKE_HEAD_COLOR))
)
(func $snake_check_collision (result i32)
(local $circle_x f32)
(local $circle_y f32)
(call $snake_buf_read_ptr_point_to_tail)
(loop $lp
(local.set $circle_x (call $snake_buf_read_cx))
(local.set $circle_y (call $snake_buf_read_cy))
(call $snake_check_circle_collides_with_head
(local.get $circle_x) (local.get $circle_y)
(global.get $SNAKE_RADIUS))
(if (then
(i32.const 1)
return
))
(call $snake_buf_read_ptr_inc)
(i32.eq (call $snake_buf_read_ptr_is_at_head) (i32.const 0))
br_if $lp
)
(i32.const 0)
return
)
(func $snake_check_circle_collides_with_head
(param $cx f32) (param $cy f32)
(param $r f32)
(result i32)
(if (i32.eq
(call $check_point_is_infront
(call $snake_buf_read_head_cx) (call $snake_buf_read_head_cy)
(local.get $cx) (local.get $cy)
(global.get $snake_unit_heading_x) (global.get $snake_unit_heading_y))
(i32.const 0)
) (then
(return (i32.const 0))
))
(call $check_circles_collide
(call $snake_buf_read_head_cx) (call $snake_buf_read_head_cy)
(global.get $SNAKE_RADIUS)
(local.get $cx) (local.get $cy)
(local.get $r))
)
(func $check_circles_collide
(param $cx1 f32) (param $cy1 f32) (param $r1 f32)
(param $cx2 f32) (param $cy2 f32) (param $r2 f32)
(result i32)
(local $delta_cx f32)
(local $delta_cy f32)
(local $sum_r f32)
(local.set $delta_cx (f32.abs (f32.sub (local.get $cx2) (local.get $cx1))))
(local.set $delta_cy (f32.abs (f32.sub (local.get $cy2) (local.get $cy1))))
(local.set $sum_r (f32.add (local.get $r1) (local.get $r2)))
(f32.lt
(f32.add
(f32.mul (local.get $delta_cx) (local.get $delta_cx))
(f32.mul (local.get $delta_cy) (local.get $delta_cy)))
(f32.mul (local.get $sum_r) (local.get $sum_r)))
)
(func $check_point_is_infront
(param $origin_x f32) (param $origin_y f32)
(param $px f32) (param $py f32)
(param $direction_x f32) (param $direction_y f32)
(result i32)
(local $delta_x f32)
(local $delta_y f32)
(local.set $delta_x (f32.sub (local.get $px) (local.get $origin_x)))
(local.set $delta_y (f32.sub (local.get $py) (local.get $origin_y)))
;; We just check that the scalar product is greater than 0
(f32.gt
(f32.add
(f32.mul (local.get $direction_x) (local.get $delta_x))
(f32.mul (local.get $direction_y) (local.get $delta_y)))
(f32.const 0))
)
(func $snake_target_length_inc
(global.set $snake_target_length
(f32.add
(global.get $snake_target_length)
(f32.mul
(f32.const 0.05)
(f32.add
(global.get $canvas_widthf)
(global.get $canvas_heightf)))))
)
(func $orb_update
(local $orb_x f32)
(local $orb_y f32)
(local $old_orb_x f32)
(local $old_orb_y f32)
(local $snake_circle_x f32)
(local $snake_circle_y f32)
(local.set $old_orb_x (call $orb_calc_x))
(local.set $old_orb_y (call $orb_calc_y))
;; Loop until orb spawned at a coordinate that is NOT inside the snake
(loop $outer_lp
(global.set $orb_x_as_canvas_width_frac (call $orb_prng_gen_0_to_1))
(global.set $orb_y_as_canvas_height_frac (call $orb_prng_gen_0_to_1))
(local.set $orb_x (call $orb_calc_x))
(local.set $orb_y (call $orb_calc_y))
(call $snake_buf_read_ptr_point_to_tail)
(block $until (loop $inner_lp
(local.set $snake_circle_x (call $snake_buf_read_cx))
(local.set $snake_circle_y (call $snake_buf_read_cy))
(call $check_circles_collide
(local.get $orb_x) (local.get $orb_y)
(global.get $ORB_RADIUS)
(local.get $snake_circle_x) (local.get $snake_circle_y)
(global.get $SNAKE_RADIUS))
br_if $outer_lp
(call $snake_buf_read_ptr_inc)
(call $snake_buf_read_ptr_is_at_head)
br_if $until
br $inner_lp
))
)
;; Over draw old orb with the background color if it existed,
;; removing the orb.
;; Non-existent orbs have coord values of -1
(if (f32.ne (local.get $old_orb_x) (f32.const -1)) (then
(call $fill_circle_f32
(local.get $old_orb_x) (local.get $old_orb_y)
(global.get $ORB_RADIUS)
(global.get $BACKGROUND_COLOR))
))
call $orb_draw
)
(func $orb_calc_x (result f32)
(f32.mul
(global.get $orb_x_as_canvas_width_frac)
(global.get $canvas_widthf))
)
(func $orb_calc_y (result f32)
(f32.mul
(global.get $orb_y_as_canvas_height_frac)
(global.get $canvas_heightf))
)
(func $orb_draw
;; Draw orb
(call $fill_circle_f32
(call $orb_calc_x) (call $orb_calc_y)
(global.get $ORB_RADIUS)
(global.get $ORB_COLOR))
;; Draw specular highlight
(call $fill_circle_f32
(f32.add
(call $orb_calc_x)
(f32.mul (f32.const 0.2) (global.get $ORB_RADIUS)))
(f32.add
(call $orb_calc_y)
(f32.mul (f32.const -0.2) (global.get $ORB_RADIUS)))
(f32.mul (f32.const 0.4) (global.get $ORB_RADIUS))
(global.get $ORB_HIGHLIGHT_COLOR))
)
;; We use a "multiplicative congruential generator" (MCG) to randomly
;; generating the x and y-coordinates of of the orbs. The MCG uses
;; m = 2^32 (and c = 0) where "mod m" implicitly implemented using the
;; wrapping behaviour of i32 integers. Additionally we only use the 16
;; most significant bits of the random value "$orb_mcg_r" because
;; less significant bits have a low period.
;; Value from "Steele GL, Vigna S. Computationally easy, spectrally good multipliers for congruential pseudorandom number generators. Softw Pract Exper. 2022; 52(2): 443–458. doi:10.1002/spe.3030", Table 4, 24-Bit value.
(global $ORB_MCG_A i32 (i32.const 14971189))
(global $ORB_SEED_PRIME i32 (i32.const 7919))
(global $orb_mcg_r (mut i32) (i32.const 0))
(func $orb_prng_seed (param $seed i32)
;; Avoid seed having bad entropy by multiplying by a
;; largish prime.
(local.set $seed (i32.mul (local.get $seed) (global.get $ORB_SEED_PRIME)))
;; Avoid $seed being 0
(i32.eq (local.get $seed) (i32.const 0))
(if (then
(local.set $seed (global.get $ORB_SEED_PRIME))
))
(global.set $orb_mcg_r (local.get $seed))
)
(func $orb_prng_gen_0_to_1 (result f32)
(global.set $orb_mcg_r
(i32.mul (global.get $orb_mcg_r) (global.get $ORB_MCG_A)))
(f32.sub
(f32.reinterpret_i32
(i32.or
;; `0x3f800000` sets the f32 exponent bits to 127,
;; producing an exponent of 2^0=1
(i32.const 0x3f800000)
;; We set the f32 mantissa bits to the most significant
;; bits from the psuedorandom i32 number `$orb_mcg_r`.
;; We shift right by 9 because:
;; 1 sign bit + 8 exponent bits = 9 bits
(i32.shr_u (global.get $orb_mcg_r) (i32.const 9))))
;; The ored bits produces an f32 number between 1.0 and 2.0
;; so we need to subtract 1 to get a number between 0.0 and 1.0
(f32.const 1))
)
(func $score_inc
;; Overdraw the old score with the background color, removing it
(call $score_draw_colored (global.get $BACKGROUND_COLOR))
(global.set $score_1s_digit
(i32.add (global.get $score_1s_digit) (i32.const 1)))
(if (i32.eq (global.get $score_1s_digit) (i32.const 10)) (then
(global.set $score_1s_digit (i32.const 0))
(global.set $score_10s_digit
(i32.add (global.get $score_10s_digit) (i32.const 1)))
(if (i32.eq (global.get $score_10s_digit) (i32.const 10)) (then
(global.set $score_10s_digit (i32.const 0))
(global.set $score_100s_digit
(i32.add (global.get $score_100s_digit) (i32.const 1)))
))
))
;; Draw the new, updated score
(call $score_draw_colored (global.get $TEXT_COLOR))
)
(func $score_draw
(call $score_draw_colored (global.get $TEXT_COLOR))
)
(func $score_draw_colored (param $color i32)
(call $text_draw_start
(f32.const 16)
(f32.const 16)
(f32.const 16)
(local.get $color))
global.get $FONT_S call $text_draw_char
global.get $FONT_C call $text_draw_char
global.get $FONT_O call $text_draw_char
global.get $FONT_R call $text_draw_char
global.get $FONT_E call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
global.get $score_100s_digit call $text_draw_digit
global.get $score_10s_digit call $text_draw_digit
global.get $score_1s_digit call $text_draw_digit
)
(func $draw_start_screen_text
(local $msg_len f32)
(local $font_size f32)
(local.set $msg_len (f32.const 12))
(local.set $font_size (f32.const 16))
(call $text_draw_start
(f32.mul
(f32.sub
(global.get $canvas_widthf)
(f32.mul (local.get $msg_len) (local.get $font_size)))
(f32.const 0.5))
(f32.mul
(f32.sub
(global.get $canvas_heightf)
(f32.mul (f32.const 4) (local.get $font_size)))
(f32.const 0.5))
(local.get $font_size)
(global.get $TEXT_COLOR))
global.get $FONT_P call $text_draw_char
global.get $FONT_R call $text_draw_char
global.get $FONT_E call $text_draw_char
global.get $FONT_S call $text_draw_char
global.get $FONT_S call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
global.get $FONT_A call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
global.get $FONT_K call $text_draw_char
global.get $FONT_E call $text_draw_char
global.get $FONT_Y call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
(local.set $msg_len (f32.const 17))
(call $text_draw_start
(f32.mul
(f32.sub
(global.get $canvas_widthf)
(f32.mul (local.get $msg_len) (local.get $font_size)))
(f32.const 0.5))
(f32.mul
(f32.add
(global.get $canvas_heightf)
(f32.mul (f32.const 2) (local.get $font_size)))
(f32.const 0.5))
(local.get $font_size)
(global.get $TEXT_COLOR))
global.get $FONT_O call $text_draw_char
global.get $FONT_R call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
global.get $FONT_T call $text_draw_char
global.get $FONT_O call $text_draw_char
global.get $FONT_U call $text_draw_char
global.get $FONT_C call $text_draw_char
global.get $FONT_H call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
global.get $FONT_T call $text_draw_char
global.get $FONT_O call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
global.get $FONT_S call $text_draw_char
global.get $FONT_T call $text_draw_char
global.get $FONT_A call $text_draw_char
global.get $FONT_R call $text_draw_char
global.get $FONT_T call $text_draw_char
)
(func $draw_end_screen_text
(local $msg_len f32)
(local $font_size f32)
(local.set $msg_len (f32.const 10))
(local.set $font_size (f32.const 16))
(call $text_draw_start
(f32.mul
(f32.sub
(global.get $canvas_widthf)
(f32.mul (local.get $msg_len) (local.get $font_size)))
(f32.const 0.5))
(f32.mul
(f32.sub
(global.get $canvas_heightf)
(f32.mul (f32.const 8) (local.get $font_size)))
(f32.const 0.5))
(local.get $font_size)
(global.get $TEXT_COLOR))
global.get $FONT_G call $text_draw_char
global.get $FONT_A call $text_draw_char
global.get $FONT_M call $text_draw_char
global.get $FONT_E call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
global.get $FONT_O call $text_draw_char
global.get $FONT_V call $text_draw_char
global.get $FONT_E call $text_draw_char
global.get $FONT_R call $text_draw_char
global.get $FONT_EXCLAIMATION call $text_draw_char
(local.set $msg_len (f32.const 15))
(local.set $font_size (f32.const 24))
(call $text_draw_start
(f32.mul
(f32.sub
(global.get $canvas_widthf)
(f32.mul (local.get $msg_len) (local.get $font_size)))
(f32.const 0.5))
(f32.mul
(f32.sub (global.get $canvas_heightf) (local.get $font_size))
(f32.const 0.5))
(local.get $font_size)
(global.get $TEXT_COLOR))
global.get $FONT_F call $text_draw_char
global.get $FONT_I call $text_draw_char
global.get $FONT_N call $text_draw_char
global.get $FONT_A call $text_draw_char
global.get $FONT_L call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
global.get $FONT_S call $text_draw_char
global.get $FONT_C call $text_draw_char
global.get $FONT_O call $text_draw_char
global.get $FONT_R call $text_draw_char
global.get $FONT_E call $text_draw_char
global.get $FONT_SPACE call $text_draw_char
global.get $score_100s_digit call $text_draw_digit
global.get $score_10s_digit call $text_draw_digit
global.get $score_1s_digit call $text_draw_digit
(local.set $msg_len (f32.const 11))
(local.set $font_size (f32.const 16))
(call $text_draw_start
(f32.mul
(f32.sub
(global.get $canvas_widthf)
(f32.mul (local.get $msg_len) (local.get $font_size)))
(f32.const 0.5))
(f32.mul
(f32.add
(global.get $canvas_heightf)
(f32.mul (f32.const 7) (local.get $font_size)))
(f32.const 0.5))
(local.get $font_size)
(global.get $TEXT_COLOR))