ELEC2645 (2018/19) / Mbed 2 deprecated el17dg

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

Revision:
18:6becc9f9de5e
Parent:
17:69a85c909566
Child:
19:b78fa41d04a9
--- a/game/game.cpp	Tue Mar 19 10:41:51 2019 +0000
+++ b/game/game.cpp	Tue Mar 19 12:43:01 2019 +0000
@@ -20,6 +20,9 @@
 
 int player_lifes = 3;
 bool game_over = false;
+const float player_collision_radius = 8;
+const float player_collision_offset_x = 5;
+const float player_collision_offset_y = 7;
 
 struct GameObject {
     int x;
@@ -192,6 +195,23 @@
     }
 }
 
+void collideEnemiesBlastsAndPlayer() {
+    for (int i = 0; i < MAX_ENEMIES; ++i) {
+        for (int j = 0; j < MAX_BLASTS; ++j) {
+            if (enemies[i].active && enemy_blasts[j].active) {
+                bool collision = circleCollideTwoObjects(
+                    x_ship_pos, y_ship_pos, player_collision_radius, player_collision_offset_x, player_collision_offset_y,
+                    enemy_blasts[j].x, enemy_blasts[j].y, blast_collision_radius, blast_collision_offset_x, blast_collision_offset_y
+                );
+                if (collision) {
+                    player_lifes -= 1;
+                    enemy_blasts[j].active = false;
+                }
+            }
+        }
+    } 
+}
+
 void newSmallStarFlies() {
     // Search the array of stars if inactive we can use it. - the same as with blasts
     int found = -1;
@@ -252,7 +272,7 @@
     }
 }
 
-void shipMovment(){           // The position of the ship
+void shipMovment(){           // The position of the player
         
     if(x_ship_pos <= screen_height && x_ship_pos >= 0){
         if(x_dir.read() > 0.6f){
@@ -292,23 +312,25 @@
 
 void playerLivesRemain(){////////////////////////////////////////////////////////////////////////////////
         if (player_lifes == 3){
-            gamepad.leds_on();
+            //turn all LEDs on
+            gamepad.leds_on();  
         }
-        if (player_lifes == 2){
+        else if (player_lifes == 2){
             // only yelow and red are lit (to tal 4)
-            gamepad.led(1,1.0);
-            gamepad.led(2,1.0);
-            gamepad.led(4,1.0);
-            gamepad.led(5,1.0);
+            
+            //gamepad.leds_on();
+            gamepad.led(6,0.0);
+            gamepad.led(3,0.0);
         }
-        if (player_lifes == 1){
+        else if (player_lifes == 1){
             // red LED is lit and flashes.
-            gamepad.led(1,1.0);
-            gamepad.led(4,1.0);
+            gamepad.led(2,0.0);
+            gamepad.led(5,0.0);
             
         }
         else {
             // all LEDs are flashing
+            gamepad.leds_off();
             game_over = true;
         }
     
@@ -322,8 +344,8 @@
     }
     if  (small_star_delay == small_star_delay_max){ 
         //This is dealy between small stars generation.
-        newSmallStarFlies();
-        newMediumStarFlies();
+        //newSmallStarFlies();
+        //newMediumStarFlies();
         spawnNewEnemy();
         
         small_star_delay = 0;
@@ -347,6 +369,7 @@
     updateAndDrawMediumStars();
     updateAndDrawEnemies();
     collideEnemiesAndBlasts();
+    collideEnemiesBlastsAndPlayer();
     updateAndDrawEnemyBlasts();
     highScore();