-
Notifications
You must be signed in to change notification settings - Fork 19
/
game.c
860 lines (741 loc) · 23.8 KB
/
game.c
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
#include "./game.h"
// #define FEATURE_DYNAMIC_CAMERA
#define FEATURE_DEV
#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
#define TRUE 1
#define FALSE 0
static char logf_buf[4096] = {0};
#define LOGF(...) \
do { \
stbsp_snprintf(logf_buf, sizeof(logf_buf), __VA_ARGS__); \
platform_log(logf_buf); \
} while(0)
static void platform_assert(const char *file, i32 line, b32 cond, const char *message)
{
if (!cond) platform_panic(file, line, message);
}
#define ASSERT(cond, message) platform_assert(__FILE__, __LINE__, cond, message)
#define UNREACHABLE() platform_panic(__FILE__, __LINE__, "unreachable")
// NOTE: This implies that the platform has to carefully choose the resolution so the cells fit into the screen
#define CELL_SIZE 100
#define COLS 16
#define ROWS 9
#define BACKGROUND_COLOR 0xFF181818
#define CELL1_COLOR BACKGROUND_COLOR
#define CELL2_COLOR 0xFF183018
#define SNAKE_BODY_COLOR 0xFF189018
#define SNAKE_SPINE_COLOR 0xFF185018
#define SNAKE_SPINE_THICCNESS_PERCENT 0.05f
#define EGG_BODY_COLOR 0xFF31A6FF
#define EGG_SPINE_COLOR 0xFF3166BB
#define DEBUG_COLOR 0xFF0000FF
#define SNAKE_INIT_SIZE 3
#define STEP_INTEVAL 0.125f
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_ACCEPT ' '
#define KEY_RESTART 'r'
#define RAND_A 6364136223846793005ULL
#define RAND_C 1442695040888963407ULL
typedef enum {
ALIGN_LEFT,
ALIGN_RIGHT,
ALIGN_CENTER,
} Align;
static void fill_text_aligned(i32 x, i32 y, const char *text, u32 size, u32 color, Align align)
{
u32 width = platform_text_width(text, size);
switch (align) {
case ALIGN_LEFT: break;
case ALIGN_CENTER: x -= width/2; break;
case ALIGN_RIGHT: x -= width; break;
}
platform_fill_text(x, y, text, size, color);
}
static u32 rand(void)
{
static u64 rand_state = 0;
rand_state = rand_state*RAND_A + RAND_C;
return (rand_state >> 32)&0xFFFFFFFF;
}
static void *memset(void *mem, u32 c, u32 n)
{
void *result = mem;
u8 *bytes = mem;
while (n-- > 0) *bytes++ = c;
return result;
}
typedef enum {
DIR_RIGHT = 0,
DIR_UP,
DIR_LEFT,
DIR_DOWN,
COUNT_DIRS,
} Dir;
static Dir dir_opposite(Dir dir)
{
ASSERT(0 <= dir && dir < COUNT_DIRS, "Invalid direction");
return (dir + 2)%COUNT_DIRS;
}
typedef struct {
f32 x, y, w, h;
} Rect;
typedef struct {
f32 lens[COUNT_DIRS];
} Sides;
static Sides rect_sides(Rect rect)
{
Sides sides = {
.lens = {
[DIR_LEFT] = rect.x,
[DIR_RIGHT] = rect.x + rect.w,
[DIR_UP] = rect.y,
[DIR_DOWN] = rect.y + rect.h,
}
};
return sides;
}
static Rect sides_rect(Sides sides)
{
Rect rect = {
.x = sides.lens[DIR_LEFT],
.y = sides.lens[DIR_UP],
.w = sides.lens[DIR_RIGHT] - sides.lens[DIR_LEFT],
.h = sides.lens[DIR_DOWN] - sides.lens[DIR_UP],
};
return rect;
}
typedef struct {
i32 x, y;
} Cell;
typedef struct {
f32 x, y;
} Vec;
#define SNAKE_CAP (ROWS*COLS)
typedef struct {
Cell items[SNAKE_CAP];
u32 begin;
u32 size;
} Snake;
typedef struct {
Rect items[SNAKE_CAP];
Vec vels[SNAKE_CAP];
u8 masks[SNAKE_CAP];
u32 size;
} Dead_Snake;
typedef enum {
STATE_GAMEPLAY = 0,
STATE_PAUSE,
STATE_GAMEOVER,
} State;
#define DIR_QUEUE_CAP 3
typedef struct {
u32 begin;
u32 size;
Dir items[DIR_QUEUE_CAP];
} Dir_Queue;
typedef struct {
u32 width;
u32 height;
Vec camera_pos;
Vec camera_vel;
State state;
Snake snake;
Dead_Snake dead_snake;
Cell egg;
b32 eating_egg;
Dir dir;
Dir_Queue next_dirs;
b32 dir_keys[COUNT_DIRS];
f32 step_cooldown;
#ifdef FEATURE_DEV
f32 dt_scale;
#endif
b32 infinite_field;
u32 score;
char score_buffer[256];
} Game;
static Game game = {0};
static Rect cell_rect(Cell cell)
{
Rect result = {
.x = cell.x*CELL_SIZE,
.y = cell.y*CELL_SIZE,
.w = CELL_SIZE,
.h = CELL_SIZE,
};
return result;
}
#define ring_empty(ring) ((ring)->size == 0)
#define ring_cap(ring) (sizeof((ring)->items)/sizeof((ring)->items[0]))
#define ring_push_back(ring, item) \
do { \
ASSERT((ring)->size < ring_cap(ring), "Ring buffer overflow"); \
u32 index = ((ring)->begin + (ring)->size)%ring_cap(ring); \
(ring)->items[index] = (item); \
(ring)->size += 1; \
} while (0)
#define ring_displace_back(ring, item) \
do { \
u32 index = ((ring)->begin + (ring)->size)%ring_cap(ring); \
(ring)->items[index] = (item); \
if ((ring)->size < ring_cap(ring)) { \
(ring)->size += 1; \
} else { \
(ring)->begin = ((ring)->begin + 1)%ring_cap(ring); \
} \
} while (0)
#define ring_pop_front(ring) \
do { \
ASSERT((ring)->size > 0, "Ring buffer underflow"); \
(ring)->begin = ((ring)->begin + 1)%ring_cap(ring); \
(ring)->size -= 1; \
} while (0)
#define ring_back(ring) \
(ASSERT((ring)->size > 0, "Ring buffer is empty"), \
&(ring)->items[((ring)->begin + (ring)->size - 1)%ring_cap(ring)])
#define ring_front(ring) \
(ASSERT((ring)->size > 0, "Ring buffer is empty"), \
&(ring)->items[(ring)->begin])
#define ring_get(ring, index) \
(ASSERT((ring)->size > 0, "Ring buffer is empty"), \
&(ring)->items[((ring)->begin + (index))%ring_cap(ring)])
static b32 cell_eq(Cell a, Cell b)
{
return a.x == b.x && a.y == b.y;
}
static i32 is_cell_snake_body(Cell cell)
{
// TODO: ignoring the tail feel hacky @tail-ignore
for (u32 index = 1; index < game.snake.size; ++index) {
if (cell_eq(*ring_get(&game.snake, index), cell)) {
return index;
}
}
return -1;
}
static i32 emod(i32 a, i32 b)
{
return (a%b + b)%b;
}
static Cell cell_wrap(Cell cell)
{
cell.x = emod(cell.x, COLS);
cell.y = emod(cell.y, ROWS);
return cell;
}
static Cell dir_cell_data[COUNT_DIRS] = {
[DIR_LEFT] = {.x = -1},
[DIR_RIGHT] = {.x = 1},
[DIR_UP] = {.y = -1},
[DIR_DOWN] = {.y = 1},
};
static Cell cell_add(Cell a, Cell b)
{
a.x += b.x;
a.y += b.y;
return a;
}
#define dir_cell(dir) (ASSERT((u32) dir < COUNT_DIRS, "Invalid direction"), dir_cell_data[dir])
#define dir_vec(dir) cell_vec(dir_cell(dir))
static Cell step_cell(Cell head, Dir dir)
{
if (game.infinite_field) {
return cell_add(head, dir_cell(dir));
} else {
return cell_wrap(cell_add(head, dir_cell(dir)));
}
}
#define SNAKE_INIT_ROW (ROWS/2)
static void random_egg(b32 first)
{
i32 col1 = 0;
i32 col2 = COLS - 1;
i32 row1 = 0;
i32 row2 = ROWS - 1;
// TODO: make a single formula that works for any mode
if (game.infinite_field) {
col1 = (i32)(game.camera_pos.x - game.width*0.5f + CELL_SIZE)/CELL_SIZE;
col2 = (i32)(game.camera_pos.x + game.width*0.5f - CELL_SIZE)/CELL_SIZE;
row1 = (i32)(game.camera_pos.y - game.height*0.5f + CELL_SIZE)/CELL_SIZE;
row2 = (i32)(game.camera_pos.y + game.height*0.5f - CELL_SIZE)/CELL_SIZE;
}
#define RANDOM_EGG_MAX_ATTEMPTS 1000
u32 attempt = 0;
do {
game.egg.x = rand()%(col2 - col1 + 1) + col1;
game.egg.y = rand()%(row2 - row1 + 1) + row1;
attempt += 1;
} while ((is_cell_snake_body(game.egg) >= 0 || (first && game.egg.y == SNAKE_INIT_ROW)) && attempt < RANDOM_EGG_MAX_ATTEMPTS);
ASSERT(attempt <= RANDOM_EGG_MAX_ATTEMPTS, "TODO: make sure we have always at least one free visible cell");
}
// TODO: animation on restart
static void game_restart(u32 width, u32 height)
{
memset(&game, 0, sizeof(game));
#ifdef FEATURE_DEV
game.dt_scale = 1.0f;
#endif
game.width = width;
game.height = height;
game.camera_pos.x = width/2;
game.camera_pos.y = height/2;
for (u32 i = 0; i < SNAKE_INIT_SIZE; ++i) {
Cell head = {.x = i, .y = SNAKE_INIT_ROW};
ring_push_back(&game.snake, head);
}
random_egg(TRUE);
game.dir = DIR_RIGHT;
// TODO: Using snprintf to render Score is an overkill
// I believe snprintf should be only used for LOGF and in the "release" build stbsp_snprintf should not be included at all
stbsp_snprintf(game.score_buffer, sizeof(game.score_buffer), "Score: %u", game.score);
}
static f32 lerpf(f32 a, f32 b, f32 t)
{
return (b - a)*t + a;
}
static f32 ilerpf(f32 a, f32 b, f32 v)
{
return (v - a)/(b - a);
}
static void fill_rect(Rect rect, u32 color)
{
platform_fill_rect(
rect.x - game.camera_pos.x + game.width/2,
rect.y - game.camera_pos.y + game.height/2,
rect.w, rect.h, color);
}
#ifdef FEATURE_DEV
static void stroke_rect(Rect rect, u32 color)
{
platform_stroke_rect(
rect.x - game.camera_pos.x + game.width/2,
rect.y - game.camera_pos.y + game.height/2,
rect.w, rect.h, color);
}
#endif
static Rect scale_rect(Rect r, float a)
{
r.x = lerpf(r.x, r.x + r.w*0.5f, 1.0f - a);
r.y = lerpf(r.y, r.y + r.h*0.5f, 1.0f - a);
r.w = lerpf(0.0f, r.w, a);
r.h = lerpf(0.0f, r.h, a);
return r;
}
static void fill_cell(Cell cell, u32 color, f32 a)
{
fill_rect(scale_rect(cell_rect(cell), a), color);
}
static void fill_sides(Sides sides, u32 color)
{
fill_rect(sides_rect(sides), color);
}
static Dir cells_dir(Cell a, Cell b)
{
for (Dir dir = 0; dir < COUNT_DIRS; ++dir) {
if (cell_eq(step_cell(a, dir), b)) return dir;
}
UNREACHABLE();
return 0;
}
static Vec cell_center(Cell a)
{
return (Vec) {
.x = a.x*CELL_SIZE + CELL_SIZE/2,
.y = a.y*CELL_SIZE + CELL_SIZE/2,
};
}
static Sides slide_sides(Sides sides, Dir dir, f32 a)
{
f32 d = sides.lens[dir] - sides.lens[dir_opposite(dir)];
sides.lens[dir] += lerpf(0, d, a);
sides.lens[dir_opposite(dir)] += lerpf(0, d, a);
return sides;
}
Vec sides_center(Sides sides)
{
return (Vec) {
.x = sides.lens[DIR_LEFT] + (sides.lens[DIR_RIGHT] - sides.lens[DIR_LEFT])*0.5f,
.y = sides.lens[DIR_UP] + (sides.lens[DIR_DOWN] - sides.lens[DIR_UP])*0.5f,
};
}
static void fill_spine(Vec center, Dir dir, float len)
{
f32 thicc = CELL_SIZE*SNAKE_SPINE_THICCNESS_PERCENT;
Sides sides = {
.lens = {
[DIR_LEFT] = center.x - thicc,
[DIR_RIGHT] = center.x + thicc,
[DIR_UP] = center.y - thicc,
[DIR_DOWN] = center.y + thicc,
}
};
if (dir == DIR_RIGHT || dir == DIR_DOWN) sides.lens[dir] += len;
if (dir == DIR_LEFT || dir == DIR_UP) sides.lens[dir] -= len;
fill_sides(sides, SNAKE_SPINE_COLOR);
}
static void fill_fractured_spine(Sides sides, u8 mask)
{
f32 thicc = CELL_SIZE*SNAKE_SPINE_THICCNESS_PERCENT;
Vec center = sides_center(sides);
for (Dir dir = 0; dir < COUNT_DIRS; ++dir) {
if (mask&(1<<dir)) {
Sides arm = {
.lens = {
[DIR_LEFT] = center.x - thicc,
[DIR_RIGHT] = center.x + thicc,
[DIR_UP] = center.y - thicc,
[DIR_DOWN] = center.y + thicc,
}
};
arm.lens[dir] = sides.lens[dir];
fill_sides(arm, SNAKE_SPINE_COLOR);
}
}
}
static void snake_render(void)
{
f32 t = game.step_cooldown / STEP_INTEVAL;
Cell head_cell = *ring_back(&game.snake);
Sides head_sides = rect_sides(cell_rect(head_cell));
Dir head_dir = game.dir;
Sides head_slided_sides = slide_sides(head_sides, dir_opposite(head_dir), t);
Cell tail_cell = *ring_front(&game.snake);
Sides tail_sides = rect_sides(cell_rect(tail_cell));
Dir tail_dir = cells_dir(*ring_get(&game.snake, 0), *ring_get(&game.snake, 1));
Sides tail_slided_sides = slide_sides(tail_sides, tail_dir, game.eating_egg ? 1.0f : 1.0f - t);
if (game.eating_egg) {
fill_cell(head_cell, EGG_BODY_COLOR, 1.0f);
fill_cell(head_cell, EGG_SPINE_COLOR, SNAKE_SPINE_THICCNESS_PERCENT*2.0f);
}
fill_sides(head_slided_sides, SNAKE_BODY_COLOR);
fill_sides(tail_slided_sides, SNAKE_BODY_COLOR);
for (u32 index = 1; index < game.snake.size - 1; ++index) {
fill_cell(*ring_get(&game.snake, index), SNAKE_BODY_COLOR, 1.0f);
}
for (u32 index = 1; index < game.snake.size - 2; ++index) {
Cell cell1 = *ring_get(&game.snake, index);
Cell cell2 = *ring_get(&game.snake, index + 1);
// TODO: can we cache that direction in the snake itself?
fill_spine(cell_center(cell1), cells_dir(cell1, cell2), CELL_SIZE);
fill_spine(cell_center(cell2), cells_dir(cell2, cell1), CELL_SIZE);
}
// Head
{
Cell cell1 = *ring_get(&game.snake, game.snake.size - 2);
Cell cell2 = *ring_get(&game.snake, game.snake.size - 1);
f32 len = lerpf(0.0f, CELL_SIZE, 1.0f - t);
fill_spine(cell_center(cell1), cells_dir(cell1, cell2), len);
fill_spine(cell_center(cell_add(cell2, dir_cell(dir_opposite(head_dir)))), head_dir, len);
}
// Tail
{
Cell cell1 = *ring_get(&game.snake, 1);
Cell cell2 = *ring_get(&game.snake, 0);
f32 len = lerpf(0.0f, CELL_SIZE, game.eating_egg ? 0.0f : t);
fill_spine(cell_center(cell1), cells_dir(cell1, cell2), len);
fill_spine(cell_center(cell_add(cell2, dir_cell(tail_dir))), dir_opposite(tail_dir), len);
}
#ifdef FEATURE_DEV
for (u32 i = 0; i < game.snake.size; ++i) {
stroke_rect(cell_rect(*ring_get(&game.snake, i)), 0xFF0000FF);
}
#endif
}
static void background_render(void)
{
i32 col1 = (i32)(game.camera_pos.x - game.width*0.5f - CELL_SIZE)/CELL_SIZE;
i32 col2 = (i32)(game.camera_pos.x + game.width*0.5f + CELL_SIZE)/CELL_SIZE;
i32 row1 = (i32)(game.camera_pos.y - game.height*0.5f - CELL_SIZE)/CELL_SIZE;
i32 row2 = (i32)(game.camera_pos.y + game.height*0.5f + CELL_SIZE)/CELL_SIZE;
for (i32 col = col1; col <= col2; ++col) {
for (i32 row = row1; row <= row2; ++row) {
u32 color = (row + col)%2 == 0 ? CELL1_COLOR : CELL2_COLOR;
Cell cell = { .x = col, .y = row, };
fill_cell(cell, color, 1.0f);
}
}
}
// TODO: controls tutorial
void game_init(u32 width, u32 height)
{
game_restart(width, height);
LOGF("Game initialized");
}
#define SCORE_PADDING 100
// TODO: font size relative to the resolution
#define SCORE_FONT_SIZE 48
#define SCORE_FONT_COLOR 0xFFFFFFFF
#define PAUSE_FONT_COLOR SCORE_FONT_COLOR
#define PAUSE_FONT_SIZE SCORE_FONT_SIZE
#define GAMEOVER_FONT_COLOR SCORE_FONT_COLOR
#define GAMEOVER_FONT_SIZE SCORE_FONT_SIZE
static u32 color_alpha(u32 color, f32 a)
{
return (color&0x00FFFFFF)|((u32)(a*0xFF)<<(3*8));
}
static void egg_render(void)
{
if (game.eating_egg) {
f32 t = 1.0f - game.step_cooldown/STEP_INTEVAL;
f32 a = lerpf(1.5f, 1.0f, t*t);
fill_cell(game.egg, color_alpha(EGG_BODY_COLOR, t*t), a);
fill_cell(game.egg, color_alpha(EGG_SPINE_COLOR, t*t), a*(SNAKE_SPINE_THICCNESS_PERCENT*2.0f));
} else {
fill_cell(game.egg, EGG_BODY_COLOR, 1.0f);
fill_cell(game.egg, EGG_SPINE_COLOR, SNAKE_SPINE_THICCNESS_PERCENT*2.0f);
}
}
static void dead_snake_render(void)
{
// @tail-ignore
for (u32 i = 1; i < game.dead_snake.size; ++i) {
fill_rect(game.dead_snake.items[i], SNAKE_BODY_COLOR);
fill_fractured_spine(rect_sides(game.dead_snake.items[i]), game.dead_snake.masks[i]);
}
}
void game_render(void)
{
switch (game.state) {
case STATE_GAMEPLAY: {
background_render();
egg_render();
snake_render();
fill_text_aligned(SCORE_PADDING, SCORE_PADDING, game.score_buffer, SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_LEFT);
}
break;
case STATE_PAUSE: {
background_render();
egg_render();
snake_render();
fill_text_aligned(SCORE_PADDING, SCORE_PADDING, game.score_buffer, SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_LEFT);
// TODO: "Pause", "Game Over" are not centered vertically
fill_text_aligned(game.width/2, game.height/2, "Pause", PAUSE_FONT_SIZE, PAUSE_FONT_COLOR, ALIGN_CENTER);
}
break;
case STATE_GAMEOVER: {
background_render();
egg_render();
dead_snake_render();
fill_text_aligned(SCORE_PADDING, SCORE_PADDING, game.score_buffer, SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_LEFT);
fill_text_aligned(game.width/2, game.height/2, "Game Over", GAMEOVER_FONT_SIZE, GAMEOVER_FONT_COLOR, ALIGN_CENTER);
}
break;
default: {
UNREACHABLE();
}
}
#ifdef FEATURE_DEV
fill_text_aligned(game.width - SCORE_PADDING, SCORE_PADDING, "Dev", SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_RIGHT);
Rect rect = { .w = COLS*CELL_SIZE, .h = ROWS*CELL_SIZE };
stroke_rect(rect, 0xFF0000FF);
#endif
}
void game_keydown(int key)
{
#ifdef FEATURE_DEV
#define DEV_DT_SCALE_STEP 0.05f
switch (key) {
case 'z':
game.dt_scale -= DEV_DT_SCALE_STEP;
if (game.dt_scale < 0.0f) game.dt_scale = 0.0f;
LOGF("dt scale = %f", game.dt_scale);
break;
case 'x':
game.dt_scale += DEV_DT_SCALE_STEP;
LOGF("dt scale = %f", game.dt_scale);
break;
case 'c':
game.dt_scale = 1.0f;
LOGF("dt scale = %f", game.dt_scale);
break;
}
#endif
switch (game.state) {
case STATE_GAMEPLAY: {
switch (key) {
case KEY_UP:
ring_displace_back(&game.next_dirs, DIR_UP);
break;
case KEY_DOWN:
ring_displace_back(&game.next_dirs, DIR_DOWN);
break;
case KEY_LEFT:
ring_displace_back(&game.next_dirs, DIR_LEFT);
break;
case KEY_RIGHT:
ring_displace_back(&game.next_dirs, DIR_RIGHT);
break;
case KEY_ACCEPT:
game.state = STATE_PAUSE;
break;
case KEY_RESTART:
game_restart(game.width, game.height);
break;
}
}
break;
case STATE_PAUSE: {
switch (key) {
case KEY_ACCEPT:
game.state = STATE_GAMEPLAY;
break;
case KEY_RESTART:
game_restart(game.width, game.height);
break;
}
}
break;
case STATE_GAMEOVER: {
game_restart(game.width, game.height);
}
break;
default: {
UNREACHABLE();
}
}
}
static Vec vec_sub(Vec a, Vec b)
{
return (Vec) {
.x = a.x - b.x,
.y = a.y - b.y,
};
}
static f32 fabsf(f32 x)
{
if (x < 0.0f) x = -x;
return x;
}
static f32 sqrtf(f32 a)
{
float x = a;
for (u32 i = 0; i < 1000 && fabsf(x*x - a) > 1e-6; ++i) {
x -= (x*x - a)/(2*x);
}
return x;
}
static f32 vec_len(Vec a)
{
return sqrtf(a.x*a.x + a.y*a.y);
}
void game_resize(u32 width, u32 height)
{
game.width = width;
game.height = height;
}
void game_update(f32 dt)
{
#ifdef FEATURE_DEV
dt *= game.dt_scale;
#endif
#define CAMERA_VELOCITY_FACTOR 0.80f
if (game.infinite_field) {
game.camera_pos.x += game.camera_vel.x*CAMERA_VELOCITY_FACTOR*dt;
game.camera_pos.y += game.camera_vel.y*CAMERA_VELOCITY_FACTOR*dt;
game.camera_vel = vec_sub(
cell_center(*ring_back(&game.snake)),
game.camera_pos);
}
switch (game.state) {
case STATE_GAMEPLAY: {
game.step_cooldown -= dt;
if (game.step_cooldown <= 0.0f) {
if (!ring_empty(&game.next_dirs)) {
if (dir_opposite(game.dir) != *ring_front(&game.next_dirs)) {
game.dir = *ring_front(&game.next_dirs);
}
ring_pop_front(&game.next_dirs);
}
Cell next_head = step_cell(*ring_back(&game.snake), game.dir);
if (cell_eq(game.egg, next_head)) {
ring_push_back(&game.snake, next_head);
random_egg(FALSE);
game.eating_egg = TRUE;
#ifdef FEATURE_DYNAMIC_CAMERA
game.infinite_field = TRUE;
#endif
game.score += 1;
stbsp_snprintf(game.score_buffer, sizeof(game.score_buffer), "Score: %u", game.score);
} else {
i32 next_head_index = is_cell_snake_body(next_head);
if (next_head_index >= 0) {
// NOTE: reseting step_cooldown to 0 is important bcause the whole smooth movement is based on it.
// Without this reset the head of the snake "detaches" from the snake on the Game Over, when
// step_cooldown < 0.0f
game.step_cooldown = 0.0f;
game.state = STATE_GAMEOVER;
game.dead_snake.size = game.snake.size;
Vec head_center = cell_center(next_head);
for (u32 i = 0; i < game.snake.size; ++i) {
#define GAMEOVER_EXPLOSION_RADIUS 1000.0f
#define GAMEOVER_EXPLOSION_MAX_VEL 200.0f
Cell cell = *ring_get(&game.snake, i);
game.dead_snake.items[i] = cell_rect(cell);
if (!cell_eq(cell, next_head)) {
Vec vel_vec = vec_sub(cell_center(cell), head_center);
f32 vel_len = vec_len(vel_vec);
f32 t = ilerpf(0.0f, GAMEOVER_EXPLOSION_RADIUS, vel_len);
if (t > 1.0f) t = 1.0f;
t = 1.0f - t;
f32 noise_x = (rand()%1000)*0.01;
f32 noise_y = (rand()%1000)*0.01;
vel_vec.x = vel_vec.x/vel_len*GAMEOVER_EXPLOSION_MAX_VEL*t + noise_x;
vel_vec.y = vel_vec.y/vel_len*GAMEOVER_EXPLOSION_MAX_VEL*t + noise_y;
game.dead_snake.vels[i] = vel_vec;
// TODO: additional velocities along the body of the dead snake
} else {
game.dead_snake.vels[i].x = 0;
game.dead_snake.vels[i].y = 0;
}
// @tail-ignore
if (i > 0) {
game.dead_snake.masks[i] = 0;
if (i > 1) {
game.dead_snake.masks[i] |= 1 << cells_dir(cell, *ring_get(&game.snake, i - 1));
}
if (i < game.snake.size - 1) {
game.dead_snake.masks[i] |= 1 << cells_dir(cell, *ring_get(&game.snake, i + 1));
}
}
if (i == game.snake.size - 1) {
game.dead_snake.masks[i] |= 1 << game.dir;
}
}
game.dead_snake.masks[next_head_index] |= 1 << cells_dir(
*ring_get(&game.snake, next_head_index),
*ring_get(&game.snake, game.snake.size - 1));
return;
} else {
ring_push_back(&game.snake, next_head);
ring_pop_front(&game.snake);
game.eating_egg = FALSE;
}
}
game.step_cooldown = STEP_INTEVAL;
}
}
break;
case STATE_PAUSE:
{} break;
case STATE_GAMEOVER: {
// @tail-ignore
for (u32 i = 1; i < game.dead_snake.size; ++i) {
game.dead_snake.vels[i].x *= 0.99f;
game.dead_snake.vels[i].y *= 0.99f;
game.dead_snake.items[i].x += game.dead_snake.vels[i].x*dt;
game.dead_snake.items[i].y += game.dead_snake.vels[i].y*dt;
}
}
break;
default: {
UNREACHABLE();
}
}
}
// TODO: inifinite field mechanics
// TODO: starvation mechanics
// TODO: bug on wrapping around when eating the first egg