ELEC2645 (2018/19) / Mbed 2 deprecated el17dg

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

Revision:
9:5ad5501c702e
Parent:
8:c18c240665aa
Child:
10:f02413ae09fe
--- a/game/game.cpp	Mon Mar 11 14:48:49 2019 +0000
+++ b/game/game.cpp	Mon Mar 11 22:07:26 2019 +0000
@@ -8,40 +8,35 @@
 #include "main.h"
 #include "game.h"
 #include "enemy.h"
+#include "stars.h"
+#include "blast.h"
 
-//int x_ship_origin = 0;
-//int y_ship_origin = 24;
+#define MAX_BLASTS (5)
 
 int x_ship_pos = 0;
 int y_ship_pos = 24;
 
-int fire_shot_time_counter = 0;
-int shot_step_counter = 0;
-int fire_shot_traveled = 0;
-int start_blast_x_position = 0;
-int start_blast_y_position = 0;
-
 const int ship_speed = 2;
-const int blaster_speed = 2;
-bool is_fire_shot = false;
 
 Enemy enemy;
+Stars stars;
+
+Blast blasts[MAX_BLASTS];
 
 bool Game::updateAndDraw() {
 
     shipMovment();
     enemy.enemyMovement();
+    stars.movingStars1();
+    stars.movingStars2();
     
     if (gamepad.check_event(gamepad.B_PRESSED)){
-        is_fire_shot = true;
-        start_blast_x_position = x_ship_pos + spaceship1_width;
-        start_blast_y_position = y_ship_pos + (spaceship1_height/2);        /////////////////////////////////////////////
+        fireNewBlast();
     }
     
-    if (is_fire_shot){ shipFire(); }    
+    updateAndDrawBlasts();
     
-    lcd.drawSprite(x_ship_pos, y_ship_pos, spaceship1_width, spaceship1_height, (int *)spaceShip1);
-    printf("%i\n", fire_shot_traveled);
+    lcd.drawSpriteOnTop(x_ship_pos, y_ship_pos, spaceship1_width, spaceship1_height, (int *)spaceShip1);
     /*char buffer[4];
     sprintf(buffer,"%i\n",(int)(x_dir.read()*84));
     printf(buffer);*/
@@ -83,33 +78,26 @@
        
 }
 
-
-
-
-void Game::shipFire(){                          //IMPROVE OR SIMPLIFY - Make it into a spearate class so that I could do multiple shots at the same time.
-    
-    if (fire_shot_time_counter == 0){
-        lcd.drawSprite(start_blast_x_position, (start_blast_y_position), 1, 3, (int *)simpleLaserShot);
-              
+void Game::fireNewBlast() {
+    // Search the array of blasts if inactive we can use it.
+    int found_inactive_blast = -1;
+    for (int i = 0; i < MAX_BLASTS; ++i) {
+        if (!blasts[i].isActive()) {
+            found_inactive_blast = i;
+            break;
+        }
     }
-    else if (fire_shot_time_counter == 1){      ///////////////////////////////////////////////////////////////////////////////////////////////////
-        lcd.drawSprite(fire_shot_traveled, (start_blast_y_position), 1, 3, (int *)simpleLaserShotDissapear);
-        
-        shot_step_counter += blaster_speed;
-        fire_shot_traveled = start_blast_x_position + shot_step_counter;
-        
-        lcd.drawSprite(fire_shot_traveled, (start_blast_y_position), 1, 3, (int *)simpleLaserShot);
-        
-        fire_shot_time_counter = 0;      
+    
+    if (found_inactive_blast != -1) {
+        blasts[found_inactive_blast].activate(x_ship_pos + spaceship1_width, y_ship_pos + (spaceship1_height/2));
     }
-    fire_shot_time_counter += 1;
-    
-    if ( fire_shot_traveled > 84){
-        
-        fire_shot_time_counter = 0;
-        shot_step_counter = 0;
-        fire_shot_traveled = 0;
-        
-        is_fire_shot = !is_fire_shot;   
-    } 
 }
+
+void Game::updateAndDrawBlasts(){
+    for (int i = 0; i < MAX_BLASTS; ++i) {
+        if (blasts[i].isActive()) {
+            blasts[i].updateAndDraw();
+        }
+    }
+}
+