-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
2540 lines (2267 loc) · 77.2 KB
/
main.cpp
File metadata and controls
2540 lines (2267 loc) · 77.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
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
/*******************************************************************************************
*
* raylib [core] example - Basic window
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
*for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
// #define __EMSCRIPTEN__
#include "raylib.h"
#include "raymath.h"
#include <map>
#include <string>
const int NUM_POWERUP_TYPES = 9;
enum powerup_types {
SHOT_SPEED,
SHOT_RELOAD,
SHOT_POWER,
SHOT_SPREAD,
SHIELD_REPAIR,
SHIELD_MAX,
HULL_REPAIR,
HULL_MAX,
HULL_STRENGTH
};
typedef enum {
keyMoveUp,
keyMoveDown,
keyMoveLeft,
keyMoveRight,
keyShoot,
keyToggleAutoshoot,
keyGodMode,
keyAbility,
keyPause,
} keys;
typedef enum {
mouseShoot,
} mouseKeys;
std::map<keys, KeyboardKey> keymap = {
{keyMoveUp, KEY_W},
{keyMoveDown, KEY_S},
{keyMoveLeft, KEY_A},
{keyMoveRight, KEY_D},
{keyShoot, KEY_SPACE},
{keyToggleAutoshoot, KEY_I},
{keyGodMode, KEY_G},
{keyAbility, KEY_LEFT_SHIFT},
{keyPause, KEY_P},
};
std::map<mouseKeys, MouseButton> mouseKeymap = {
{mouseShoot, MOUSE_BUTTON_LEFT},
};
const std::pair<int, int> upgrade_matrix[NUM_POWERUP_TYPES] = {
{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}};
const int NUM_ENEMY_TYPES= 36;
const int NUM_SHOT_STYLES = 30;
const int NUM_BOSS_TYPES = 6;
const int NUM_BOSS_SHOT_STYLES = 18;
/* const int NUM_BOSS_SPECIAL_ATTACKS = 4; */
const int NUM_METALS = 4;
enum metals { IRON, NICKEL, COBALT, SILICON };
const int NUM_ACHIEVEMENTS = 5;
// -----------------------------------------------------------
// Bounding boxes for every object that has a collision.
// These are arrays of Rectangle objects that contain the
// appropriate offsets for each subitem within the spritemap
// area.
//
// { object.x, object.y, object.width, object.height }
// -----------------------------------------------------------
const Rectangle shot_size_matrix[NUM_SHOT_STYLES] = {
{4, 4, 1, 1}, {3, 4, 1, 2}, {3, 3, 1, 3}, {3, 3, 2, 3}, {3, 3, 3, 2},
{2, 3, 4, 2}, {2, 1, 3, 6}, {2, 1, 3, 6}, {0, 0, 8, 8}, {1, 1, 6, 6},
{1, 1, 6, 4}, {0, 0, 8, 5}, {2, 1, 3, 5}, {2, 1, 3, 6}, {3, 3, 2, 2},
{2, 2, 4, 4}, {3, 1, 3, 4}, {2, 1, 4, 5}, {2, 1, 3, 6}, {2, 1, 3, 6},
{3, 1, 3, 6}, {2, 3, 3, 3}, {1, 1, 6, 6}, {2, 1, 4, 5}, {2, 1, 5, 5},
{0, 0, 8, 8}, {1, 1, 6, 6}, {1, 1, 6, 6}, {3, 3, 3, 3}, {2, 1, 3, 6}};
const Rectangle boss_shot_size_matrix[NUM_BOSS_SHOT_STYLES] = {
{3, 0, 3, 8}, {2, 0, 4, 8}, {1, 0, 6, 8},
{2, 0, 6, 8}, {2, 1, 6, 5}, {2, 1, 6, 5},
{3, 1, 1, 6}, {0, 0, 8, 5}, {2, 0, 5, 8},
{3, 0, 3, 8}, {2, 1, 3, 6}, {2, 1, 2, 6},
{0, 0, 8, 8}, {-1, 0, 10, 4}, {1, 1, 6, 5},
{0, 0, 8, 6}, {2, 1, 4, 6}, {3, 1, 3, 6},
};
const Rectangle enemy_size_matrix[NUM_ENEMY_TYPES] = {
{2, 2, 5, 5}, {0, 2, 7, 5}, {1, 2, 5, 5},
{1, 3, 6, 5}, {2, 2, 5, 4}, {1, 2, 7, 5},
{1, 2, 6, 4}, {1, 2, 6, 4}, {1, 2, 5, 5},
{1, 2, 6, 5}, {1, 2, 6, 5}, {2, 2, 4, 5},
{1, 1, 7, 6}, {0, 1, 8, 6}, {1, 2, 6, 6},
{1, 2, 5, 6}, {0, 2, 8, 5}, {1, 2, 6, 5},
{1, 1, 6, 6}, {1, 1, 6, 6}, {1, 1, 6, 6},
{1, 1, 5, 6}, {1, 1, 6, 5}, {0, 1, 8, 6},
{0, 0, 8, 8}, {0, 0, 8, 8}, {0, 0, 8, 8},
{0, 1, 8, 7}, {1, 1, 6, 6}, {0, 1, 8, 6},
{0, 1, 8, 6}, {1, 0, 6, 8}, {1, 1, 6, 6},
{1, 1, 6, 6}, {1, 1, 6, 6}, {1, 0, 6, 7},
};
struct Player {
bool autoShoot = false;
bool GOD_MODE = false;
Vector2 pos = {0};
Vector2 vel = {0};
Vector2 target = {0};
Vector2 movement = {0};
float rotation = 0;
Vector2 prevPos = {0};
Vector2 size = {32.0f, 32.0f};
bool dead = false;
bool won = false;
Vector2 speed = {100.0f, 150.0f};
int hull = 3;
int hullMax = 3;
int hullStrength = 1; // mitigates damage
int shield = 0;
int shieldMax = 0;
float shotSpeed = 160.0f;
float shotReload = 10.0f / 60.0f;
float shotTimer = 0;
bool canShoot = true;
int shotPower = 1;
int shotSpread = 1;
int shotStyle = 0;
float shieldRegenTime = 2.0f;
float shieldRegenTimer = 0;
float shieldRegenAmt = 0.02f;
// animation variables
float shieldFrameTime = 0.25f; // secs between shield frame transitions
float shieldFrameTimer = 0.0f;
int shieldFrames = 4;
int shieldFrame = 0;
bool shieldActivating = false;
bool shieldDeactivating = false;
// player exploding variables
bool exploding = false;
int expFrame = 0;
float expTimer = 0;
// player alert variables
float alertTimer = 0;
float flashTime = 0.5f;
float flashTimer = 0;
double gameTime = 0;
double endTime = 0;
// player time freeze variables
float freezeTime = 1.0f;
float freezeTimer = freezeTime;
bool timeFrozen = false;
int score = 0;
};
struct Enemy {
bool boss;
int type;
int shot_style;
int bullets_per_shot;
bool active;
Vector2 position;
Vector2 direction;
Vector2 size;
float lifetime;
Color tint;
bool player_bullet;
float shot_timer;
int damage;
int hull;
int hull_max;
float shield;
int shield_max;
int hull_strength;
float flash_timer;
bool shield_activating;
bool shield_deactivating;
float shield_regen_amt;
float shield_regen_timer;
float shield_frame_timer;
float shield_frame;
float exploding;
float exp_frame;
float exp_timer;
};
struct Bullet {
bool boss;
int type;
int shot_style;
bool active;
Vector2 position;
Vector2 direction;
Vector2 size;
float lifetime;
Color tint;
bool player_bullet;
float shot_timer;
int damage;
float rotation;
};
struct Powerup {
int type;
bool active;
Vector2 position;
Vector2 direction;
float lifetime;
};
struct Asteroid {
bool active;
float lifetime;
int type;
Vector2 position;
Vector2 direction;
float size;
int durability;
int durability_max;
float rotation_speed;
float rotation;
int metal;
int metal_amount;
float alpha; // for fade out effects
};
struct Popup {
bool visible;
float lifetime;
Vector2 position;
Vector2 size;
Color border_color;
int border_thickness;
float anim_total;
float anim_current;
};
struct Achievement {
std::string title;
std::string desc;
bool achieved;
int logo;
};
Achievement achievement_info[NUM_ACHIEVEMENTS] = {
{"Slayer I", "Slay 100 enemies!", false, 0},
{"Slayer II", "Slay 500 enemies!", false, 0},
{"Slayer III", "Slay 1000 enemies!", false, 0},
{"Winner", "Win the Game!", false, 0},
{"Gun Nut", "Max out a single gun stat!", false, 0},
};
#define MAX_ENEMIES 100
#define MAX_BULLETS 1000
#define MAX_POWERUPS 10
#define MAX_ASTEROIDS 5
#define MAX_FRAGMENTS 100
Vector2 screenSize = {800, 1200};
Enemy Enemies[MAX_ENEMIES];
Bullet Bullets[MAX_BULLETS];
Powerup Powerups[MAX_POWERUPS];
Asteroid Asteroids[MAX_ASTEROIDS];
Asteroid Fragments[MAX_FRAGMENTS];
Player player; // main player structure
Popup achievement;
Popup death = {false, 20.0f, Vector2{screenSize.x * 0.5f, screenSize.y * 0.5f}, Vector2{300, 50}, RED, 1, 0.25f, 0};
Vector2 GetScreenSize(void);
Vector2 GetShotSpeed(int totalShots, int currentShot, float speed, float direction);
int NewEnemy(void);
void UpdatePlayer(void);
void UpdatePowerup(int id);
void UpdateEnemy(int id);
void UpdateBullet(int id);
void UpdateAsteroid(int id);
void UpdateFragment(int id);
void StopAllMusic(void);
void PerformHitPlayer(Bullet &bullet);
void PerformHitEnemy(Bullet &bullet, Enemy &enemy);
void PerformHitAsteroid(Bullet &bullet, Asteroid &hit);
void PerformKillEnemy(Enemy *enemy);
void PerformKillBullet(Bullet *bullet);
void PerformKillAsteroid(Asteroid *asteroid);
void PerformKillPowerup(Powerup *powerup);
void PerformFragmentAsteroid(Asteroid *asteroid);
void ResetEnemy(Enemy *enemy);
void ResetBullet(Bullet *bullet);
void ResetAsteroid(Asteroid *asteroid);
void ResetPowerup(Powerup *powerup);
Rectangle getBulletRec(Bullet &bullet);
Vector2 getAsteroidCenter(const Asteroid& asteroid);
Rectangle getAsteroidRec(Asteroid &asteroid);
Rectangle getPlayerRec(void);
Rectangle getEnemyRec(Enemy &enemy);
Vector2 getEnemyCenter(Enemy &enemy);
float getEnemyShieldRadius(Enemy &enemy);
Vector2 getPlayerCenter(void);
float getPlayerShieldRadius(void);
void DrawBackground(void);
void DrawPlayer(void);
void DrawPlayerTarget(void);
void DrawStats(void);
void DrawHullBar(void);
void DrawShieldBar(void);
void DrawExpBar(void);
void DrawPlayerFreezeTimer(void);
void ProcessPlayerState(void);
void UpdateAndDraw(void);
void DrawIntro(void);
void DrawMainMenu(void);
void DrawSettingsMenu(void);
void DrawShipSetupMenu(void);
void DrawPauseMenu(void);
void DrawDeathScreen(void);
void DrawWinScreen(void);
void UpdateBackground(void);
// // PLAYER VARIABLES
// bool autoShoot = false;
// bool GOD_MODE = false;
// Vector2 playerPos = {0};
// Vector2 playerTarget = {0};
// float playerRotation = 0.0f;
// Vector2 playerPreviousPos = {0};
// Vector2 playerSize = {32.0f, 32.0f};
// bool playerDead = false;
// bool playerWon = false;
//
// float playerSpeed = 150.0f; // pixels per frame
// // start with 3 hull
// int playerHull = 3;
// int playerHullMax = 3;
// int playerHullStrength = 1; // mitigates 1 damage
// // start with no shields
// float playerShield = 0;
// float playerShieldMax = 0;
// int playerShotSpeed = 100;
// float playerShotReload = 10.0f / 60.0f;
// float playerShotTimer = 0;
// bool playerCanShoot = true;
// int playerShotPower = 1;
// int playerShotSpread = 1;
// int playerShotStyle = 0;
//
// float playerShieldRegenTime = 2.0f;
// float playerShieldRegenTimer = 0;
// float playerShieldRegenAmt = 0.02f;
//
// // shield animation VARIABLES
// float playerShieldFrameTime = 0.25f; // sec between shield frame transitions
// float playerShieldFrameTimer = 0.0f;
// int playerShieldFrames = 4;
// int playerShieldFrame = 0;
// bool playerShieldActivating = false;
// bool playerShieldDeactivating = false;
//
// // player exploding variables
// bool playerExploding = false;
// int playerExpFrame = 0;
// float playerExpTimer = 0;
//
// // player alert variables
// float playerAlertTimer = 0;
//
// float playerFlashTime = 0.5f;
// float playerFlashTimer = 0;
// double gameTime = 0;
// double endTime = 0;
//
// // player time freeze variables
// float playerFreezeTime = 1.0f;
// float playerFreezeTimer = playerFreezeTime;
// bool playerTimeFrozen = false;
float AsteroidSpeed = 100.0f;
float EnemyShotSpeed = 200.0f;
float ShotLifetime = 2.0f;
Vector2 EnemySize = {48.0f, 48.0f};
Vector2 BulletSize = {48.0f, 48.0f};
Vector2 BossSize = {96.0f, 96.0f};
float EnemySpeed = 100.0f;
Vector2 PowerupSize = {25.0f, 25.0f};
float PowerupSpeed = 150.0f;
float PowerupLifetime = 2.0f;
float movingBackgroundPosY = 0;
float musicPlayed = 0;
enum game_states {
GAME_STATE_INTRO,
GAME_STATE_MAIN_MENU,
GAME_STATE_SETTINGS,
GAME_STATE_SHIP_SETUP,
GAME_STATE_PLAYING,
GAME_STATE_PAUSED,
GAME_STATE_DEAD,
GAME_STATE_WIN
};
int player_game_state = GAME_STATE_PLAYING;
const int NUM_TEXTURES = 7;
enum texture_names {
SHIPS,
PROJECTILES,
BACKGROUND,
MOVING_BACKGROUND,
UI,
MISC,
UPGRADES
};
const char *texture_paths[NUM_TEXTURES]{
"assets/SpaceShooterAssetPack_Ships.png",
"assets/SpaceShooterAssetPack_Projectiles.png",
"assets/SpaceBackground.png",
"assets/SpaceShooterAssetPack_BackGrounds.png",
"assets/SpaceShooterAssetPack_IU.png",
"assets/SpaceShooterAssetPack_Miscellaneous.png",
"assets/ship_upgrades.png"};
Texture2D textures[NUM_TEXTURES];
void load_textures(void) {
for (int i = 0; i < NUM_TEXTURES; i++) {
textures[i] = LoadTexture(texture_paths[i]);
}
}
void unload_textures(void) {
for (int i = 0; i < NUM_TEXTURES; i++) {
UnloadTexture(textures[i]);
}
}
const int NUM_MUSIC = 7;
enum music_names { DEATH, GAME1, GAME2, GAME3, GAME4, MENU1, WIN };
const char *music_paths[NUM_MUSIC]{
"assets/music_death01.wav", "assets/music_game01.wav",
"assets/music_game02.wav", "assets/music_game03.mp3",
"assets/music_game04.wav", "assets/music_menu01.mp3",
"assets/music_win01.wav"};
Music music[NUM_MUSIC];
Music *nowPlaying;
int numMusicRepeats = 0;
int currentMusicRepeats = 0;
const int NUM_GAME_MUSIC = 4;
int game_music[NUM_GAME_MUSIC] = {GAME1, GAME2, GAME3, GAME4};
const int NUM_SOUNDS = 28;
enum sound_names {
ALARM,
ALERT_LOW_HULL,
ASTEROID_HIT,
ATTACK_HYPER_SHOT,
ATTACK_ROCKET,
BOSS_ATTACK_1,
BOSS_ATTACK_2,
EXPLOSION,
HULL_HIT,
MENU_HOVER,
MENU_SELECT,
NEGATIVE,
NAVIGATION_BUOY,
POWERUP_ATTACK_SPEED,
POWERUP_COLLECT,
POWERUP_DAMAGE_INCREASE,
POWERUP_DROP,
POWERUP_HULL_INCREASE,
POWERUP_HULL_STRENGTH,
POWERUP_HULL_RESTORE,
POWERUP_SHIELD_INCREASE,
POWERUP_SPREAD_INCREASE,
SHIELD_BOUNCE,
SHIELD_DROP,
SHIELD_HIT,
SHIELD_RAISE,
SUCCESS,
WIN_TRUMPETS
};
const char *sound_paths[NUM_SOUNDS]{"assets/sounds/alarm.wav",
"assets/sounds/alert_low_hull.wav",
"assets/sounds/asteroid_hit.ogg",
"assets/sounds/attack_hyper_shot.ogg",
"assets/sounds/attack_rocket.wav",
"assets/sounds/boss_attack_1.wav",
"assets/sounds/boss_attack_2.wav",
"assets/sounds/explosion.wav",
"assets/sounds/hull_hit.ogg",
"assets/sounds/menu_hover.ogg",
"assets/sounds/menu_select.ogg",
"assets/sounds/negative.wav",
"assets/sounds/navigation_buoy.wav",
"assets/sounds/powerup_attack_speed.wav",
"assets/sounds/powerup_collect.wav",
"assets/sounds/powerup_damage_increase.ogg",
"assets/sounds/powerup_drop.ogg",
"assets/sounds/powerup_hull_increase.ogg",
"assets/sounds/powerup_hull_restore.wav",
"assets/sounds/powerup_hull_strength.wav",
"assets/sounds/powerup_shield_increase.ogg",
"assets/sounds/powerup_spread_increase.ogg",
"assets/sounds/shield_bounce.ogg",
"assets/sounds/shield_drop.wav",
"assets/sounds/shield_hit.wav",
"assets/sounds/shield_raise.wav",
"assets/sounds/success.wav",
"assets/sounds/win_trumpets.wav"};
Sound sounds[NUM_SOUNDS];
void load_music(void) {
for (int i = 0; i < NUM_MUSIC; i++) {
music[i] = LoadMusicStream(music_paths[i]);
}
}
void unload_music(void) {
for (int i = 0; i < NUM_MUSIC; i++) {
UnloadMusicStream(music[i]);
}
}
void load_sounds(void) {
for (int i = 0; i < NUM_SOUNDS; i++) {
sounds[i] = LoadSound(sound_paths[i]);
}
}
void unload_sounds(void) {
for (int i = 0; i < NUM_SOUNDS; i++) {
UnloadSound(sounds[i]);
}
}
void PlayBackgroundMusic(void) {
StopAllMusic();
currentMusicRepeats++;
if (!numMusicRepeats || currentMusicRepeats > numMusicRepeats) {
int newTrack = GetRandomValue(0, NUM_GAME_MUSIC - 1);
currentMusicRepeats = 0;
numMusicRepeats = GetRandomValue(1, 3);
nowPlaying = &music[game_music[newTrack]];
nowPlaying->looping = false;
}
PlayMusicStream(*nowPlaying);
musicPlayed = 0.0f;
}
Vector2 GetScreenSize(void) {
int monitor = GetCurrentMonitor();
float minHeight = fminf(screenSize.y, GetMonitorHeight(monitor));
return Vector2{minHeight * 0.66f, minHeight};
// Vector2 out = {fminf(screenSize.x, (float)GetMonitorWidth(monitor)),
// fminf(screenSize.y, (float)GetMonitorHeight(monitor))};
// return out;
}
void ResetPlayer(void) {
player.autoShoot = false;
player.GOD_MODE = false;
player.pos = {0};
player.vel = {0};
player.target = {0};
player.rotation = 0;
player.prevPos = {0};
player.size = {32.0f, 32.0f};
player.dead = false;
player.won = false;
player.speed = {100.0f, 150.0f};
player.hull = 3;
player.hullMax = 3;
player.hullStrength = 1; // mitigates damage
player.shield = 0;
player.shieldMax = 0;
player.shotSpeed = 160.0f;
player.shotReload = 10.0f / 60.0f;
player.shotTimer = 0;
player.canShoot = true;
player.shotPower = 1;
player.shotSpread = 1;
player.shotStyle = 0;
player.shieldRegenTime = 2.0f;
player.shieldRegenTimer = 0;
player.shieldRegenAmt = 0.02f;
// animation variables
player.shieldFrameTime = 0.25f; // secs between shield frame transitions
player.shieldFrameTimer = 0.0f;
player.shieldFrames = 4;
player.shieldFrame = 0;
player.shieldActivating = false;
player.shieldDeactivating = false;
// player exploding variables
player.exploding = false;
player.expFrame = 0;
player.expTimer = 0;
// player alert variables
player.alertTimer = 0;
player.flashTime = 0.5f;
player.flashTimer = 0;
player.gameTime = 0;
player.endTime = 0;
// player time freeze variables
player.freezeTime = 1.0f;
player.freezeTimer = player.freezeTime;
player.timeFrozen = false;
player.score = 0;
player.gameTime = GetTime();
}
void InitGame(void) {
screenSize = GetScreenSize();
SetWindowSize(screenSize.x, screenSize.y);
SetWindowState(FLAG_WINDOW_UNDECORATED);
for (int i = 0; i < NUM_TEXTURES; i++) {
if (!IsTextureReady(textures[i])) {
TraceLog(LOG_ERROR, "Error loading texture %d", i);
}
}
for (int i = 0; i < NUM_MUSIC; i++) {
if (!IsMusicReady(music[i])) {
TraceLog(LOG_ERROR, "Error loading music track %d", i);
}
}
numMusicRepeats = 0;
currentMusicRepeats = 0;
if (IsAudioDeviceReady())
PlayBackgroundMusic();
ResetPlayer();
movingBackgroundPosY = 0;
musicPlayed = 0;
for (int i = 0; i < MAX_ENEMIES; i++) {
Enemies[i].active = false;
}
for (int i = 0; i < MAX_BULLETS; i++) {
Bullets[i].active = false;
}
for (int i = 0; i < MAX_POWERUPS; i++) {
Powerups[i].active = false;
}
for (int i = 0; i < MAX_ASTEROIDS; i++) {
Asteroids[i].active = false;
}
for (int i = 0; i < MAX_FRAGMENTS; i++) {
Fragments[i].active = false;
}
int count = 5; // GetRandomValue(10, MAX_ENTITIES / 10);
for (int i = 0; i < count; i++) {
NewEnemy();
}
player.pos = {screenSize.x / 2.0f, screenSize.y - (100.0f + player.size.y)};
SetMousePosition(player.pos.x, player.pos.y);
}
void DrawShipTexture(int x, int y, Vector2 coords, float rotation, Color hitColor) {
DrawTexturePro(textures[SHIPS], Rectangle{8.0f * x, 8.0f * y, 8, 8},
Rectangle{coords.x+(player.size.x*0.5f), coords.y+(player.size.y*0.5f), player.size.x, player.size.y},
{player.size.x*0.5f, player.size.y*0.5f}, rotation, hitColor);
// DrawRectangleLines(coords.x, coords.y, playerSize.x, playerSize.y, WHITE);
}
void DrawEnemyTexture(int x, int y, Vector2 coords, Color hitColor) {
DrawTexturePro(textures[SHIPS],
Rectangle{8.0f * 4 + (8.0f * x), 8.0f * y, 8.0f, 8.0f},
Rectangle{coords.x, coords.y, EnemySize.x, EnemySize.y},
Vector2Zero(), 0, hitColor);
}
void DrawBossTexture(int x, int y, Vector2 coords, Color hitColor) {
DrawTexturePro( textures[SHIPS],
Rectangle{8.0f * 4 + (16.0f * x), 8.0f * 6 + (16.0f * y), 16.0f, 16.0f},
Rectangle{coords.x, coords.y, BossSize.x, BossSize.y}, Vector2Zero(), 0, hitColor);
// DrawRectangleLines(coords.x, coords.y, BossSize.x, BossSize.y, WHITE);
}
void DrawProjectileTexture(int x, int y, Vector2 coords, Vector2 size,
float rotation, Color tint) {
DrawTexturePro(textures[PROJECTILES],
Rectangle{8.0f * x, 8.0f * y, 8.0f, 8.0f},
Rectangle{coords.x, coords.y, size.x, size.y},
Vector2{size.x * 0.5f, size.y * 0.5f}, rotation, tint);
}
void DrawBossProjectileTexture(int x, int y, Vector2 coords, Vector2 size,
float rotation, Color tint) {
DrawTexturePro(textures[PROJECTILES],
Rectangle{8.0f * x, 8.0f * 5 + (8.0f * y), 8.0f, 8.0f},
Rectangle{coords.x, coords.y, size.x, size.y},
Vector2{size.x * 0.5f, size.y * 0.5f}, rotation, tint);
}
void DrawUpgradeTexture(int x, int y, Vector2 coords, Vector2 size,
Color tint) {
DrawTexturePro(textures[UPGRADES],
Rectangle{ 16.0f * x, 16.0f * y, 16.0f, 16.0f, },
Rectangle{coords.x, coords.y, size.x, size.y},
Vector2Zero(), 0.0f, tint);
}
void DrawMiscTexture(Rectangle source, Rectangle output, Color tint) {
DrawTexturePro(textures[MISC], source, output, Vector2Zero(), 0.0f, tint);
}
void DrawShieldTexture(int frame, Vector2 coords, Vector2 size, float amt,
Color tint) {
DrawTexturePro(
textures[MISC],
Rectangle{(8.0f * 5) + (16.0f * frame), 8.0f * 4, 16.0f, 16.0f},
Rectangle{coords.x - (size.x * 0.25f) - (20 * amt) * 0.5f,
coords.y - (size.y * 0.25f) - (20 * amt) * 0.5f,
(size.x * 1.5f) + (20 * amt), (size.y * 1.5f) + (20 * amt)},
Vector2Zero(), 0.0f, tint);
}
void DrawAsteroidTexture(int type, Vector2 coords, float size, float rotation,
Color tint) {
int x = 1;
int y = 3;
if (type == 2) {
x = 2;
y = 2;
}
DrawTexturePro(textures[MISC],
Rectangle{(8.0f * x), (8.0f * y), (type == 1 ? 8.0f : 16.0f),
(type == 1 ? 8.0f : 16.0f)},
Rectangle{coords.x, coords.y, size, size},
Vector2{size * 0.5f, size * 0.5f}, rotation, tint);
}
int FindEmptyEnemy(Enemy *list) {
for (int i = 0; i < MAX_ENEMIES; i++) {
if (!list[i].active)
return i;
}
return -1;
}
int FindEmptyBullet(Bullet *list) {
for (int i = 0; i < MAX_BULLETS; i++) {
if (!list[i].active)
return i;
}
return -1;
}
int FindEmptyPowerup(Powerup *list) {
for (int i = 0; i < MAX_POWERUPS; i++) {
if (!list[i].active)
return i;
}
return -1;
}
int FindEmptyAsteroid(Asteroid *list) {
for (int i = 0; i < MAX_ASTEROIDS; i++) {
if (!list[i].active)
return i;
}
return -1;
}
int FindEmptyFragment(Asteroid *list) {
for (int i = 0; i < MAX_FRAGMENTS; i++) {
if (!list[i].active)
return i;
}
return -1;
}
Vector2 calculateBulletTrajectory(const Vector2& shipPos, const Vector2& shipVelocity, const Vector2& targetPos, float bulletSpeed) {
// calculate direction from ship to target
float dx = targetPos.x - shipPos.x;
float dy = targetPos.y - shipPos.y;
float distance = std::sqrt(dx * dx + dy * dy);
// handle case where ship is at target position
if (distance == 0) {
return shipVelocity; // just inherit the ship velocity
}
// normalize direction vector
Vector2 normalized = {dx / distance, dy / distance};
// calculate bullet velocity relative to ship
Vector2 bulletVelRelative = Vector2Scale(normalized, bulletSpeed);
// add ship's velocity to get absolute bullet velocity
Vector2 bulletVelAbsolute = Vector2Add(bulletVelRelative, shipVelocity);
return bulletVelAbsolute;
}
int NewPlayerBullet(Vector2 position, int style, float lifetime, Color tint,
bool boss) {
int id = -1;
for (int i = 0; i < player.shotSpread; i++) {
id = FindEmptyBullet(Bullets);
if (id == -1)
return -1; // nope
Bullets[id].active = true;
Bullets[id].boss = boss;
Bullets[id].position = position;
Bullets[id].lifetime = lifetime;
Bullets[id].tint = tint;
Bullets[id].player_bullet = true;
Bullets[id].direction = calculateBulletTrajectory(position, player.vel, player.target, player.shotSpeed);
// using enhanced shot targeting
// GetShotSpeed(playerShotSpread, i, playerShotSpeed, -1.0f);
Bullets[id].damage = player.shotPower;
Bullets[id].type = style;
Bullets[id].size = Vector2{48.0f, 48.0f};
Bullets[id].rotation = player.rotation;
}
return id;
}
int NewEnemyBullet(Enemy &enemy, float lifetime, Color tint) {
int id = -1;
for (int i = 0; i < enemy.bullets_per_shot; i++) {
id = FindEmptyBullet(Bullets);
if (id == -1)
return -1; // nope
Bullets[id].active = true;
Bullets[id].boss = enemy.boss;
Bullets[id].position = Vector2{enemy.position.x + (enemy.size.x * 0.5f),
enemy.position.y + (enemy.size.x * 0.5f)};
Bullets[id].lifetime = lifetime;
Bullets[id].tint = tint;
Bullets[id].player_bullet = false;
Bullets[id].direction =
GetShotSpeed(enemy.bullets_per_shot, i, EnemyShotSpeed, 1.0f);
Bullets[id].damage =
(enemy.boss ? GetRandomValue(5, 10) : GetRandomValue(1, 3));
Bullets[id].type = enemy.shot_style;
Bullets[id].size = Vector2{32.0f, 32.0f};
Bullets[id].rotation = 180.0f;
}
return id;
}
Vector2 GetShotSpeed(int totalShots, int currentShot, float speed,
float direction) {
Vector2 dir = {0};
switch (totalShots) {
case 1:
dir = Vector2{0, 1};
break;
case 2:
switch (currentShot) {
case 0:
dir = Vector2{-0.15f, 1};
break;
case 1:
dir = Vector2{0.15f, 1};
break;
}
break;
case 3:
switch (currentShot) {
case 0:
dir = Vector2{-0.25f, 1};
break;
case 1:
dir = Vector2{0, 1};
break;
case 2:
dir = Vector2{0.25f, 1};
break;
}
break;
case 4:
switch (currentShot) {
case 0:
dir = Vector2{-0.35f, 1};
break;
case 1:
dir = Vector2{-0.15f, 1};
break;
case 2:
dir = Vector2{0.15f, 1};
break;
case 3:
dir = Vector2{0.35f, 1};
break;
}
break;
case 5:
switch (currentShot) {
case 0:
dir = Vector2{-0.45f, 1};
break;
case 1:
dir = Vector2{-0.25f, 1};
break;
case 2:
dir = Vector2{0, 1};
break;
case 3:
dir = Vector2{0.25f, 1};
break;
case 4:
dir = Vector2{0.45f, 1};
break;
}
break;
}
return Vector2Multiply(Vector2Scale(Vector2Normalize(dir), speed), Vector2{1.0f, direction});
}
void UpdatePlayerShield(void) {
if (player.exploding || player.dead || player.won)
return;
if ((player.shield < player.shieldMax)) {
if (player.shieldRegenTimer == 0)
player.shield += player.shieldRegenAmt * GetFrameTime();
else {
player.shieldRegenTimer -= GetFrameTime();
if (player.shieldRegenTimer <= 0) {
player.shieldRegenTimer = 0;
player.shieldActivating = true;
PlaySound(sounds[SHIELD_RAISE]);
}
}
}
if (player.shieldActivating) {
player.shieldFrameTimer += GetFrameTime();
if (player.shieldFrameTimer >= player.shieldFrameTime) {
player.shieldFrameTimer = 0;
player.shieldFrame++;
if (player.shieldFrame >= player.shieldFrames - 1) {
player.shieldFrame = 3;
player.shieldActivating = false;
}
}
} else if (player.shieldDeactivating) {
player.shieldFrameTimer += GetFrameTime();
if (player.shieldFrameTimer >= player.shieldFrameTime) {
player.shieldFrameTimer = 0;