-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
1135 lines (963 loc) · 41.3 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
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
#include "game.h"
/****** Entities: Player ******/
//Useful for the reinforcement learning
enum EnemyAction {
EnemyActionAttack,
EnemyActionRetreat,
EnemyActionStand,
EnemyActionJump
};
Enemy enemies[MAX_ENEMIES];
#define MAX_BULLETS 30
Entity* bullets[MAX_BULLETS];
bool bulletsDirection[MAX_BULLETS];
Entity* enemyBullets[MAX_BULLETS];
bool enemyBulletsDirection[MAX_BULLETS];
// Forward declaration of player_desc, because it's used in player_spawn function.
/* Begin of variables from header */
//Configurable values
//TODO Reloading with faster shooting.
uint32_t shootingDelay = 500;
uint32_t enemyShootingDelay = 1500;
float bulletMoveSpeed = 0.55f;
float speed = 0.6f;
float enemySpeed = 0.13f;
float jumpHeight = 22.0F;
float jumpSpeed = 0.074f;
float enemyJumpHeight = 22.0F + 10.0F;
//Tracking player data
uint32_t kills = 0;
bool jumping = false;
float targetY = WORLD_BORDER_BOTTOM_Y;
float targetX = 10.0F;
bool horizontalGame = true;
bool horizontalView = true;
int16_t transitionLeftTicks = 0;
int16_t transitionRightTicks = 0;
//Internal vars
int firstMobSpawnTicks = 0;
//While debugging we increase all lives for longer testing/gameplay.
#ifdef DEBUGGING
int ENEMY_LIVES = 30;
int health = 100;
#else
int ENEMY_LIVES = STARTING_ENEMY_HEALTH;
int health = STARTING_PLAYER_HEALTH;
#endif
uint32_t firstKillTick = 0;
uint32_t secondKillTick = 0;
uint32_t gameBeginningTick = 0;
Level* gameLevel = NULL;
GameManager* globalGameManager = NULL;
/* End of header variables */
//Enemy, and bullet storage
Entity* globalPlayer = NULL;
//Internal variables
//static const EntityDescription player_desc;
static const EntityDescription target_desc;
//static const EntityDescription enemy_desc;
static const EntityDescription target_enemy_desc;
static const EntityDescription global_desc;
//More internal variables
uint32_t lastShotTick;
uint32_t lastBehaviorTick;
enum EnemyAction lastAction = EnemyActionStand;
float lerp(float y1, float y2, float t) {
return y1 + t * (y2 - y1);
}
static void frame_cb(GameEngine* engine, Canvas* canvas, InputState input, void* context) {
UNUSED(engine);
GameManager* game_manager = context;
game_manager_input_set(game_manager, input);
game_manager_update(game_manager);
game_manager_render(game_manager, canvas);
}
/*
static Vector random_pos(void) {
return (Vector){rand() % 120 + 4, rand() % 58 + 4};
}
*/
void player_spawn(Level* level, GameManager* manager) {
Entity* player = level_add_entity(level, &player_desc);
globalPlayer = player;
// Set player position.
// Depends on your game logic, it can be done in start entity function, but also can be done here.
entity_pos_set(player, (Vector){10, 32});
// Add collision box to player entity
// Box is centered in player x and y, and it's size is 16x16
entity_collider_add_rect(player, 16, 16);
// Get player context
PlayerContext* player_context = entity_context_get(player);
// Load player sprite
player_context->sprite_right = game_manager_sprite_load(manager, "player_right.fxbm");
player_context->sprite_right_shadowed =
game_manager_sprite_load(manager, "player_right_shadow.fxbm");
player_context->sprite_right_recoil =
game_manager_sprite_load(manager, "player_right_recoil.fxbm");
player_context->sprite_left = game_manager_sprite_load(manager, "player_left.fxbm");
player_context->sprite_left_shadowed =
game_manager_sprite_load(manager, "player_left_shadow.fxbm");
player_context->sprite_left_recoil =
game_manager_sprite_load(manager, "player_left_recoil.fxbm");
player_context->sprite_jump = game_manager_sprite_load(manager, "player_jump.fxbm");
player_context->sprite_stand = game_manager_sprite_load(manager, "player_stand.fxbm");
player_context->sprite_forward = game_manager_sprite_load(manager, "player_forward.fxbm");
player_context->sprite = player_context->sprite_right;
gameBeginningTick = furi_get_tick();
}
void enemy_spawn(
Level* level,
GameManager* manager,
Vector spawnPosition,
uint32_t mercyTicks,
bool right) {
int enemyIndex = -1;
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].instance != NULL) continue;
enemyIndex = i;
//FURI_LOG_I("deadzone", "PRE ALLOCATION!");
enemies[i].instance = level_add_entity(level, &enemy_desc);
enemies[i].direction = right;
//FURI_LOG_I("deadzone", "ALLOCATING ENEMY TO MEMORY!");
enemies[i].jumping = false;
enemies[i].targetY = WORLD_BORDER_BOTTOM_Y;
enemies[i].lives = ENEMY_LIVES;
enemies[i].spawnTime = furi_get_tick();
enemies[i].mercyTicks = mercyTicks;
enemies[i].lastShot = furi_get_tick() + 2000;
break;
}
// Too many enemies
if(enemyIndex == -1) return;
// Set enemy position.
// Depends on your game logic, it can be done in start entity function, but also can be done here.
entity_pos_set(enemies[enemyIndex].instance, spawnPosition);
//FURI_LOG_I("deadzone", "SET ENEMY POS");
// Add collision box to player entity
// Box is centered in player x and y, and it's size is 16x16
entity_collider_add_rect(enemies[enemyIndex].instance, 16, 16);
//FURI_LOG_I("deadzone", "SET ENEMY RECT");
// Get enemy context
PlayerContext* player_context = entity_context_get(enemies[enemyIndex].instance);
//FURI_LOG_I("deadzone", "GET PLAYER CTX FOR ENEMY");
// Load enemy sprite
player_context->sprite_left = game_manager_sprite_load(manager, "enemy_left.fxbm");
player_context->sprite_right = game_manager_sprite_load(manager, "enemy_right.fxbm");
if(right) {
player_context->sprite = player_context->sprite_right;
} else {
player_context->sprite = player_context->sprite_left;
}
//FURI_LOG_I("deadzone", "DONE SPAWNING");
}
void player_jump_handler(PlayerContext* playerCtx, Vector* pos, InputState* input) {
Sprite* jumpingSprite = horizontalGame ? playerCtx->sprite_jump : playerCtx->sprite_forward;
Sprite* standingSprite = horizontalGame ? playerCtx->sprite_stand : playerCtx->sprite_forward;
//Initiate jump process (first jump) if we are on ground (and are not jumping)
if(input->held & GameKeyUp && !jumping && roundf(pos->y) == WORLD_BORDER_BOTTOM_Y) {
//Start jumping
jumping = true;
//Set target Y (based on maximum jump height)
targetY = CLAMP(pos->y - jumpHeight, WORLD_BORDER_BOTTOM_Y, WORLD_BORDER_TOP_Y);
//Set player sprite to jumping
//playerCtx->sprite = jumpingSprite;
return;
}
//Interpolate to the target jump position
pos->y = lerp(pos->y, targetY, jumpSpeed);
//Continuation of jump (until threshold is reached)
if(jumping) {
//We've reached our threshold
if(roundf(pos->y) == roundf(targetY)) {
//Reset threshold and jumping state
targetY = WORLD_BORDER_BOTTOM_Y;
jumping = false;
}
}
if(roundf(pos->y) == WORLD_BORDER_BOTTOM_Y && !jumping && playerCtx->sprite == jumpingSprite) {
playerCtx->sprite = standingSprite;
}
}
void player_shoot_handler(PlayerContext* playerCtx, InputState* input, Vector* pos) {
//If we are not facing right or left, we cannot shoot
bool canShoot = playerCtx->sprite != playerCtx->sprite_jump &&
playerCtx->sprite != playerCtx->sprite_stand &&
playerCtx->sprite != playerCtx->sprite_left_recoil &&
playerCtx->sprite != playerCtx->sprite_right_recoil &&
playerCtx->sprite != playerCtx->sprite_left_shadowed &&
playerCtx->sprite != playerCtx->sprite_right_shadowed &&
playerCtx->sprite !=
playerCtx->sprite_forward; //TODO For now, can't shoot forward
uint32_t currentTick = furi_get_tick();
//Shooting action
if((input->held & GameKeyOk) && (currentTick - lastShotTick >= shootingDelay) && canShoot) {
lastShotTick = currentTick;
//Spawn bullet
int bulletIndex = -1;
//Find first empty bullet
for(int i = 0; i < MAX_BULLETS; i++) {
if(bullets[i] != NULL) continue;
bulletIndex = i;
break;
}
//Too many bullets in the scene.
if(bulletIndex == -1) return;
bullets[bulletIndex] = level_add_entity(gameLevel, &target_desc);
bulletsDirection[bulletIndex] = playerCtx->sprite == playerCtx->sprite_right ||
playerCtx->sprite == playerCtx->sprite_right_recoil ||
playerCtx->sprite == playerCtx->sprite_right_shadowed;
float deltaX = bulletsDirection[bulletIndex] ? 10 : -10;
// Set target position
Vector bulletPos = (Vector){pos->x + deltaX, pos->y};
entity_pos_set(bullets[bulletIndex], bulletPos);
//Handle recoil
float recoil = 0.0f;
if(playerCtx->sprite == playerCtx->sprite_right) {
playerCtx->sprite = playerCtx->sprite_right_recoil;
recoil -= 7;
} else if(playerCtx->sprite == playerCtx->sprite_left) {
playerCtx->sprite = playerCtx->sprite_left_recoil;
recoil += 7;
}
pos->x += recoil;
targetX += recoil;
}
if(furi_get_tick() - lastShotTick > 300) {
if(playerCtx->sprite == playerCtx->sprite_right_recoil) {
playerCtx->sprite = playerCtx->sprite_right;
} else if(playerCtx->sprite == playerCtx->sprite_left_recoil) {
playerCtx->sprite = playerCtx->sprite_left;
}
}
}
void enemy_shoot_handler(Enemy* enemy, Vector* pos, uint32_t* enemyLastShotTick, void* context) {
PlayerContext* enemyCtx = context;
uint32_t currentTick = furi_get_tick();
bool gracePeriod = furi_get_tick() < (enemy->spawnTime + enemy->mercyTicks);
float shootingRateFactor = gracePeriod ? 45.0F : 1;
//Shooting action
if(currentTick - *enemyLastShotTick >= (enemyShootingDelay * (shootingRateFactor)) &&
health != 0) {
*enemyLastShotTick = currentTick;
//Spawn bullet
int bulletIndex = -1;
//Find first empty bullet
for(int i = 0; i < MAX_BULLETS; i++) {
if(enemyBullets[i] != NULL) continue;
bulletIndex = i;
break;
}
//Too many bullets in the scene.
if(bulletIndex == -1) return;
enemyBullets[bulletIndex] = level_add_entity(gameLevel, &target_enemy_desc);
// Set target position
Vector bulletPos;
enemyBulletsDirection[bulletIndex] = enemyCtx->sprite == enemyCtx->sprite_right;
if(enemyBulletsDirection[bulletIndex]) {
bulletPos = (Vector){pos->x + 10, pos->y};
} else {
bulletPos = (Vector){pos->x - 10, pos->y};
}
entity_pos_set(enemyBullets[bulletIndex], bulletPos);
}
}
uint32_t lastSwitchRight = 0;
bool damage_player(Entity* self) {
const NotificationSequence* damageSound = &sequence_single_vibro;
if(--health == 0) {
//Ran out of lives
level_remove_entity(gameLevel, self);
//Replace damage sound with death sound
damageSound = &sequence_double_vibro;
/*
//Destroy all associated bullets
for(int i = 0; i < MAX_BULLETS; i++) {
if(bullets[i] == NULL) continue;
level_remove_entity(gameLevel, bullets[i]);
bullets[i] = NULL;
}*/
}
//Play sound of getting hit
NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
notification_message(notifications, damageSound);
notification_message(notifications, &sequence_blink_red_100);
return health == 0;
}
void player_update(Entity* self, GameManager* manager, void* context) {
if(game_menu_quit_selected) return;
UNUSED(context);
// Get game input
InputState input = game_manager_input_get(manager);
// Get player position
Vector pos = entity_pos_get(self);
PlayerContext* playerCtx = context;
//Player bullet collision
for(int i = 0; i < MAX_BULLETS; i++) {
if(enemyBullets[i] == NULL) continue;
Vector bulletPos = entity_pos_get(enemyBullets[i]);
float distXSq = (bulletPos.x - pos.x) * (bulletPos.x - pos.x);
float distYSq = (bulletPos.y - pos.y) * (bulletPos.y - pos.y);
float distance = sqrtf(distXSq + distYSq);
//FURI_LOG_I("deadzone","Enemy bullet dist: %f, %f",(double)sqrtf(distXSq),(double)sqrtf(distYSq));
if(distance < BULLET_DISTANCE_THRESHOLD ||
(playerCtx->sprite == playerCtx->sprite_jump && sqrtf(distXSq) < 6 &&
(distYSq > 0 ? distYSq < 8 : sqrtf(distYSq) < 7))) {
//Self destruction of bullet and potentially player
level_remove_entity(gameLevel, enemyBullets[i]);
enemyBullets[i] = NULL;
damage_player(self);
}
}
game_level_player_update(self, manager, playerCtx, &pos);
tutorial_level_player_update(self, manager, playerCtx, &pos);
//Movement - Game position starts at TOP LEFT.
//The higher the Y, the lower we move on the screen
//The higher the X, the more we move toward the right side.
//Jump Mechanics
player_jump_handler(playerCtx, &pos, &input);
//Player Shooting Mechanics
player_shoot_handler(playerCtx, &input, &pos);
if(input.held & GameKeyLeft && playerCtx->sprite != playerCtx->sprite_left_recoil) {
if(playerCtx->sprite == playerCtx->sprite_right_shadowed) {
transitionLeftTicks = TRANSITION_FRAMES;
}
//Switch sprite to left direction
if((playerCtx->sprite != playerCtx->sprite_left_shadowed &&
playerCtx->sprite != playerCtx->sprite_right_shadowed &&
playerCtx->sprite != playerCtx->sprite_left)) {
if(playerCtx->sprite == playerCtx->sprite_stand) {
//transitionLeftTicks++;
}
if(transitionLeftTicks < TRANSITION_FRAMES) {
transitionLeftTicks++;
if(playerCtx->sprite != playerCtx->sprite_stand &&
playerCtx->sprite != playerCtx->sprite_jump) {
playerCtx->sprite = playerCtx->sprite_forward;
}
} else if(
playerCtx->sprite != playerCtx->sprite_left_shadowed &&
transitionLeftTicks >= TRANSITION_FRAMES) {
transitionLeftTicks = 0;
if(playerCtx->sprite == playerCtx->sprite_right_shadowed) {
playerCtx->sprite = playerCtx->sprite_left_shadowed;
} else {
playerCtx->sprite = playerCtx->sprite_left;
}
}
}
if(playerCtx->sprite == playerCtx->sprite_left_shadowed ||
playerCtx->sprite == playerCtx->sprite_left) {
targetX -= speed;
targetX = CLAMP(targetX, WORLD_BORDER_RIGHT_X, WORLD_BORDER_LEFT_X);
pos.x = CLAMP(pos.x - speed, WORLD_BORDER_RIGHT_X, WORLD_BORDER_LEFT_X);
}
}
if(input.held & GameKeyRight && playerCtx->sprite != playerCtx->sprite_right_recoil) {
//Switch sprite to left direction
if(playerCtx->sprite == playerCtx->sprite_left_shadowed) {
transitionRightTicks = TRANSITION_FRAMES;
}
if(playerCtx->sprite != playerCtx->sprite_right_shadowed &&
playerCtx->sprite != playerCtx->sprite_right) {
if(playerCtx->sprite == playerCtx->sprite_forward) {
//transitionRightTicks++;
}
if(transitionRightTicks < TRANSITION_FRAMES) {
transitionRightTicks++;
if(playerCtx->sprite != playerCtx->sprite_forward &&
playerCtx->sprite != playerCtx->sprite_jump) {
playerCtx->sprite = playerCtx->sprite_stand;
}
} else if(
playerCtx->sprite != playerCtx->sprite_right_shadowed &&
transitionRightTicks >= TRANSITION_FRAMES) {
transitionRightTicks = 0;
if(playerCtx->sprite == playerCtx->sprite_left_shadowed) {
playerCtx->sprite = playerCtx->sprite_right_shadowed;
} else {
playerCtx->sprite = playerCtx->sprite_right;
}
}
}
//Switch to right direction
if(playerCtx->sprite == playerCtx->sprite_right_shadowed ||
playerCtx->sprite == playerCtx->sprite_right) {
targetX += speed;
targetX = CLAMP(targetX, WORLD_BORDER_RIGHT_X, WORLD_BORDER_LEFT_X);
pos.x = CLAMP(pos.x + speed, WORLD_BORDER_RIGHT_X, WORLD_BORDER_LEFT_X);
}
}
//TODO Add more animation states for turning around, so it's smoother.
//TODO Is this necessary?: (Switch to left/right when jumping up as part of jump animation)
if(input.pressed & GameKeyDown && playerCtx->sprite != playerCtx->sprite_left_recoil &&
playerCtx->sprite != playerCtx->sprite_right_recoil) {
playerCtx->sprite = playerCtx->sprite_forward;
}
// Clamp player position to screen bounds
pos.x = CLAMP(lerp(pos.x, targetX, jumpSpeed), WORLD_BORDER_RIGHT_X, WORLD_BORDER_LEFT_X);
pos.y = CLAMP(pos.y, WORLD_BORDER_BOTTOM_Y, WORLD_BORDER_TOP_Y);
//Screen space begins at top left corner.
//Y increases as we go down.
//X increases as we go right
// Set new player position
entity_pos_set(self, pos);
// Control game exit
if(input.pressed & GameKeyBack) {
//Only reaches if player is alive. Send them to the main menu
game_manager_game_stop(manager);
}
}
void global_update(Entity* self, GameManager* manager, void* context) {
UNUSED(self);
UNUSED(context);
//Check player input when they're dead, since enemy is alive, processing must proceed here.
InputState input = game_manager_input_get(manager);
if(input.pressed & GameKeyBack) {
if(health == 0) {
//If dead, restart game
health = STARTING_PLAYER_HEALTH;
player_spawn(gameLevel, manager);
entity_pos_set(globalPlayer, (Vector){WORLD_BORDER_LEFT_X, WORLD_BORDER_BOTTOM_Y});
}
}
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].instance == NULL || globalPlayer == NULL) continue;
Vector playerPos = entity_pos_get(globalPlayer);
Vector enemyPos = entity_pos_get(enemies[i].instance);
if(playerPos.x - enemyPos.x > 0) {
enemies[i].direction = true;
} else {
enemies[i].direction = false;
}
}
//Player bullet movement mechanics
for(int i = 0; i < MAX_BULLETS; i++) {
if(bullets[i] == NULL) continue;
Vector bulletPos = entity_pos_get(bullets[i]);
float deltaX = bulletsDirection[i] ? bulletMoveSpeed : -bulletMoveSpeed;
bulletPos.x =
CLAMP(bulletPos.x + deltaX, WORLD_BORDER_RIGHT_X + 5, WORLD_BORDER_LEFT_X - 5);
entity_pos_set(bullets[i], bulletPos);
//Once bullet reaches end, destroy it
if(roundf(bulletPos.x) >= WORLD_BORDER_RIGHT_X ||
roundf(bulletPos.x) <= WORLD_BORDER_LEFT_X) {
level_remove_entity(gameLevel, bullets[i]);
bullets[i] = NULL;
}
}
//Enemy bullet movement mechanics
for(int i = 0; i < MAX_BULLETS; i++) {
if(enemyBullets[i] == NULL) continue;
Vector bulletPos = entity_pos_get(enemyBullets[i]);
//TODO Currently movement is dependent on enemy's trajectory, no metadata of bullet.
float deltaX = enemyBulletsDirection[i] ? bulletMoveSpeed : -bulletMoveSpeed;
bulletPos.x =
CLAMP(bulletPos.x + deltaX, WORLD_BORDER_RIGHT_X + 5, WORLD_BORDER_LEFT_X - 5);
entity_pos_set(enemyBullets[i], bulletPos);
//Once bullet reaches end, destroy it
if(roundf(bulletPos.x) >= WORLD_BORDER_RIGHT_X ||
roundf(bulletPos.x) <= WORLD_BORDER_LEFT_X) {
level_remove_entity(gameLevel, enemyBullets[i]);
enemyBullets[i] = NULL;
}
}
}
void global_render(Entity* self, GameManager* manager, Canvas* canvas, void* context) {
UNUSED(self);
UNUSED(manager);
UNUSED(context);
UNUSED(context);
if(health == 0) {
canvas_printf(canvas, 30, 10, "You're dead.");
canvas_printf(canvas, 20, 20, "Press Back to respawn.");
}
}
void player_render(Entity* self, GameManager* manager, Canvas* canvas, void* context) {
if(game_menu_quit_selected) return;
// Get player context
PlayerContext* player = context;
// Get player position
Vector pos = entity_pos_get(self);
// Draw player sprite
// We subtract 5 from x and y, because collision box is 10x10, and we want to center sprite in it.
canvas_draw_sprite(canvas, player->sprite, pos.x - 5, pos.y - 5);
/*if((transitionLeftTicks < TRANSITION_FRAMES && transitionLeftTicks != 0) ||
(transitionRightTicks < TRANSITION_FRAMES && transitionRightTicks != 0)) {
canvas_draw_line(canvas, pos.x - 3, pos.y, pos.x - 5, pos.y);
canvas_draw_line(canvas, pos.x + 3, pos.y, pos.x + 5, pos.y);
}*/
if(horizontalView) {
canvas_draw_frame(canvas, 2, -4, 124, 61);
canvas_draw_line(canvas, 1, 57, 0, 60);
canvas_draw_line(canvas, 126, 57, 128, 61);
} else {
canvas_draw_frame(canvas, 32, -4, 64, 30);
canvas_draw_line(canvas, 32, 26, 0, 57);
canvas_draw_line(canvas, 64 + 32, 26, 128, 57);
}
//canvas_draw_frame(canvas, 0, -4, 135, 64);
// Get game context
GameContext* game_context = game_manager_game_context_get(manager);
UNUSED(game_context);
// Draw score
//canvas_printf(canvas, 60, 7, "Enemy Lives: %d", enemies[0].lives);
canvas_printf(canvas, 80, 7, "Health: %d", health);
if(kills > 0) {
canvas_printf(canvas, 10, 7, "Kills: %d", kills);
}
game_level_player_render(manager, canvas, player);
tutorial_level_player_render(manager, canvas, player);
#ifdef DEBUGGING
canvas_printf(
canvas,
20,
50,
"Weights: %.1f, %.1f",
(double)game_ai_weights[0],
(double)game_ai_weights[1]);
canvas_printf(
canvas, 40, 60, "%.1f, %.1f", (double)game_ai_weights[2], (double)game_ai_weights[3]);
#endif
}
void enemy_render(Entity* self, GameManager* manager, Canvas* canvas, void* context) {
if(game_menu_quit_selected) return;
UNUSED(manager);
// Get enemy context
PlayerContext* player = context;
// Get enemy position
Vector pos = entity_pos_get(self);
//Draw enemy health above their head
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].instance != self) continue;
if(enemies[i].direction) {
player->sprite = player->sprite_right;
} else {
player->sprite = player->sprite_left;
}
canvas_printf(canvas, pos.x + 1, pos.y - 10, "%d", enemies[i].lives);
break;
}
// Draw enemy sprite
// We subtract 5 from x and y, because collision box is 16x16, and we want to center sprite in it.
canvas_draw_sprite(canvas, player->sprite, pos.x - 5, pos.y - 5);
//This must be processed here, as the player object will be deinstantiated
game_level_enemy_render(manager, canvas, context);
game_level_player_render(manager, canvas, context);
}
void enemy_update(Entity* self, GameManager* manager, void* context) {
if(game_menu_quit_selected) return;
PlayerContext* enemyCtx = context;
UNUSED(manager);
Vector pos = entity_pos_get(self);
//Bullet collision
for(int i = 0; i < MAX_BULLETS; i++) {
if(bullets[i] == NULL) continue;
Vector bulletPos = entity_pos_get(bullets[i]);
float distXSq = (bulletPos.x - pos.x) * (bulletPos.x - pos.x);
float distYSq = (bulletPos.y - pos.y) * (bulletPos.y - pos.y);
float distance = sqrtf(distXSq + distYSq);
//FURI_LOG_I("deadzone", "Bullet dist: %f", (double)distance);
if(distance < BULLET_DISTANCE_THRESHOLD) {
//Self destruction of bullet and entity
level_remove_entity(gameLevel, bullets[i]);
bullets[i] = NULL;
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].instance != self) continue;
const NotificationSequence* damageSound = &sequence_semi_success;
if(--enemies[i].lives == 0) {
//Ran out of lives
level_remove_entity(gameLevel, self);
//Replace damage sound with death sound
damageSound = &sequence_success;
enemies[i].instance = NULL;
kills++;
if(kills == 1) {
firstKillTick = furi_get_tick();
}
if(enemies[i].instance != NULL) {
update_weights(
game_ai_weights,
game_ai_features,
enemies[i].jumping ? EnemyActionJump : lastAction,
-400 / abs((int)roundf(
entity_pos_get(enemies[i].instance).x -
entity_pos_get(globalPlayer).x)));
}
}
//Play sound of getting hit
NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
notification_message(notifications, damageSound);
break;
}
break;
}
}
//Enemy NPC behavior
uint32_t currentTick = furi_get_tick();
if(currentTick - lastBehaviorTick > 200) {
lastBehaviorTick = currentTick;
Vector playerPos = entity_pos_get(globalPlayer);
float distXSqToPlayer = (playerPos.x - pos.x) * (playerPos.x - pos.x);
float distYSqToPlayer = (playerPos.y - pos.y) * (playerPos.y - pos.y);
float distanceToPlayer = sqrtf(distXSqToPlayer + distYSqToPlayer);
if(roundf(distanceToPlayer) > 80.0f) {
update_weights(game_ai_weights, game_ai_features, EnemyActionAttack, -300);
} else if(roundf(distanceToPlayer) < 45.0f) {
update_weights(game_ai_weights, game_ai_features, EnemyActionAttack, -300);
} else if(roundf(distanceToPlayer) > 45.0f) {
update_weights(game_ai_weights, game_ai_features, EnemyActionAttack, 300);
}
if(roundf(pos.y) == WORLD_BORDER_BOTTOM_Y) {
update_weights(game_ai_weights, game_ai_features, lastAction, 10.0f);
}
Vector closestBullet = (Vector){200.0F, 200.0F};
for(int i = 0; i < MAX_BULLETS; i++) {
if(bullets[i] == NULL) continue;
Vector bulletPos = entity_pos_get(bullets[i]);
float distXSq = (bulletPos.x - pos.x) * (bulletPos.x - pos.x);
float distYSq = (bulletPos.y - pos.y) * (bulletPos.y - pos.y);
float distance = sqrtf(distXSq + distYSq);
float closestBulletDistX = (closestBullet.x - pos.x) * (closestBullet.x - pos.x);
float closestBulletDistY = (closestBullet.y - pos.y) * (closestBullet.y - pos.y);
float closestBulletDist = sqrtf(closestBulletDistX + closestBulletDistY);
if(distance <= closestBulletDist) {
closestBullet = bulletPos;
}
}
bool jumped = false;
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].instance != self) continue;
jumped = enemies[i].jumping;
break;
}
if(closestBullet.x > pos.x && (closestBullet.x - pos.x) < 0.9F) {
update_weights(
game_ai_weights,
game_ai_features,
jumped ? EnemyActionJump : lastAction,
500 / abs((int)roundf(pos.x - entity_pos_get(globalPlayer).x)));
}
if(closestBullet.y < pos.y && (pos.y - closestBullet.y) < 1.0f) {
update_weights(
game_ai_weights,
game_ai_features,
EnemyActionJump,
500 / abs((int)roundf(pos.x - entity_pos_get(globalPlayer).x)));
}
enum EnemyAction action = (enum EnemyAction)decide_action(
pos.x,
pos.y,
closestBullet.x,
closestBullet.y,
playerPos.x,
playerPos.y,
game_ai_weights);
lastAction = action;
switch(action) {
case EnemyActionAttack:
Enemy* enemy = NULL;
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].instance != self) continue;
enemy = &enemies[i];
break;
}
bool gracePeriod = furi_get_tick() < (enemy->spawnTime + enemy->mercyTicks);
if(enemyCtx->sprite == enemyCtx->sprite_left) {
pos.x = CLAMP(
lerp(pos.x, playerPos.x + ((pos.x - playerPos.x > 0) ? 30 : -30), enemySpeed),
WORLD_BORDER_RIGHT_X,
//Prevent from approaching too much during player spawning / grace period
gracePeriod ? (WORLD_BORDER_RIGHT_X - 14) : WORLD_BORDER_LEFT_X);
} else {
pos.x = CLAMP(
lerp(pos.x, pos.x + (pos.x - playerPos.x), enemySpeed),
WORLD_BORDER_RIGHT_X,
WORLD_BORDER_LEFT_X);
break;
}
break;
case EnemyActionRetreat:
pos.x = CLAMP(
lerp(
pos.x,
pos.x + (enemyCtx->sprite == enemyCtx->sprite_left ? (pos.x - playerPos.x) :
(playerPos.x - pos.x)),
enemySpeed),
WORLD_BORDER_RIGHT_X,
WORLD_BORDER_LEFT_X);
break;
case EnemyActionStand:
break;
case EnemyActionJump:
//Prevent from jumping during start of first level (player spawning)
if(furi_get_tick() - firstMobSpawnTicks < 3000) {
break;
}
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].instance != self) continue;
//Is on ground and can jump
if(!enemies[i].jumping) {
enemies[i].jumping = true;
//Enemies have a higher jump height
enemies[i].targetY = CLAMP(
WORLD_BORDER_BOTTOM_Y - (enemyJumpHeight),
WORLD_BORDER_BOTTOM_Y,
WORLD_BORDER_TOP_Y);
return;
}
break;
}
break;
}
}
//Gravity & movement interpolation
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].instance != self) continue;
pos.y = lerp(pos.y, enemies[i].targetY, jumpSpeed);
if(enemies[i].jumping && roundf(pos.y) == roundf(enemies[i].targetY)) {
enemies[i].targetY = WORLD_BORDER_BOTTOM_Y;
}
if(enemies[i].jumping && roundf(pos.y) == WORLD_BORDER_BOTTOM_Y) {
enemies[i].jumping = false;
}
break;
}
//Enemy shooting
for(int i = 0; i < MAX_ENEMIES; i++) {
if(enemies[i].instance != self) continue;
enemy_shoot_handler(&enemies[i], &pos, &enemies[i].lastShot, context);
break;
}
//Update position of npc
entity_pos_set(self, pos);
}
/****** Entities: Target ******/
static void target_render(Entity* self, GameManager* manager, Canvas* canvas, void* context) {
if(game_menu_quit_selected) return;
UNUSED(context);
UNUSED(manager);
// Get target position
Vector pos = entity_pos_get(self);
// Draw target
//canvas_draw_box(canvas, pos.x, pos.y, 8, 5);
canvas_draw_disc(canvas, pos.x, pos.y, 2);
}
static const EntityDescription target_desc = {
.start = NULL, // called when entity is added to the level
.stop = NULL, // called when entity is removed from the level
.update = NULL, // called every frame
.render = target_render, // called every frame, after update
.collision = NULL, // called when entity collides with another entity
.event = NULL, // called when entity receives an event
.context_size = 0, // size of entity context, will be automatically allocated and freed
};
static const EntityDescription target_enemy_desc = {
.start = NULL, // called when entity is added to the level
.stop = NULL, // called when entity is removed from the level
.update = NULL, // called every frame
.render = target_render, // called every frame, after update
.collision = NULL, // called when entity collides with another entity
.event = NULL, // called when entity receives an event
.context_size = 0, // size of entity context, will be automatically allocated and freed
};
static const EntityDescription global_desc = {
.start = NULL, // called when entity is added to the level
.stop = NULL, // called when entity is removed from the level
.update = global_update, // called every frame
.render = global_render, // called every frame, after update
.collision = NULL, // called when entity collides with another entity
.event = NULL, // called when entity receives an event
.context_size = 0, // size of entity context, will be automatically allocated and freed
};
static void tutorial_level_alloc(Level* level, GameManager* manager) {
if(!game_menu_tutorial_selected) return;
//Add enemy to level
if(firstKillTick == 0) enemy_spawn(level, manager, (Vector){110, 49}, 3000, false);
}
/****** Level ******/
static void level_alloc(Level* level, GameManager* manager, void* context) {
if(game_menu_quit_selected) return;
UNUSED(manager);
UNUSED(context);
level_add_entity(level, &global_desc);
// Add player entity to the level
player_spawn(level, manager);
tutorial_level_alloc(level, manager);
// Add target entity to the level
//level_add_entity(level, &target_desc);
//level_add_entity(level, &target_enemy_desc);
gameLevel = level;
}
/*
Alloc/free is called once, when level is added/removed from the game.
It useful if you have small amount of levels and entities, that can be allocated at once.
Start/stop is called when level is changed to/from this level.
It will save memory, because you can allocate entities only for current level.
*/
static const LevelBehaviour level = {
.alloc = level_alloc, // called once, when level allocated
.free = NULL, // called once, when level freed
.start = NULL, // called when level is changed to this level
.stop = NULL, // called when level is changed from this level
.context_size = 0, // size of level context, will be automatically allocated and freed
};
/****** Game ******/
static void game_start_post_menu(GameManager* game_manager, void* ctx) {
// Do some initialization here, for example you can load score from storage.
// For simplicity, we will just set it to 0.
GameContext* game_context = ctx;
game_context->score = 0;
globalGameManager = game_manager;
// Add level to the game
game_manager_add_level(game_manager, &level);
//Instantiate all bullets
for(int i = 0; i < MAX_BULLETS; i++) {
bullets[i] = NULL;
enemyBullets[i] = NULL;
}
//TODO ANIMATIONS FOR GAME, such as particles going up like fire on player
//TODO walk animation, optional shooting animation
}
/*
Write here the start code for your game, for example: creating a level and so on.
Game context is allocated (game.context_size) and passed to this function, you can use it to store your game data.
*/
static void game_start(GameManager* game_manager, void* ctx) {
UNUSED(game_manager);
if(game_menu_quit_selected) return;
//Instantiate the lock
game_menu_open(game_manager, false);
if(game_menu_settings_selected) {
// End access to the GUI API.
//furi_record_close(RECORD_GUI);
Gui* gui = furi_record_open(RECORD_GUI);
Submenu* submenu = submenu_alloc();
submenu_add_item(submenu, "A", 0, game_settings_menu_button_callback, game_manager);
submenu_add_item(submenu, "B", 1, game_settings_menu_button_callback, game_manager);
submenu_add_item(submenu, "C", 2, game_settings_menu_button_callback, game_manager);
submenu_add_item(submenu, "BACK", 3, game_settings_menu_button_callback, game_manager);
ViewHolder* view_holder = view_holder_alloc();
view_holder_set_view(view_holder, submenu_get_view(submenu));
view_holder_attach_to_gui(view_holder, gui);
api_lock_wait_unlock(game_menu_exit_lock);
view_holder_set_view(view_holder, NULL);
// Delete everything to prevent memory leaks.