-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
1262 lines (1151 loc) · 47.5 KB
/
game.cpp
File metadata and controls
1262 lines (1151 loc) · 47.5 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
/*
game.cpp
Classic Invaders
Copyright (c) 2010, Todd Steinackle
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of The No Quarter Arcade (http://www.noquarterarcade.com/) nor the names of
its contributors may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "game.h"
#include "game_defs.h"
#include "game_entity.h"
#include "printf.h"
#include "screen.h"
#include "shields.h"
#include "starfield.h"
#include "system.h"
extern const char* datadir;
//#define FRAME_COUNTER
using Graphics::Screen;
using GameEntities::GameEntityTypeProperties;
using GameEntities::GameEntity;
using GameEntities::Player;
using GameEntities::Alien;
using GameEntities::BonusShip;
using GameEntities::Explosion;
using GameEntities::Shot;
using Game::ShieldPiece;
using Game::ShieldGroupTiles;
#ifdef EVENT_COUNTER
EventCounter event_counter;
#endif
#define PAUSE_COOLDOWN_TIME 100
#define max(a, b) ( ((a)>=(b)) ? (a) : (b) )
#define min(a, b) ( ((b)>=(a)) ? (a) : (b) )
#define STARFIELD_LAYER_INDEX 0
#define STARFIELD2_LAYER_INDEX 1
#define SHIELD_LAYER_INDEX 3
namespace {
// Determines which type of alien is in each row.
const uint8_t kAlienTypesByRow[ALIEN_ARRAY_HEIGHT] = {
GAME_ENTITY_ALIEN3,
GAME_ENTITY_ALIEN2,
GAME_ENTITY_ALIEN2,
GAME_ENTITY_ALIEN,
GAME_ENTITY_ALIEN,
};
// Keep count of per-type index offsets in each row.
uint8_t per_type_index_offsets[ALIEN_ARRAY_HEIGHT] = { 0, 0, 0, 0, 0 };
inline int get_alien_type_by_row(int row) {
return kAlienTypesByRow[row];
}
// Put all the data arrays needed by Game into a separate struct, so the
// amount of memory required can be easily obtained using sizeof().
struct GameData {
Game::ReducedAlien alien_array[NUM_ALIENS];
Alien reference;
uint8_t num_aliens_per_col[ALIEN_ARRAY_WIDTH];
ShieldPiece shield_array[NUM_SHIELDS];
Shot player_shot_array[num_player_shots];
Shot alien_shot_array[MAX_NUM_ALIEN_SHOTS];
Explosion explosion_array[num_explosions];
int direction_array[random_list_len];
int bonus_select_array[random_list_len];
int launch_delay_array[random_list_len];
GameEntity shield_group_array[NUM_SHIELD_GROUPS];
ShieldGroupTiles shield_group_tiles_array[NUM_SHIELD_GROUPS];
};
// Initializes a shield piece entity given its index and state.
void make_shield(const ShieldPiece& piece, GameEntity* object) {
int x = (piece.group * SHIELD_GROUP_X_SPACING) +
SHIELD_X_OFFSET +
(piece.x * SHIELD_PIECE_SIZE);
int y = SHIELD_Y_OFFSET + SHIELD_PIECE_SIZE * piece.y;
object->ShieldPiece_init(0, x, y, piece.intact);
}
fixed increase_speed(fixed speed, fixed32 increase) {
// Multiplying fixed and fixed32 results in 12 bits after the point.
// FIXED32_TO_INT() converts it back into 4-bit post-point precision.
fixed new_speed = FIXED32_TO_INT(speed * increase);
if (new_speed != speed || speed == 0)
return new_speed;
// If the increase was so small that speed was not updated, inc/dec the
// speed by 1 so it is still updated.
if (speed > 0)
return speed + 1;
else
return speed - 1;
}
// Generates a full alien object.
void make_alien(const Game::ReducedAlien& alien, const Alien* reference,
Alien* new_alien) {
uint8_t type = get_alien_type_by_row(alien.row);
uint8_t index = per_type_index_offsets[alien.row] + alien.col;
int x = reference->get_x() + alien.col * ALIEN_STEP_X;
int y = reference->get_y() + alien.row * ALIEN_STEP_Y;
new_alien->Alien_init(type, index, x, y,
alien.is_active(),
alien.get_fire_chance());
if (!alien.is_alive() && new_alien->is_alive()) {
new_alien->kill();
}
new_alien->set_image_num(reference->get_image_num());
}
} // namespace
namespace Game {
void Game::game_control()
{
printf_P("Memory needed for game data: %u bytes\n",
sizeof(GameData) + sizeof(Game));
printf_P("Memory needed for one game entity: %u bytes\n",
sizeof(GameEntity));
printf_P("Memory needed for game entity type properties: %u bytes\n",
sizeof(GameEntityTypeProperties) * NUM_GAME_ENTITY_TYPES);
// Define arrays statically, so they can be freed when the game loop exits.
GameData data;
printf_P("Game data allocated at 0x%x (%u bytes)\n", &data, &data);
// Populate the game data pointers.
aliens = data.alien_array;
reference_alien = &data.reference;
num_aliens_per_col = data.num_aliens_per_col;
shields = data.shield_array;
player_shots = data.player_shot_array;
alien_shots = data.alien_shot_array;
explosions = data.explosion_array;
direction = data.direction_array;
bonus_select = data.bonus_select_array;
launch_delay = data.launch_delay_array;
shield_groups = data.shield_group_array;
shield_group_tiles = data.shield_group_tiles_array;
// Instantiate these here, instead of allocating from heap.
Player player_obj;
BonusShip bonus_obj;
BonusShip small_bonus_obj;
player = &player_obj;
rbonus = bonus = &bonus_obj;
sbonus = &small_bonus_obj;
// init for new game
wave = score = 0;
next_free_guy = free_guy_val;
aliens_landed = false;
current_alien_speed = INT_TO_FIXED(ALIEN_BASE_SPEED);
player_life = 3;
init_wave();
game_loop();
}
void Game::init_wave()
{
// create conditions for next wave
logic_this_loop = wave_over = false;
last_shot = 0;
player_shot_counter = alien_shot_counter = explosion_counter = 0;
factory();
// output wave message and status display
//ui.wave_msg(++wave);
//sound.play_start_wave();
screen.update();
//status.blit_wave(wave);
//status.blit_score(score);
//status.blit_lives(player_life);
//status.blit_player_ships(player_life, images.get_image_index("ship.png"));
}
void Game::init_aliens(int rand_max)
{
// Keep count of both the total number of aliens and the number of each
// type of alien.
alien_count = 0;
uint8_t alien_type_counts[NUM_GAME_ENTITY_TYPES];
memset(alien_type_counts, 0, sizeof(alien_type_counts));
// Create a formation of aliens.
for (int row = 0; row < ALIEN_ARRAY_HEIGHT; ++row) {
int type = get_alien_type_by_row(row);
// For the first alien in each row, store its index relative to
// other aliens of its own class.
per_type_index_offsets[row] = alien_type_counts[type];
for (int col = 0; col < ALIEN_ARRAY_WIDTH; ++col) {
// Populate the alien count for each column. This is redundant
// because it goes through the same initialization
// |ALIEN_ARRAY_HEIGHT| times, but it saves code space by not
// having a separate for loop from 0 to |ALIEN_ARRAY_WIDTH - 1|.
num_aliens_per_col[col] = ALIEN_ARRAY_HEIGHT;
// initialize the bottom row of aliens to fire
bool active = (row == ALIEN_ARRAY_HEIGHT - 1);
int index = alien_type_counts[type]++;
// Instantiate an alien.
Alien temp_alien;
temp_alien.Alien_init(type, index, ALIEN_BASE_X, ALIEN_BASE_Y,
active, rand() % (rand_max + 1));
// Convert it to a reduced alien.
ReducedAlien& alien = aliens[alien_count];
alien.alive = temp_alien.is_alive();
alien.active = temp_alien.is_active();
alien.dirty = temp_alien.is_dirty();
alien.fire_chance = temp_alien.get_fire_chance();
alien.row = row;
alien.col = col;
// If this is the first alien, save it as a reference.
if (alien_count == 0) {
*reference_alien = temp_alien;
}
++alien_count;
}
}
// create alien shots
for (int i = 0; i < num_alien_shots; ++i) {
alien_shots[i].Shot_init(num_player_shots + i, 0, 0, false);
}
}
void Game::factory()
{
// create the player ship and place it in the center of the screen
player->Player_init(player_center, player_top, true);
current_player_speed = 0;
// create bonus ship and small bonus ship
bonus->BonusShip_init(false, 0, 0, false);
sbonus->BonusShip_init(true, 0, 0, false);
current_bonus_speed = 0;
// create the shields
int num_shields = 0;
memset(shields, 0, NUM_SHIELDS * sizeof(shields[0]));
for (int j = 0; j < NUM_SHIELD_GROUPS; ++j) {
for (int k = 0; k < SHIELD_GROUP_HEIGHT; ++k) {
for (int i = 0; i < SHIELD_GROUP_WIDTH; ++i) {
ShieldPiece& shield = shields[num_shields++];
//shields[num_shields++].ShieldPiece_init(x, y, 0, 0, true);
shield.intact = true;
if ((k == 0) && (i == 0 || i == SHIELD_GROUP_WIDTH - 1))
shield.intact = false;
shield.group = j;
shield.x = i;
shield.y = k;
shield_group_tiles[j].update_shield_piece(shield);
}
}
shield_groups[j].init(GAME_ENTITY_SHIELD_GROUP, j,
j * SHIELD_GROUP_X_SPACING + SHIELD_X_OFFSET,
SHIELD_Y_OFFSET, true);
}
for (int type = 0; type < NUM_GAME_ENTITY_TYPES; ++type) {
GameEntities::GameEntityTypeProperties prop;
memset(&prop, 0, sizeof(prop));
switch(type) {
case GAME_ENTITY_PLAYER:
prop.images[0] = 0;
prop.w = PLAYER_WIDTH;
prop.h = PLAYER_HEIGHT;
prop.coll_w = int (PLAYER_WIDTH * 0.9);
prop.coll_h = int (PLAYER_HEIGHT * 0.8);
prop.right_limit = screen_w - PLAYER_WIDTH;
prop.sprite_w = PLAYER_SPRITE_WIDTH;
prop.sprite_h = PLAYER_SPRITE_HEIGHT;
break;
case GAME_ENTITY_ALIEN:
prop.images[0] = 0;
prop.images[1] = 1;
prop.images[2] = 2;
prop.images[3] = 3;
prop.images[4] = 2;
prop.images[5] = 1;
prop.points = 25;
prop.frame_duration = 225;
prop.right_limit = screen_w - ALIEN_WIDTH;
prop.bottom_limit = player_top - ALIEN_HEIGHT / 3;
prop.w = ALIEN_WIDTH;
prop.h = ALIEN_HEIGHT;
prop.coll_w = ALIEN_WIDTH;
prop.coll_h = int (ALIEN_HEIGHT * 0.8);
prop.sprite_w = ALIEN_SPRITE_WIDTH;
prop.sprite_h = ALIEN_SPRITE_HEIGHT;
break;
case GAME_ENTITY_ALIEN2:
prop.images[0] = 0;
prop.images[1] = 1;
prop.images[2] = 2;
prop.images[3] = 3;
prop.images[4] = 2;
prop.images[5] = 1;
prop.points = 50;
prop.frame_duration = 225;
prop.right_limit = screen_w - ALIEN_WIDTH;
prop.bottom_limit = player_top - ALIEN_HEIGHT / 3;
prop.w = ALIEN_WIDTH;
prop.h = ALIEN_HEIGHT;
prop.coll_w = ALIEN_WIDTH;
prop.coll_h = int (ALIEN_HEIGHT * 0.8);
prop.sprite_w = ALIEN_SPRITE_WIDTH;
prop.sprite_h = ALIEN_SPRITE_HEIGHT;
break;
case GAME_ENTITY_ALIEN3:
prop.images[0] = 0;
prop.images[1] = 1;
prop.images[2] = 2;
prop.images[3] = 3;
prop.images[4] = 2;
prop.images[5] = 1;
prop.points = 100;
prop.frame_duration = 225;
prop.right_limit = screen_w - ALIEN_WIDTH;
prop.bottom_limit = player_top - ALIEN_HEIGHT / 3;
prop.w = ALIEN_WIDTH;
prop.h = ALIEN_HEIGHT;
prop.coll_w = int (ALIEN_WIDTH * 0.8);
prop.coll_h = int (ALIEN_HEIGHT * 0.8);
prop.sprite_w = ALIEN_SPRITE_WIDTH;
prop.sprite_h = ALIEN_SPRITE_HEIGHT;
break;
case GAME_ENTITY_BONUS_SHIP:
prop.images[0] = 0;
prop.images[1] = 1;
prop.points = 1000;
prop.frame_duration = 55;
prop.w = BONUS_SHIP_WIDTH;
prop.coll_w = int (BONUS_SHIP_WIDTH * 0.9);
prop.coll_h = prop.h = BONUS_SHIP_HEIGHT;
prop.sprite_w = BONUS_SHIP_SPRITE_WIDTH;
prop.sprite_h = BONUS_SHIP_SPRITE_HEIGHT;
break;
case GAME_ENTITY_SMALL_BONUS_SHIP:
prop.images[0] = 0;
prop.images[1] = 1;
prop.points = 5000;
prop.frame_duration = 55;
prop.w = SMALL_BONUS_SHIP_WIDTH;
prop.coll_w = int (SMALL_BONUS_SHIP_WIDTH * 0.9);
prop.coll_h = prop.h = SMALL_BONUS_SHIP_HEIGHT;
prop.sprite_w = SMALL_BONUS_SHIP_SPRITE_WIDTH;
prop.sprite_h = SMALL_BONUS_SHIP_SPRITE_HEIGHT;
break;
case GAME_ENTITY_SHOT:
prop.images[0] = 0;
prop.coll_w = prop.w = SHOT_WIDTH;
prop.h = SHOT_HEIGHT;
prop.coll_h = int (SHOT_HEIGHT * 0.7);
prop.sprite_w = SHOT_SPRITE_WIDTH;
prop.sprite_h = SHOT_SPRITE_HEIGHT;
break;
case GAME_ENTITY_SHIELD_PIECE:
prop.images[0] = 0;
prop.coll_w = prop.w = SHIELD_PIECE_SIZE;
prop.h = SHIELD_PIECE_SIZE;
prop.coll_h = int (SHIELD_PIECE_SIZE * 0.9);
break;
case GAME_ENTITY_EXPLOSION:
prop.images[0] = 0;
prop.sprite_w = EXPLOSION_SPRITE_SIZE;
prop.sprite_h = EXPLOSION_SPRITE_SIZE;
break;
case GAME_ENTITY_SHIELD_GROUP:
prop.w = prop.coll_w = SHIELD_GROUP_WIDTH * SHIELD_PIECE_SIZE;
prop.h = prop.coll_h = SHIELD_GROUP_HEIGHT * SHIELD_PIECE_SIZE;
break;
default:
continue;
}
prop.coll_x_offset = (prop.w - prop.coll_w) / 2;
prop.coll_y_offset = (prop.h - prop.coll_h) / 2;
GameEntity::set_type_property(type, prop);
}
// Allocate sprites for each type of entity.
int num_objects_per_type[NUM_GAME_ENTITY_TYPES];
memset(num_objects_per_type, 0, sizeof(num_objects_per_type));
num_objects_per_type[GAME_ENTITY_PLAYER] = 1;
num_objects_per_type[GAME_ENTITY_ALIEN] = 2 * ALIEN_ARRAY_WIDTH;
num_objects_per_type[GAME_ENTITY_ALIEN2] = 2 * ALIEN_ARRAY_WIDTH;
num_objects_per_type[GAME_ENTITY_ALIEN3] = ALIEN_ARRAY_WIDTH;
num_objects_per_type[GAME_ENTITY_BONUS_SHIP] = 1;
num_objects_per_type[GAME_ENTITY_SMALL_BONUS_SHIP] = 1;
num_objects_per_type[GAME_ENTITY_SHOT]
= num_player_shots + MAX_NUM_ALIEN_SHOTS;
num_objects_per_type[GAME_ENTITY_SHIELD_PIECE] = 0;
num_objects_per_type[GAME_ENTITY_EXPLOSION] = num_explosions;
screen.allocate_sprites(num_objects_per_type);
// Set up tile layer for drawing shield pieces.
uint16_t shield_data_offset =
screen.get_image_offset(GAME_ENTITY_SHIELD_PIECE);
screen.setup_tile_layer(SHIELD_LAYER_INDEX, true, 0,
shield_data_offset, 0xff);
screen.scroll_tile_layer(SHIELD_LAYER_INDEX, SHIELD_X_OFFSET,
SHIELD_Y_OFFSET);
// Create starfield layers.
generate_starfield(&screen, STARFIELD_LAYER_INDEX, 1,
SCREEN_TILE_SIZE, SCREEN_TILE_SIZE,
NUM_STARFIELD_TILES, STARFIELD_DENSITY / 2, 32, 128);
generate_starfield(&screen, STARFIELD2_LAYER_INDEX, 2,
SCREEN_TILE_SIZE, SCREEN_TILE_SIZE,
NUM_STARFIELD_TILES, STARFIELD_DENSITY, 16, 64);
starfield_y_offset = 0;
screen.scroll_tile_layer(STARFIELD_LAYER_INDEX, 0, 0);
screen.scroll_tile_layer(STARFIELD2_LAYER_INDEX, 0, 0);
// create explosions and player shots
for (int i = 0; i < num_explosions; ++i) {
explosions[i].Explosion_init(i, 0, 0, false);
}
for (int i = 0; i < num_player_shots; ++i) {
player_shots[i].Shot_init(i, 0, 0, false);
}
player_shot_delay = 225;
bonus_launch_delay = base_launch_delay;
int array_select;
// increase difficulty and chance for bonus points as waves progress
if (wave < 6) {
alien_shot_delay = 400 - wave * 25;
num_alien_shots = 16 + wave;
current_alien_speed +=
INT_TO_FIXED(ALIEN_BASE_SPEED) +
wave * INT_TO_FIXED(ALIEN_WAVE_SPEED_INCREASE);
array_select = wave;
} else { // if (wave >= 6)
if (alien_shot_delay > 200) {
alien_shot_delay -= 20;
num_alien_shots += 3;
if (num_alien_shots > MAX_NUM_ALIEN_SHOTS)
num_alien_shots = MAX_NUM_ALIEN_SHOTS;
}
current_alien_speed += INT_TO_FIXED(ALIEN_HIGH_WAVE_SPEED_INCREASE);
array_select = 6;
}
const uint8_t kAlienOddRangeValues[] = {10, 9, 8, 8, 7, 6, 6};
const uint8_t kBonusSelectMax[] = {5, 5, 4, 4, 3, 2, 2};
const uint8_t kLaunchDelayMax[] = {4, 4, 3, 3, 3, 3, 2};
alien_odd_range = kAlienOddRangeValues[array_select];
for (int i = 0; i < random_list_len; ++i) {
direction[i] = (rand() % 3);
bonus_select[i] = (rand() % (kBonusSelectMax[array_select] + 1));
launch_delay[i] = (rand() % (kLaunchDelayMax[array_select] + 1));
}
init_aliens(alien_odd_range);
#ifdef FRAME_COUNTER
printf_P("wave %d\n", wave + 1);
#endif
}
void Game::game_loop()
{
System::KeyState keys;
int reloading = 0;
last_bonus_launch = last_alien_shot = last_loop_time = System::get_ticks();
#ifdef EVENT_COUNTER
event_counter.reset();
#endif
while (1) {
// used to calculate how far the entities should move this loop
// delta is the number of milliseconds the last loop iteration took
// movement is a function of delta
delta = System::get_ticks() - last_loop_time;
last_loop_time = System::get_ticks();
// background track
//sound.play_bg(alien_count);
#ifdef FRAME_COUNTER
// frame counter
uint32_t last_fps_time;
int fps;
last_fps_time += delta;
++fps;
// update fps counter
if (last_fps_time >= 1000) {
printf_P("FPS: %d\n", fps);
last_fps_time = 0;
fps = 0;
}
#endif
// poll input queue
keys = System::get_key_state();
if (keys.quit) {
//sound.halt_all_sounds();
player_dead = true;
player_life = 0;
return;
}
if (keys.pause) {
pause();
}
// player attempt to fire
if (!reloading) {
if (keys.fire) {
fire_shot();
}
}
reloading = keys.fire;
// set player direction based on key input
current_player_speed = 0;
if (keys.left && !keys.right) {
current_player_speed = -player_speed;
}
if (keys.right && !keys.left) {
current_player_speed = player_speed;
}
#ifdef EVENT_COUNTER
event_counter.start_game_logic_section(0);
#endif
// alien behavior
alien_fire();
launch_bonus_ship();
#ifdef EVENT_COUNTER
event_counter.end_game_logic_section(0);
event_counter.start_game_logic_section(1);
#endif
// move everything
if (player->is_active())
player->movement(delta, current_player_speed);
if (bonus->is_active()) {
//sound.play_bonus();
bonus->movement(delta, current_bonus_speed);
} else {
//sound.halt_bonus();
}
// Cast |delta| to a signed value to correctly multiply. Otherwise
// the result is incorrect due to mixing signed and unsigned ints
// with different widths.
fixed alien_movement =
(current_alien_speed * (int32_t)delta) / 1000;
// All the aliens move in tandem so just update the reference alien.
reference_alien->movement(delta, alien_movement);
// Now find an alien on each edge of the formation and use it for
// edge collision detection.
int8_t left_col, right_col;
for (left_col = 0; left_col < ALIEN_ARRAY_WIDTH; ++left_col) {
if (num_aliens_per_col[left_col] > 0)
break;
}
for (right_col = ALIEN_ARRAY_WIDTH - 1; right_col >= 0;
--right_col) {
if (num_aliens_per_col[right_col] > 0)
break;
}
// Move these edge aliens and test edge collision.
uint8_t num_aliens_to_test = (left_col == right_col) ? 1 : 2;
uint8_t alien_cols[] = { left_col, right_col };
for (uint8_t index = 0; index < num_aliens_to_test; ++index) {
uint8_t offset;
for (uint8_t row = 0, offset = 0;
row < ALIEN_ARRAY_HEIGHT;
++row, offset += ALIEN_ARRAY_WIDTH) {
if (aliens[offset + alien_cols[index]].is_alive()) {
Alien alien;
make_alien(aliens[offset + alien_cols[index]],
reference_alien, &alien);
alien.movement(delta, alien_movement);
break;
}
}
}
#ifdef EVENT_COUNTER
event_counter.end_game_logic_section(1);
event_counter.start_game_logic_section(2);
#endif
for (int i = 0; i < num_player_shots; ++i) {
if (player_shots[i].is_active()) {
player_shots[i].movement(delta, shot_speed);
}
}
for (int i = 0; i < num_alien_shots; ++i) {
if (alien_shots[i].is_active()) {
alien_shots[i].movement(delta, alien_shot_speed);
}
}
// explosion duration
for (int i = 0; i < num_explosions; ++i) {
if (explosions[i].is_active()) {
explosions[i].duration(delta, 0);
}
}
// Move starfields.
starfield_y_offset += INT_TO_FIXED(delta * STARFIELD_SPEED) / 1000;
#ifdef EVENT_COUNTER
event_counter.end_game_logic_section(2);
event_counter.start_game_logic_section(3);
#endif
// collision handling
// alien shots with player and shields
for (int i = 0; i < num_alien_shots; ++i) {
Shot* shot = &alien_shots[i];
if (!shot->is_active())
continue;
if (player->collides_with(shot)) {
player->player_shot_collision(shot);
if (!shot->is_active())
continue;
}
uint8_t group;
if (!collides_with_shield_group(shot, &group))
continue;
for (int i = group * NUM_SHIELDS_PER_GROUP;
i < (group + 1) * NUM_SHIELDS_PER_GROUP && i < NUM_SHIELDS;
++i) {
//ShieldPiece* shield = &shields[i];
if (!shields[i].intact)
continue;
GameEntity shield;
make_shield(shields[i], &shield);
if (shield.is_alive() && shield.collides_with(shot)) {
shot->shot_shield_collision(&shield);
shields[i].intact = false;
shield_group_tiles[shields[i].group].
update_shield_piece(shields[i]);
if (!shot->is_active())
break;
}
}
}
#ifdef EVENT_COUNTER
event_counter.end_game_logic_section(3);
event_counter.start_game_logic_section(4);
#endif
// shots with shots
for (int j = 0; j < num_player_shots; ++j) {
Shot* shot = &player_shots[j];
if (!shot->is_active())
continue;
for (int i = 0; i < num_alien_shots; ++i) {
Shot* alien_shot = &alien_shots[i];
if (!alien_shot->is_active())
continue;
if (shot->collides_with(alien_shot)) {
//sound.play_shot_collision();
shot->shot_shot_collision(alien_shot);
if (!shot->is_active())
break;
}
}
}
#ifdef EVENT_COUNTER
event_counter.end_game_logic_section(4);
event_counter.start_game_logic_section(5);
#endif
// player shots with aliens, bonus, and shields
for (int j = 0; j < num_player_shots; ++j) {
Shot* shot = &player_shots[j];
if (!shot->is_active())
continue;
if (bonus->is_active() && shot->collides_with(bonus)) {
bonus->bonus_shot_collision(shot);
if (!shot->is_active())
break;
}
if (!shot->is_active())
continue;
// TODO: Should be able to check only nearby aliens using array.
for (int i = 0; i < NUM_ALIENS; ++i) {
if (!aliens[i].is_alive())
continue;
// Construct full alien from reduced alien for collision
// testing.
Alien temp_alien;
make_alien(aliens[i], reference_alien, &temp_alien);
if (shot->collides_with(&temp_alien)) {
shot->shot_alien_collision(&temp_alien);
// Update the reduced alien.
aliens[i].active = temp_alien.is_active();
aliens[i].dirty = temp_alien.is_dirty();
if (!temp_alien.is_alive()) {
aliens[i].alive = false;
--num_aliens_per_col[aliens[i].col];
}
if (!shot->is_active())
break;
}
}
if (!shot->is_active())
continue;
uint8_t group;
if (!collides_with_shield_group(shot, &group))
continue;
for (int i = group * NUM_SHIELDS_PER_GROUP;
i < (group + 1) * NUM_SHIELDS_PER_GROUP && i < NUM_SHIELDS;
++i) {
if (!shields[i].intact)
continue;
GameEntity shield;
make_shield(shields[i], &shield);
if (shield.is_alive() && shield.collides_with(shot)) {
shot->shot_shield_collision(&shield);
shields[i].intact = false;
shield_group_tiles[shields[i].group].
update_shield_piece(shields[i]);
if (!shot->is_active())
break;
}
}
}
#ifdef EVENT_COUNTER
event_counter.end_game_logic_section(5);
event_counter.start_game_logic_section(6);
#endif
// Aliens with shields.
for (int k = 0; k < ALIEN_ARRAY_HEIGHT; ++k) {
uint8_t alien_index = k * ALIEN_ARRAY_WIDTH;
for (int j = 0; j < ALIEN_ARRAY_WIDTH; ++j, ++alien_index) {
if (!aliens[alien_index].is_alive())
continue;
Alien temp_alien;
make_alien(aliens[alien_index], reference_alien,
&temp_alien);
Alien* alien = &temp_alien;
// If the alien is higher than the shield groups, so are all
// other aliens in the row, so skip the rest of the row.
if (alien->get_y() + alien->get_h() < SHIELD_Y_OFFSET)
break;
uint8_t group;
if (!collides_with_shield_group(alien, &group))
continue;
// Compute the alien collision edges, relative to the upper
// left corner of the shield group.
int alien_left = alien->get_x() + alien->coll_x_offset() -
shield_groups[group].get_x();
int alien_right = alien_left + alien->coll_w() - 1;
int alien_top = alien->get_y() + alien->coll_y_offset() -
shield_groups[group].get_y();
int alien_bottom = alien_top + alien->coll_h() - 1;
// Now compute the range of shield pieces within the group
// that are touching the alien.
int shield_left = max(alien_left / SHIELD_PIECE_SIZE, 0);
int shield_right = min(alien_right / SHIELD_PIECE_SIZE,
SHIELD_GROUP_WIDTH - 1);
int shield_top = max(alien_top / SHIELD_PIECE_SIZE, 0);
int shield_bottom = min(alien_bottom / SHIELD_PIECE_SIZE,
SHIELD_GROUP_HEIGHT - 1);
uint8_t offset = group * NUM_SHIELDS_PER_GROUP +
shield_top * SHIELD_GROUP_WIDTH;
for (int y = shield_top; y <= shield_bottom;
++y, offset += SHIELD_GROUP_WIDTH) {
for (int x = shield_left; x <= shield_right; ++x) {
if (!shields[offset + x].intact)
continue;
// Break all pieces that are still intact. The
// alien survives.
shields[offset + x].intact = false;
shield_group_tiles[group].
update_shield_piece(shields[offset + x]);
}
}
}
}
// Aliens with player.
for (int k = 0; k < ALIEN_ARRAY_HEIGHT && player->is_active(); ++k) {
uint8_t alien_index = k * ALIEN_ARRAY_WIDTH;
for (int j = 0; j < ALIEN_ARRAY_WIDTH; ++j, ++alien_index) {
ReducedAlien& reduced_alien = aliens[alien_index];
if (!reduced_alien.is_alive())
continue;
Alien alien;
make_alien(reduced_alien, reference_alien, &alien);
// If the alien doesn't overlap with the player along the
// vertical axis, skip the entire row of aliens.
if (alien.get_y() + alien.get_h() < player->get_y() ||
alien.get_y() > player->get_y() + player->get_w()) {
break;
}
if (player->collides_with(&alien)) {
player->player_alien_collision(&alien);
if (!player->is_alive())
break;
if (!alien.is_alive()) {
reduced_alien.alive = false;
--num_aliens_per_col[reduced_alien.col];
}
reduced_alien.dirty = alien.is_dirty();
}
}
}
#ifdef EVENT_COUNTER
event_counter.end_game_logic_section(6);
#endif
// the conditions to break out of the game loop
// the order of these matters
if (aliens_landed) {
//sound.halt_bonus();
//sound.halt_all_bg();
//sound.wait_for_all_to_finish();
//sound.play_aliens_landed();
//ui.screen_msg("Game Over");
//sound.play_game_over();
//ui.check_high_scores(score, wave);
return;
}
// conditions for player death by alien shot or collision
if (player_dead && no_explosions_active()) {
--player_life;
//status.blit_lives(player_life);
//sound.halt_bonus();
//sound.halt_all_bg();
if (!player_life) {
//sound.halt_all_bg();
//sound.play_player_dead();
//sound.wait_for_all_to_finish();
//ui.screen_msg("Game Over");
//sound.play_game_over();
//ui.check_high_scores(score, wave);
return;
}
dead_pause = System::get_ticks();
//sound.play_player_dead();
//status.erase_player_ship(player_life, images.get_image_index("ship.png"));
player_rebirth();
//sound.play_bonus();
//sound.play_bg(alien_count);
last_loop_time += System::get_ticks() - dead_pause;
last_bonus_launch = last_alien_shot = System::get_ticks();
// erase player and alien shots to give player a chance to continue
for (int i = 0; i < num_alien_shots; ++i) {
alien_shots[i].deactivate();
}
for (int i = 0; i < num_player_shots; ++i) {
player_shots[i].deactivate();
}
player_dead = false;
}
// conditions for end of wave
//if (wave_over) sound.halt_all_bg();
if (wave_over && !bonus->is_active()) {
//sound.halt_bonus();
bonus_launch_delay = 100000;
player_shot_delay = 100000;
// wait for explosions and shots to finish
if (no_explosions_active() && no_player_shots_active() && no_alien_shots_active()) {
wave_cleanup();
//sound.wait_for_all_to_finish();
//sound.play_end_wave();
return;
}
}
// run alien logic if neccessary
if (logic_this_loop) {
current_alien_speed = -current_alien_speed;
reference_alien->do_alien_logic();
logic_this_loop = false;
}
// draw everything
screen.begin_update();
#ifdef EVENT_COUNTER
event_counter.start_game_logic_section(7);
#endif
if (player->is_dirty())
player->draw();
if (bonus->is_dirty())
bonus->draw();
for (int i = 0; i < NUM_ALIENS; ++i) {
if (!aliens[i].is_dirty())
continue;
Alien alien;
make_alien(aliens[i], reference_alien, &alien);
alien.draw();
}
for (int i = 0; i < num_player_shots; ++i) {
if (player_shots[i].is_dirty()) {
player_shots[i].draw();
}
}
for (int i = 0; i < num_alien_shots; ++i) {
if (alien_shots[i].is_dirty()) {
alien_shots[i].draw();
}
}
for (int i = 0; i < num_explosions; ++i) {
if (explosions[i].is_dirty()) {
explosions[i].draw();
}
}
for (int i = 0; i < NUM_SHIELD_GROUPS; ++i) {
uint8_t x = i * SHIELD_GROUP_X_SPACING / SCREEN_TILE_SIZE;
shield_group_tiles[i].draw(&screen, SHIELD_LAYER_INDEX, x,
0);
}
// Scroll starfields. One scrolls slower than the other, for a neat
// parallax effect.
screen.scroll_tile_layer(STARFIELD_LAYER_INDEX, 0,
FIXED_TO_INT(starfield_y_offset));
screen.scroll_tile_layer(STARFIELD2_LAYER_INDEX, 0,
FIXED_TO_INT(starfield_y_offset) * 3 / 4);
#ifdef EVENT_COUNTER
event_counter.end_game_logic_section(7);
#endif
screen.update();
#ifdef EVENT_COUNTER
event_counter.new_loop();
#endif
}
}
void Game::player_rebirth()
{