ELEC2645 (2018/19) / Mbed 2 deprecated el17dg

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

Revision:
33:c623c6d5ed16
Parent:
32:5403bb974294
Child:
34:754915ce9de5
--- a/game/game.cpp	Fri Apr 26 13:09:03 2019 +0000
+++ b/game/game.cpp	Mon Apr 29 08:10:52 2019 +0000
@@ -1,18 +1,21 @@
 #include "mbed.h"
 #include "N5110.h"
 #include "Gamepad.h"
+#include "collision_lib.h"
 
 #include "models.h"
 #include "main.h"
 #include "game.h"
-#include "geometry.h"
 #include "gameobject.h"
 
+#include "boss.h"
 #include "enemies.h"
 #include "constants.h"
 #include "stars.h"
 #include "player.h"
 #include "hud.h"
+#include "gameovermanager.h"
+
 
 const int increase_difficulty = 50;
 int GameGlobals::game_score = 0;
@@ -21,45 +24,104 @@
 int GameGlobals::high_score = 0;
 bool GameGlobals::is_shield_on = false;
 
+Boss boss;
 Enemies enemies;
 Stars stars;
-Player playerShip;
-GameObject gameOverLogo;
-GameObject youDied;
+Player player;
+GameOverManager gameOverManager;
+
 CircleBounds circleBounds;
 Hud hud;
 
