ELEC2645 (2018/19) / Mbed 2 deprecated el17dg

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

Revision:
34:754915ce9de5
Parent:
33:c623c6d5ed16
Child:
35:172db1608332
--- a/game/game.cpp	Mon Apr 29 08:10:52 2019 +0000
+++ b/game/game.cpp	Tue Apr 30 19:24:41 2019 +0000
@@ -17,15 +17,17 @@
 #include "gameovermanager.h"
 
 
-const int increase_difficulty = 50;
+const int increase_difficulty = 70;
 int GameGlobals::game_score = 0;
 int GameGlobals::score_count_for_difficulty = 0;
 int GameGlobals::player_lifes = 3;
 int GameGlobals::high_score = 0;
 bool GameGlobals::is_shield_on = false;
+int GameGlobals::score_count_for_boss_mode = 0;
 
 Boss boss;
 Enemies enemies;
+Enemy enemy;
 Stars stars;
 Player player;
 GameOverManager gameOverManager;
@@ -39,6 +41,7 @@
     player.updateAndDraw();
     player.updateAndDrawBlasts();
     stars.updateAndDrawSmallStars();
+    //stars.updateAndDrawMediumStars();
     stars.starsSpawnDelay();
     increaseGameDifficultyAndEnemySpawnDelay();
     hud.displayLifes();
@@ -46,13 +49,12 @@
     collideEnemiesAndBlasts();
     collideEnemiesBlastsAndPlayer();
     collideEnemiesAndPlayer();
-    stars.updateAndDrawMediumStars();
     enemies.updateAndDrawEnemies();
     enemies.updateAndDrawEnemyBlasts();
+    boss.updateAndDrawBossBlasts();
     
