deemo1

Dependencies:   mbed

Revision:
3:1db91ad3ab84
Parent:
2:03cd3bb32511
Child:
4:9fa0c5edd1a1
--- a/StarcraftEngine/StarcraftEngine.cpp	Tue May 12 08:01:52 2020 +0000
+++ b/StarcraftEngine/StarcraftEngine.cpp	Tue May 12 15:13:22 2020 +0000
@@ -10,7 +10,7 @@
     
 }
 
-void StarcraftEngine::init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width, int speed)
+void StarcraftEngine::init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width,int Boss_height, int Boss_width, int speed)
 {
      // initialise the game parameters
      _Battleship_height = Battleship_height;
@@ -19,6 +19,8 @@
      _Laser_width = Laser_width;
      _Swarm_height = Swarm_height;
      _Swarm_width = Swarm_width;
+     _Boss_height = Boss_height;
+     _Boss_width = Boss_width;
      _speed = speed;
      
      // x position on screen - WIDTH is defined in N5110.h
@@ -28,6 +30,7 @@
      _Battleship.init(_Battleshipx, _Battleship_height, _Battleship_width);
      _Laser.init(_Laser_height, _Laser_width, _speed);
      _Swarm.init(_Swarm_height, _Swarm_width, _speed);
+     _Boss.init(_Boss_height, _Boss_width, _speed);
      
 }
 
@@ -58,6 +61,8 @@
     _Battleship.draw(lcd);
     // Swarm
     _Swarm.draw(lcd);
+    // Boss
+    _Boss.draw(lcd);
     // Laser
     _Laser.draw(lcd); 
     
@@ -72,9 +77,11 @@
      
      _Battleship.update(_d,_mag);
      _Swarm.update();
+     _Boss.update();
      _Laser.update();
      
      check_Swarm_collisions(pad);
+     check_Boss_collisions(pad);
 }
 
 void StarcraftEngine::check_Swarm_collisions(Gamepad &pad)
@@ -83,7 +90,7 @@
      Vector2D Swarm_pos = _Swarm.get_pos();
      Vector2D Swarm_velocity = _Swarm.get_velocity(); 
 
-     // check if swarm hit or over the battleship
+     // check if swarm over the battleship
      if (
           (Swarm_pos.y >= HEIGHT - 1) 
         )   {                 
@@ -97,14 +104,36 @@
     _Swarm.set_pos(Swarm_pos);
 } 
 
+void StarcraftEngine::check_Boss_collisions(Gamepad &pad)
+{
+     // read current Boss attributes
+     Vector2D Boss_pos = _Boss.get_pos();
+     Vector2D Boss_velocity = _Boss.get_velocity(); 
+
+     // check if Boss over the battleship
+     if (
+          (Boss_pos.y >= HEIGHT - 1) 
+        )   {                 
+         _Battleship.minus_life();
+         pad.tone(800.0,0.1);  //  Audio feedback 
+     }
+     
+     
+     // write new attributes
+    _Boss.set_velocity(Boss_velocity);
+    _Boss.set_pos(Boss_pos);
+} 
+
 void StarcraftEngine::check_goal(Gamepad &pad)    
 {
      Vector2D Swarm_pos = _Swarm.get_pos();
+     Vector2D Boss_pos = _Boss.get_pos();
      Vector2D Battleship_pos = _Battleship.get_pos();  
      Vector2D Laser_pos = _Laser.get_pos();
      
      Vector2D Laser_velocity = _Laser.get_velocity();
      Vector2D Swarm_velocity = _Swarm.get_velocity();
+     Vector2D Boss_velocity = _Boss.get_velocity();
      
      // Player get score
      if ((Laser_pos.x >= Swarm_pos.x)&&
@@ -121,6 +150,21 @@
          pad.tone(800.0,0.25);
          wait(0.1);
      }
+     
+     if ((Laser_pos.x >= Boss_pos.x)&&
+         (Laser_pos.x <= Boss_pos.x + 6)&&
+         (Laser_pos.y <= Boss_pos.y + 4)&&
+         (Laser_pos.y >= Boss_pos.y))
+     {
+         _Battleship.add_score();
+         Laser_pos.x = Battleship_pos.x + 3;    
+         Laser_pos.y = Battleship_pos.y - 1; 
+         Boss_pos.x = rand() % 64;
+         Boss_pos.y = 2;
+         
+         pad.tone(800.0,0.25);
+         wait(0.1);
+     }
       
      if ((Laser_pos.y < 6)||(Battleship_pos.y - Laser_pos.y >= 20))
      {
@@ -130,8 +174,10 @@
      
       _Laser.set_velocity(Laser_velocity);
       _Swarm.set_velocity(Swarm_velocity);
+      _Boss.set_velocity(Boss_velocity);
       _Laser.set_pos(Laser_pos); 
-      _Swarm.set_pos(Swarm_pos);      
+      _Swarm.set_pos(Swarm_pos);    
+      _Boss.set_pos(Boss_pos);  
 }              
        
 void StarcraftEngine::print_scores(N5110 &lcd)