+
+void Game::updateAndDrawGameplay() {
+    checkButtonToShoot();
+    player.updateAndDraw();
+    player.updateAndDrawBlasts();
+    stars.updateAndDrawSmallStars();
+    stars.starsSpawnDelay();
+    increaseGameDifficultyAndEnemySpawnDelay();
+    hud.displayLifes();
+    hud.drawScore();
+    collideEnemiesAndBlasts();
+    collideEnemiesBlastsAndPlayer();
+    collideEnemiesAndPlayer();
+    stars.updateAndDrawMediumStars();
+    enemies.updateAndDrawEnemies();
+    enemies.updateAndDrawEnemyBlasts();
+    
+    if (checkForGameOver()) {
+        game_state = GameState_gameover;
+    }
+}
+
+void Game::updateAndDrawGameover() {
+    gameOverManager.updateAndDraw();
+    if (gamepad.check_event(gamepad.Y_PRESSED) && !gameOverManager.isPlayingAnimation()) {
+        gameOverManager.reset();
+        game_state = GameState_newgame;
+    }
+}
+
+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;
+        game_state = GameState_boss_gameplay;
+    }
+}
+
+void Game::updateAndDrawBossGameplay() {
+    checkButtonToShoot();
+    player.updateAndDraw();
+    player.updateAndDrawBlasts();
+    stars.updateAndDrawSmallStars();
+    stars.starsSpawnDelay();
+    hud.displayLifes();
+    hud.drawScore();
+    boss.updateAndDrawBoss();
+    boss.updateAndDrawBossBlasts();
+    collideBossAndPlayerBlasts();
+    if (checkForGameOver()) {
+        game_state = GameState_gameover;
+    }
+}
+
 /**
-    * This is the main function of game.cpp, where the actual gameplay happens.
-    * Here all other functions are activeated, and when the player dies, it
-    * returns back to main menu "main.cpp". 
-    */
+  * This is the main function of game.cpp, where the actual gameplay happens.
+  * Here all other functions are activeated, and when the player dies, it
+  * returns back to main menu "main.cpp". 
+  */
 bool Game::updateAndDraw() {
-    if (game_over) {
-        printf("start game \n");
+    if (game_state == GameState_newgame) {
+        //printf("start game \n");
         startNewGame();
     }
+    if (game_state == GameState_gameplay) { updateAndDrawGameplay(); }
+    else if (game_state == GameState_boss_cutscene) { updateAndDrawBossCutscene(); }
+    else if (game_state == GameState_boss_gameplay) { updateAndDrawBossGameplay(); }
+    else if (game_state == GameState_gameover) { updateAndDrawGameover(); }
+    
+    return checkIfNeedsToReturnToMenu();
+}
+
+void Game::checkButtonToShoot(){
     if (gamepad.check_event(gamepad.X_PRESSED) && !GameGlobals::is_shield_on){
         // Checking the button second time to prevent double blast.
         gamepad.check_event(gamepad.X_PRESSED); 
-        playerShip.fireNewBlast();
+        player.fireNewBlast();
         gamepad.tone(200,0.1);
     }
-    playerShip.playerShipMovement();
-    stars.starsSpawnDelay();
-    increaseGameDifficultyAndEnemySpawnDelay();
-    hud.displayLifes();
-    playerShip.updateAndDrawBlasts();
-    stars.updateAndDrawSmallStars();
-    //stars.updateAndDrawMediumStars();
-    enemies.updateAndDrawEnemies();
-    collideEnemiesAndBlasts();
-    collideEnemiesBlastsAndPlayer();
-    collideEnemiesAndPlayer();
-    enemies.updateAndDrawEnemyBlasts();
-    hud.drawScore();
-    
-    return returnToMenu();
 }
 
 /**
@@ -70,17 +132,17 @@
   */
 void Game::collideEnemiesAndBlasts() {
     for (int i = 0; i < max_enemies; ++i) {
-        for (int j = 0; j < playerShip.max_player_blasts; ++j) {
+        for (int j = 0; j < player.max_player_blasts; ++j) {
             Enemy& enemy = enemies.enemies[i];
-            GameObject& blast = playerShip.blasts[j];
+            GameObject& blast = player.blasts[j];
             if (enemy.active && !enemy.dead && blast.active) {
                 bool collision = circleBounds.circleCollideTwoObjects(
                     enemy.pos, enemies.enemy_bounds, 
-                    blast.pos, playerShip.blast_bounds
+                    blast.pos, player.blast_bounds
                 );
                 if (collision) {
                     enemy.die();
-                    printf("enemy got hit and dies from blast");
+                    //printf("enemy got hit and dies from blast");
                     GameGlobals::game_score += 30;
                     GameGlobals::score_count_for_difficulty +=30;
                     blast.active = false;
@@ -102,14 +164,14 @@
         GameObject& blast = enemies.enemy_blasts[i];
         if (blast.active) {
             bool collision = circleBounds.circleCollideTwoObjects(
-                playerShip.player.pos, playerShip.player_bounds,
+                player.pos, player.player_bounds,
                 blast.pos, enemies.enemy_blast_bounds
             );
             if (collision) {
                 if (!GameGlobals::is_shield_on){
                     gamepad.tone(423,0.4); 
                     GameGlobals::player_lifes -= 1;
-                    printf("lost a life from blast. left: %i \n", GameGlobals::player_lifes);
+                    //printf("lost a life from blast. left: %i \n", GameGlobals::player_lifes);
                     blast.active = false;
                 }else{
                     blast.active = false;
@@ -129,14 +191,32 @@
         Enemy& enemy = enemies.enemies[i];
         if (enemy.active && !enemy.dead) {
             bool collision = circleBounds.circleCollideTwoObjects(
-                playerShip.player.pos, playerShip.player_bounds,
+                player.pos, player.player_bounds,
                 enemy.pos, enemies.enemy_bounds
             );
             if (collision) {
                 GameGlobals::player_lifes -= 1;
-                printf("lost a life from enemy col. left: %i \n", GameGlobals::player_lifes);
+                //printf("lost a life from enemy col. left: %i \n", GameGlobals::player_lifes);
                 enemy.die();
-                printf("enemy got hit from collsion and dies");
+                //printf("enemy got hit from collsion and dies");
+            }
+        }
+    } 
+}
+
+void Game::collideBossAndPlayerBlasts() {
+    for (int i = 0; i < player.max_player_blasts; ++i) {
+        GameObject& blast = player.blasts[i];
+        if (blast.active) {
+            bool collision = circleBounds.circleCollideTwoObjects(
+                boss.pos, boss.boss_bounds,
+                blast.pos, player.blast_bounds
+            );
+            if (collision) {
+                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");
             }
         }
     } 
@@ -148,15 +228,10 @@
     * It does not reset the values when the game is paused.
     */
 void Game::startNewGame() {
-    low_frequency_music_counter = 0;
-    high_frequency_music_counter = 0;
-    gameOverLogo.pos.x = game_area_x - 29; // 0 - the sprite length
-    gameOverLogo.pos.y = game_area_y;
-    youDied.pos.x = game_area_width; 
-    youDied.pos.y = game_area_y;
-    game_over = false;
-    playerShip.player.pos.x = 0; //player was defined in player.h
-    playerShip.player.pos.y = 24;
+    //game_state = GameState_gameplay;
+    game_state = GameState_boss_cutscene;
+    player.pos.x = 0; //player was defined in player.h
+    player.pos.y = 24;
     stars.stars_delay = 0;
     enemy_ship_delay_max = 40;
     enemy_ship_delay_counter = enemy_ship_delay_max;
@@ -170,8 +245,8 @@
     for (int i = 0; i < max_enemies; ++i) {
         enemies.enemies[i].active = false;
     }
-    for (int i = 0; i < playerShip.max_player_blasts; ++i) {
-        playerShip.blasts[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) {
         enemies.enemy_blasts[i].active = false;
@@ -181,26 +256,6 @@
         gamepad.check_event(gamepad.Y_PRESSED);
 }
 
-/**
-    * A game over function that shows the sprites of "game over" and "you died".
-    * Allows to reset the game to play again.
-    */
-void Game::gameOver() {
-    drawGameOver();
-    lcd.normalMode();   
-    char buffer[32];
-    sprintf(buffer,"Your Score %i", GameGlobals::game_score);
-    lcd.printString(buffer,0,3);   
-    wait(1);
-    lcd.printString("Press Y",0,4);
-    lcd.printString("to restart",0,5);
-    lcd.refresh();
-    gamepad.check_event(gamepad.START_PRESSED);
-    while (!gamepad.check_event(gamepad.Y_PRESSED)){/////////////////////////////////
-        musicGameOver();
-        ledsGameOver();
-    }
-}
 
 /**
     * A function tbat delays enemy spawn on low game score.
@@ -244,108 +299,17 @@
     return GameGlobals::player_lifes == 0;
 }
 
-/**
-    * A separate function that draws game over. */
-void Game::drawGameOver(){
-    for (int i = 0; i < 42; i++){
-        if (gamepad.check_event(gamepad.START_PRESSED)){
-            ifGameOverSkipped();
-            break;
-        }
-        musicGameOver();
-        lcd.clear();
-        gameOverLogo.pos.x += 1;
-        youDied.pos.x -= 1;
-        drawSprite(gameOverLogo.pos, game_over_sprite);
-        drawSprite(youDied.pos, you_died_sprite);
-        lcd.refresh();
-        wait(0.1);
-    } 
-}
-
-void Game::ifGameOverSkipped(){
-    lcd.clear();
-    gameOverLogo.pos.x = game_area_x - 29 + 42;
-    youDied.pos.x = game_area_width - 42;
-    drawSprite(gameOverLogo.pos, game_over_sprite);
-    drawSprite(youDied.pos, you_died_sprite);
-    lcd.refresh();  
-}
-
-void Game::ledsGameOver(){
-    gamepad.led(1,(float)led_state);
-    gamepad.led(2,(float)!led_state);
-    gamepad.led(3,(float)led_state);
-    gamepad.led(4,(float)!led_state);
-    gamepad.led(5,(float)led_state);
-    gamepad.led(6,(float)!led_state);
-    wait(0.5);
-    led_state = !led_state;   
-}
-
-void Game::musicGameOver(){
-    lowFrequencyPartMusic();
-    highFrequencyPartMusic();
-    high_frequency_music_counter ++;
-    //low_frequency_music_counter ++;       //comment out this for epic game over beat.
-    printMusicCountersTest();
-}
-
-void Game::lowFrequencyPartMusic(){
-    // Low frequency
-    if (low_frequency_music_counter == 0){ gamepad.tone(60,3);}
-    else if (low_frequency_music_counter == 3){gamepad.tone(90,3);}
-    else if (low_frequency_music_counter == 6){gamepad.tone(60,3);}
-    else if (low_frequency_music_counter == 9){gamepad.tone(80,3);}
-    else if (low_frequency_music_counter == 12){gamepad.tone(70,2);}
-    else if (low_frequency_music_counter == 14){gamepad.tone(60,2);}
-    else if (low_frequency_music_counter == 16){gamepad.tone(70,3);}
-    else if (low_frequency_music_counter == 19){gamepad.tone(50,1);}
-    else if (low_frequency_music_counter == 20){gamepad.tone(40,3);}
-    else if (low_frequency_music_counter== 23){
-        gamepad.tone(60,2);
-        low_frequency_music_counter = 0;
-    } 
-}
-
-void Game::highFrequencyPartMusic(){
-    // High frequency
-    if ( high_frequency_music_counter == 0){ gamepad.tone(300,0.1);}
-    else if (high_frequency_music_counter == 3){gamepad.tone(250,0.1);}
-    else if (high_frequency_music_counter == 6){gamepad.tone(230,0.2);}
-    else if (high_frequency_music_counter == 9){gamepad.tone(250,0.1);}
-    else if ( high_frequency_music_counter == 12){gamepad.tone(250,0.2);}
-    else if ( high_frequency_music_counter == 14){gamepad.tone(220,0.1);}
-    else if ( high_frequency_music_counter == 16){gamepad.tone(210,0.3);}
-    else if ( high_frequency_music_counter == 19){gamepad.tone(200,1);}
-    else if ( high_frequency_music_counter == 21){gamepad.tone(250,1);}
-    else if ( high_frequency_music_counter == 22){
-        gamepad.tone(200,1);
-        high_frequency_music_counter = 0;
-    }
-}
-
 /**  This small statment checks whether the pause button was pressed or not.
   * If it was pressed, then the game will go back to main menu and will save 
   * the current status of the object until the game is continued.
   */
-bool Game::returnToMenu(){
+bool Game::checkIfNeedsToReturnToMenu(){
     bool want_to_pause = false;
-    game_over = checkForGameOver();
-    if (game_over){
-        printf("game over happened\n");
-        gameOver();
-        want_to_pause = true;
-    }
+
     if (gamepad.check_event(gamepad.START_PRESSED)){
-        printf("game paused\n");
+        //printf("game paused\n");
         gamepad.check_event(gamepad.START_PRESSED);
         want_to_pause = true;
     }
     return want_to_pause;
-}
-
-void Game::printMusicCountersTest(){
-    printf("Low frequency counter value:: %i\n", low_frequency_music_counter);
-    printf("high frequency counter value:: %i\n", high_frequency_music_counter);
 }
\ No newline at end of file