-    if (checkForGameOver()) {
-        game_state = GameState_gameover;
-    }
+    if (checkForGameOver()) { game_state = GameState_gameover;}
+    if (is_boss_active) { game_state = GameState_boss_cutscene;}
 }
 
 void Game::updateAndDrawGameover() {
@@ -64,37 +66,33 @@
 }
 
 void Game::updateAndDrawBossCutscene() {
-    static int animation_counter = 0; // TODO change to be like gameover cutscene
-    if (animation_counter == 0) {
-        boss.pos.y = screen_height/2 - (enemy1_height/2);
-        boss.pos.x = screen_width;
-    }
-
-    if (animation_counter < 20) {
-        animation_counter++;
-        boss.pos.x -= 1;
-        boss.draw();
-        player.draw();
-    } else {
-        animation_counter = 0;
+    boss.updateCutscene();
+    enemies.updateAndDrawEnemyBlasts();
+    boss.draw();
+    player.draw();
+    stars.updateAndDrawSmallStars();
+    stars.updateAndDrawMediumStars();
+    if (boss.isFinishedCutscene()) {
         game_state = GameState_boss_gameplay;
+        boss.resetCutscene(); 
     }
 }
 
 void Game::updateAndDrawBossGameplay() {
     checkButtonToShoot();
+    enemies.updateAndDrawEnemyBlasts();
     player.updateAndDraw();
     player.updateAndDrawBlasts();
     stars.updateAndDrawSmallStars();
     stars.starsSpawnDelay();
     hud.displayLifes();
     hud.drawScore();
-    boss.updateAndDrawBoss();
     boss.updateAndDrawBossBlasts();
     collideBossAndPlayerBlasts();
-    if (checkForGameOver()) {
-        game_state = GameState_gameover;
-    }
+    collideBossBlastsAndPlayer();
+    is_boss_active = boss.updateAndDrawBoss();
+    if (checkForGameOver()) { game_state = GameState_gameover;}
+    if (!is_boss_active) { game_state = GameState_gameplay;}
 }
 
 /**
@@ -131,7 +129,7 @@
   * becomes inactive, in game score increases and enemy dies.
   */
 void Game::collideEnemiesAndBlasts() {
-    for (int i = 0; i < max_enemies; ++i) {
+    for (int i = 0; i < enemies.max_enemies; ++i) {
         for (int j = 0; j < player.max_player_blasts; ++j) {
             Enemy& enemy = enemies.enemies[i];
             GameObject& blast = player.blasts[j];
@@ -145,6 +143,7 @@
                     //printf("enemy got hit and dies from blast");
                     GameGlobals::game_score += 30;
                     GameGlobals::score_count_for_difficulty +=30;
+                    GameGlobals::score_count_for_boss_mode += 30;
                     blast.active = false;
                 }
             }
@@ -160,7 +159,7 @@
   * becomes inactive, in game score increases and enemy dies.
   */
 void Game::collideEnemiesBlastsAndPlayer() {
-    for (int i = 0; i < max_enemies; ++i) {
+    for (int i = 0; i < enemies.max_enemies; ++i) {
         GameObject& blast = enemies.enemy_blasts[i];
         if (blast.active) {
             bool collision = circleBounds.circleCollideTwoObjects(
@@ -187,7 +186,7 @@
   * of enemy ship and the player's ship
   */
 void Game::collideEnemiesAndPlayer() {
-    for (int i = 0; i < max_enemies; ++i) {
+    for (int i = 0; i < enemies.max_enemies; ++i) {
         Enemy& enemy = enemies.enemies[i];
         if (enemy.active && !enemy.dead) {
             bool collision = circleBounds.circleCollideTwoObjects(
@@ -213,10 +212,43 @@
                 blast.pos, player.blast_bounds
             );
             if (collision) {
+                boss.boss_lives -= 1;
                 gamepad.tone(123,0.4);
                 blast.active = false;
-                //printf("lost a life from enemy col. left: %i \n", GameGlobals::player_lifes);
-                //printf("enemy got hit from collsion and dies");
+                #ifdef DEBUGel17dg
+                printf("boss has. left: %i \n", boss.boss_lives);
+                #endif 
+            }
+        }
+    } 
+}
+
+void Game::collideBossBlastsAndPlayer() {
+    for (int i = 0; i < boss.max_boss_blasts; ++i) {
+        GameObject& blast = boss.boss_blasts[i];
+        if (blast.active) {
+            bool collision = circleBounds.circleCollideTwoObjects(
+                player.pos, player.player_bounds,
+                blast.pos, boss.boss_blast_bounds
+            );
+            #ifdef DEBUGel17dg
+            printf("player pos:%i,%i; offset:%i,%i; radius:%f.3; \n", player.pos.x, player.pos.y, player.player_bounds.center.x, player.player_bounds.center.y, player.player_bounds.radius);
+            printf("blas pos:%i,%i; offset:%i,%i; radius:%f.3; \n", blast.pos.x, blast.pos.y, boss.boss_blast_bounds.center.x, boss.boss_blast_bounds.center.y, boss.boss_blast_bounds.radius);
+            #endif 
+            if (collision) {
+                if (!GameGlobals::is_shield_on){
+                    
+                    gamepad.tone(423,0.4); 
+                    GameGlobals::player_lifes -= 1;
+                    blast.active = false;
+                    #ifdef DEBUGel17dg
+                    printf("collision happened;\n");
+                    printf("lost a life from blast. left: %i \n", GameGlobals::player_lifes);
+                    #endif
+                }else{
+                    blast.active = false;
+                    gamepad.tone(700,0.6);
+                }
             }
         }
     } 
@@ -228,8 +260,8 @@
     * It does not reset the values when the game is paused.
     */
 void Game::startNewGame() {
-    //game_state = GameState_gameplay;
-    game_state = GameState_boss_cutscene;
+    is_boss_active = false;
+    game_state = GameState_gameplay;
     player.pos.x = 0; //player was defined in player.h
     player.pos.y = 24;
     stars.stars_delay = 0;
@@ -239,24 +271,28 @@
     GameGlobals::game_score = 0;
     GameGlobals::score_count_for_difficulty = 0;
     GameGlobals::player_lifes = 3;
+    GameGlobals::score_count_for_boss_mode = 0;
     hud.resetRedLed();
-    enemy_blast_speed = 3;
-    enemy_speed = 1;   
-    for (int i = 0; i < max_enemies; ++i) {
+    enemies.enemy_blast_speed = 3;
+    enemy.enemy_speed = 1;
+    #ifdef DEBUGel17dg
+    printf("startNewGame\n");
+    printf("boss lives set to: %i \n", boss.boss_lives); 
+    #endif 
+    for (int i = 0; i < enemies.max_enemies; ++i) {
         enemies.enemies[i].active = false;
     }
     for (int i = 0; i < player.max_player_blasts; ++i) {
         player.blasts[i].active = false;
     }
-    for (int i = 0; i < max_enemy_blasts; ++i) {
+    for (int i = 0; i < enemies.max_enemy_blasts; ++i) {
         enemies.enemy_blasts[i].active = false;
     }
-        //Reset start and Y button event to avoid errors
-        gamepad.check_event(gamepad.START_PRESSED);
-        gamepad.check_event(gamepad.Y_PRESSED);
+     for (int i = 0; i < boss.max_boss_blasts; ++i) {
+        boss.boss_blasts[i].active = false;
+    }
 }
 
-
 /**
     * A function tbat delays enemy spawn on low game score.
     * It decreases the enemy spawn delay as in game score increases.
@@ -274,12 +310,13 @@
         //decrease enemy delay spawn.
         enemy_ship_delay_max -= 3;
         if (enemy_ship_delay_max <= 20 && enemy_ship_delay_max >= 15){
-            enemy_blast_speed += 1;
-            enemy_speed += 1;   
+            enemies.enemy_blast_speed += 1;
+            enemy.enemy_speed += 1;   
         }
         GameGlobals::score_count_for_difficulty = 0;   
     }
-    if (GameGlobals::game_score >= 500){ 
+    if (GameGlobals::score_count_for_boss_mode >= 400){
+        is_boss_active = true; 
         lcd.inverseMode();
     }
 }
@@ -296,7 +333,7 @@
     * game is over. 
     */
 bool Game::checkForGameOver() {
-    return GameGlobals::player_lifes == 0;
+    return GameGlobals::player_lifes <= 0;
 }
 
 /**  This small statment checks whether the pause button was pressed or not.
@@ -307,7 +344,9 @@
     bool want_to_pause = false;
 
     if (gamepad.check_event(gamepad.START_PRESSED)){
-        //printf("game paused\n");
+        #ifdef DEBUGel17dg
+        printf("game paused\n");
+        #endif
         gamepad.check_event(gamepad.START_PRESSED);
         want_to_pause = true;
     }