ELEC2645 (2018/19) / Mbed 2 deprecated el17dg

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

Revision:
26:676874c42883
Parent:
24:0570cb4b92d7
Child:
27:f05f4e738ba9
--- a/game/game.cpp	Wed Apr 03 12:46:04 2019 +0000
+++ b/game/game.cpp	Sat Apr 06 20:13:33 2019 +0000
@@ -1,5 +1,3 @@
-
-
 #include "mbed.h"
 #include "N5110.h"
 #include "Gamepad.h"
@@ -14,24 +12,27 @@
 #include "constants.h"
 #include "stars.h"
 #include "player.h"
+#include "hud.h"
 
 bool game_over = true;
-int small_star_delay;
+int stars_delay;
 int game_score;
+int score_count_for_difficulty;
 int player_lifes;
 bool red_led_state;
 int red_led_flashing;
+int enemy_ship_delay_counter;
 
-const int small_star_delay_max = 3;
-
+const int stars_delay_max = 5;
+const int increase_difficulty = 50;
+int enemy_ship_delay_max = 40;
 
 Enemies enemies;
 Stars stars;
 Player playerShip;
 GameObject gameOverLogo;
 GameObject youDied;
-
-#include "hud.h"
+Hud hud;
 
 /**@brief
   * This function checks whether the requirments for the collision of the two objects,
@@ -55,6 +56,7 @@
                     gamepad.tone(123,0.3);
                     enemy.die();
                     game_score += 30;
+                    score_count_for_difficulty +=30;
                     blast.active = false;
                 }
             }
@@ -87,11 +89,11 @@
         }
     } 
 }
+
 /**@brief
   * This code does the same work as the one before but with two other object.
   * of enemy ship and the player's ship
   */
-
 void Game::collideEnemiesAndPlayer() {
     for (int i = 0; i < max_enemy_blasts; ++i) {
         Enemy& enemy = enemies.enemies[i];
@@ -108,9 +110,9 @@
     } 
 }
 
-    /**@brief
-    * This function resets all values to their intial values when the game is
-    * first starts and when the player dies and wants to restart the game.
+/**@brief
+    * This function resets all the values to their intial states when the game is
+    * first began when the player dies and wants to restart the game.
     * It does not reset the values when the game is paused.
     */
 void Game::startNewGame() {
@@ -121,11 +123,16 @@
     game_over = false;
     player.pos.x = 0; //player was defined in player.h
     player.pos.y = 24;
-    small_star_delay = 0;
+    stars_delay = 0;
+    enemy_ship_delay_max = 40;
+    enemy_ship_delay_counter = enemy_ship_delay_max;
     game_score = 0;
+    score_count_for_difficulty = 0;
     player_lifes = 3;
     red_led_state = false;
     red_led_flashing = 0;
+    enemy_blast_speed = 3;
+    enemy_speed = 1;   
     for (int i = 0; i < max_enemies; ++i) {
         enemies.enemies[i].active = false;
     }
@@ -137,7 +144,7 @@
     }
 }
 
-    /**@brief
+/**@brief
     * A game over function that shows the sprites of "game over" and "you died".
     * Allows to reset the game to play again.
     */
@@ -150,7 +157,7 @@
         lcd.refresh();
         wait(0.1);
     }
-    char buffer[4];
+    char buffer[32];
     sprintf(buffer,"Your Score %i",game_score);
     lcd.printString(buffer,0,3);   
     wait(1);
@@ -169,34 +176,62 @@
         led_state = !led_state;
     }
 }
+/**@brief
+    * A separate function for delaying the stars spawn.
+    */
+void Game::starsSpawnDelay(){
+    if  (stars_delay == stars_delay_max){ 
+        //This is dealy between stars generation.
+        //stars.newSmallStarFlies();
+        stars.newMediumStarFlies();
+        stars_delay = 0;
+    }
+    else { stars_delay += 1;}   
+}
 
-    /**@brief
+/**@brief
+    * A function tbat delays enemy spawn on low game score.
+    * It decreases the enemy spawn delay as in game score increases.
+    */
+void Game::increaseGameDifficultyAndEnemySpawnDelay(){
+    if  (enemy_ship_delay_counter <= 0){ 
+        enemies.spawnNewEnemy();
+        enemy_ship_delay_counter = enemy_ship_delay_max;
+    }
+    else { enemy_ship_delay_counter -= 1;}
+    
+    if (enemy_ship_delay_max >= 4 && score_count_for_difficulty >= increase_difficulty){
+        enemy_ship_delay_max -= 3;
+        
+        if (enemy_ship_delay_max <= 20 && enemy_ship_delay_max >= 15){
+            enemy_blast_speed += 1;
+            enemy_speed += 1;   
+        }
+        score_count_for_difficulty = 0;   
+    }
+}
+
+/**@brief
     * 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) {
         startNewGame();
     }
-    playerShip.playerShipMovement();
+    
     if (gamepad.check_event(gamepad.X_PRESSED)){
         playerShip.fireNewBlast();
         gamepad.tone(200,0.1);
+        // The delay is so that two shots wouldn't be fired on one press.
+        wait_ms(0.1);
     }
-    if  (small_star_delay == small_star_delay_max){ 
-        //This is dealy between small stars generation.
-        enemies.spawnNewEnemy();
-        //stars.newSmallStarFlies();
-        //stars.newMediumStarFlies();
-        small_star_delay = 0;
-    }
-    else {
-        small_star_delay += 1;
-    }
-
-    displayLifes();
+    
+    playerShip.playerShipMovement();
+    starsSpawnDelay();
+    increaseGameDifficultyAndEnemySpawnDelay();
+    hud.displayLifes();
     playerShip.updateAndDrawBlasts();
     stars.updateAndDrawSmallStars();
     stars.updateAndDrawMediumStars();
@@ -205,16 +240,13 @@
     collideEnemiesBlastsAndPlayer();
     collideEnemiesAndPlayer();
     enemies.updateAndDrawEnemyBlasts();
-    drawHighScore();
-    
-    lcd.drawSpriteOnTop(player.pos.x, player.pos.y, spaceship1_width, spaceship1_height, (int *)spaceShip1);
-    
+    hud.drawHighScore();
     
     /**@brief
-    * 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.
-    */
+        * 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 want_to_pause = false;
     if (game_over){
         gameOver();
@@ -222,8 +254,6 @@
     }
     if (gamepad.check_event(gamepad.START_PRESSED)){
         want_to_pause = true;
-        
     }
     return want_to_pause;
-}
-
+}
\ No newline at end of file