Library containing the Game Engine

Revision:
11:832eb031310b
Parent:
10:df8ea4e747e2
Child:
12:6eeb06ed7c6b
--- a/GameEngine.cpp	Wed May 03 15:51:10 2017 +0000
+++ b/GameEngine.cpp	Thu May 04 08:34:38 2017 +0000
@@ -12,8 +12,8 @@
 
 void GameEngine::init()
 {
-    int HP = 8;
-    _hb.init(HP);
+    int HP = 8; //Health value is chosen here.
+    _hb.init(HP); //HP value is then passed to the HealthBar library.
     
 }
 
@@ -21,36 +21,56 @@
 
 void GameEngine::read_input(Gamepad &pad)
 {
-    _d = pad.get_direction();
-    _mag = pad.get_mag();
+    _d = pad.get_direction(); //Obtains numeric data from hardware.
+    _mag = pad.get_mag(); //Obtains numeric data from hardware.
 }
 
 void GameEngine::draw(N5110 &lcd, Gamepad &pad)
 {
-    _p.draw(lcd);
+    _p.draw(lcd); //Draws the Player Sprite.
     /*while ( pad.check_event(Gamepad::A_PRESSED) == true) {
         _proj.draw(lcd);
     }*/
-    _proj.draw(lcd);
-    _t.draw(lcd);
-    _tt.draw(lcd);
-    _ttt.draw(lcd);
-    _hb.draw(lcd);
+    
+    //PROJ SPAWN, IF N = N +1, PROJ END, N-1
+    
+    _proj.draw(lcd); //Draws the Projectile.
+    _t.draw(lcd); //Draws the first target.
+    _tt.draw(lcd); //Draws the second target.
+    _ttt.draw(lcd); //Draws the third target.
+    _hb.draw(lcd); //Draws the health bar.
+    
+    if(_hb.ZeroHP == 1){ //Checks if health bar is empty. //Working
+        
+        lcd.clear(); 
+        lcd.printString("   You Lose    ",0,1);  
+        lcd.printString("   Press A  ",0,4);
+        lcd.refresh();
+        
+        while(1){
+            if(pad.check_event(Gamepad::A_PRESSED)){
+                NVIC_SystemReset(); //Software Reset.
+                }
+
+        }
+    }
+        
+        
     
 }
 
 
-void GameEngine::update(Gamepad &pad)
+void GameEngine::update(Gamepad &pad) //Updates objects on screen.
 {
     _p.update(_d,_mag);
     _proj.update(); 
     _t.update();
     _tt.update();
     _ttt.update();
-    CheckProjTargetCollision(pad);
-    CheckPlayerTargetCollision(pad);
-    CheckTargetFloorCollision(pad);
-    _hb.update();
+    CheckProjTargetCollision(pad); //Function checks for when a projectile collides with target.
+    CheckPlayerTargetCollision(pad); //Function checks for when the player collides with target.
+    CheckTargetFloorCollision(pad); //Function checks for when a target collides with the floor or bottom of the screen.
+    _hb.update(); //Updates HP bar.
     
 }
 
@@ -67,7 +87,7 @@
     playerx = player_pos.x;
     playery = player_pos.y;
     //printf("playerxy in GAME = %d %d \n", playerx, playery);
-    _proj.init(playerx,playery);
+    _proj.init(playerx,playery); //Sends the Player position to the projectile library
     
     }
     
@@ -78,14 +98,18 @@
 
 void GameEngine::CheckTargetFloorCollision(Gamepad &pad)
 {   
-    Vector2D t_pos = _t.get_pos();
-    Vector2D tt_pos = _tt.get_pos();
-    Vector2D ttt_pos = _ttt.get_pos();
+    Vector2D t_pos = _t.get_pos(); //Obtains the first target position.
+    Vector2D tt_pos = _tt.get_pos(); //Obtains the second target position.
+    Vector2D ttt_pos = _ttt.get_pos(); //Obtains the third target position.
     
     //printf("%f t_pos \n", t_pos.y);
     //printf("%f tt_pos \n", tt_pos.y);
     //printf("%f ttt_pos \n", ttt_pos.y);
     
+    
+    //If statements check for when the position of targets are equal to the
+    //bottom of the screen, if so then HP is deducted using the HPLost2 variable.
+    
     if(t_pos.y == 47){
         printf("t floor \n");
         HPLost2 = HPLost2 - 1;
@@ -99,9 +123,9 @@
         HPLost2 = HPLost2 - 1;
         }
         
-        printf("%d HP \n", HPLost2);
+        //printf("%d HP \n", HPLost2);
         
-        _hb.MinusHP2(HPLost2);
+        _hb.MinusHP2(HPLost2); //Variable is sent to the HealthBar library.
 
     }
 /////////////////////////////////////////////
@@ -115,7 +139,7 @@
     
 void GameEngine::CheckProjTargetCollision(Gamepad &pad)
 {
-    
+    //Obtains all needed coordinates.
     Vector2D proj_pos = _proj.get_pos();
     Vector2D t_pos = _t.get_pos();
     Vector2D tt_pos = _tt.get_pos();
@@ -126,6 +150,10 @@
    // printf("proj %f %f \n", proj_pos.x, proj_pos.y);
     //printf("targ %f %f \n", t_pos.x, t_pos.y);
     
+    
+    //If states check if the projectile has collided with any of the three
+    //targets, if so the target and the projectile's location is reset.
+    
     if (
         ((proj_pos.y == t_pos.y) ||
         (proj_pos.y == t_pos.y + 1) ||
@@ -284,7 +312,7 @@
     
 void GameEngine::CheckPlayerTargetCollision(Gamepad &pad)
     {
-        
+        //Obtains all required coordinates.
         
         
         Vector2D t_pos = _t.get_pos();
@@ -292,6 +320,9 @@
         Vector2D ttt_pos = _ttt.get_pos();
         Vector2D player_pos = _p.get_pos();
         
+        //If statements check if the player sprite has collided with any
+        //of the three targets, if so then the target location is reset and
+        //health is deducted using the HPLost1 variable.
          
     if (
         ((player_pos.y == t_pos.y) ||