ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Revision:
31:5c4acae51026
Parent:
30:863565e9859f
Child:
33:de130e274391
--- a/Floors/Floors.cpp	Thu May 09 08:59:39 2019 +0000
+++ b/Floors/Floors.cpp	Thu May 09 10:15:20 2019 +0000
@@ -10,68 +10,68 @@
 {
     _height = height;
     _width = width;
-//  x-coordinate initially random: 2-30 or 40-71 so doodler falls to bottom floor
-    int decide = rand() % 2;
-    if (decide == 0) { // right
+    end_game = false;  // the read decision should be false initially so that the game is not ended as soon as it starts
+    int decide = rand() % 2;  // x-coordinate initially random: 2-30 or 40-71 so doodler falls to bottom floor
+    if (decide == 0) {  // right
         _position.x = 2 + (rand()% 28);
-    } else { // left
+    } else {  // left
         _position.x = 40 + (rand()% 31);
     }
     _position.y = y;
-    
+
 }
 
 void Floors::draw(N5110 &lcd)
 {
     lcd.drawRect(_position.x, _position.y, _width, _height, FILL_BLACK);
-    eny.draw(lcd); // draws enemy
+    eny.draw(lcd);  // draws enemy
 }
 
 void Floors::update(float doodler_pos_x, float doodler_pos_y, float _bullet_pos_x, float _bullet_pos_y)
-{
-// when they leave the screen they will re-appear in random x-coordinate so that 10 floors are always on screen
-//rectangle (1-82 & 9 - 48 )
+{  // when they leave the screen they will re-appear in random x-coordinate so that 10 floors are always on screen
+    // rectangle (1-82 & 9 - 48 )
     _doodler_pos_x = doodler_pos_x;
     _doodler_pos_y = doodler_pos_y;
-    if (_position.y > 45 ) { // the place decision bool is updated every time a floor re-appears
+    if (_position.y > 45 ) {  // the place decision bool is updated every time a floor re-appears
         _position.y = 9;
-        _position.x = 1 + (rand()% 70);
+        _position.x = 1 + (rand()% 69);
         place = rand()% 6;
-        if (place == 2) { // 1/6 chance of placing a ghost
+        if (place == 2) {  // 1/6 chance of placing a ghost
             eny.update(_position);
             put_enemy = true;
-        } else{
+        } else {
             eny.erase();
             put_enemy = false;
         }
     }
-    if (_doodler_pos_y < 13) { // shift floors once doodler reaches this value on the screen
-    // I chose this value because testing with the doodler's jump, it allows the doodler to keep jumping on a floor
-    // without it moving down and dissapearing
+    if (_doodler_pos_y < 13) {  // shift floors once doodler reaches this value on the screen
+        // I chose this value because testing with the doodler's jump, it allows the doodler to keep jumping on a floor
+        // without it moving down and dissapearing
         _position.y  += 1;
     }
     check_enemy(_bullet_pos_x, _bullet_pos_y);
 }
 
-void Floors::check_enemy(float _bullet_pos_x, float _bullet_pos_y){
+void Floors::check_enemy(float _bullet_pos_x, float _bullet_pos_y)
+{
     enemy_position = eny.get_position();
-    if (put_enemy == true){
+    if (put_enemy == true) {
         eny.update(_position);
     }
-    if ( // to check if the doodler has collided with the ghost
-    (_doodler_pos_x + 6 <= enemy_position.x + 12) &&
-    (_doodler_pos_x + 6 >= enemy_position.x) &&
-    (_doodler_pos_y <= enemy_position.y + 15) &&
-    (_doodler_pos_y >= enemy_position.y) 
-    ){
-        eny.erase(); // should end the game
+    if (  // to check if the doodler has collided with the ghost
+        (_doodler_pos_x + 6 <= enemy_position.x + 12) &&
+        (_doodler_pos_x + 6 >= enemy_position.x) &&
+        (_doodler_pos_y <= enemy_position.y + 11) &&
+        (_doodler_pos_y >= enemy_position.y)
+    ) {
+        end_game = true;
     }
-    if ( // to check if the bullet has collided with the ghost
-    (_bullet_pos_x <= enemy_position.x + 12) &&
-    (_bullet_pos_x >= enemy_position.x) &&
-    (_bullet_pos_y <= enemy_position.y + 15) &&
-    (_bullet_pos_y >= enemy_position.y) 
-    ){
+    if (  // to check if the bullet has collided with the ghost
+        (_bullet_pos_x <= enemy_position.x + 12) &&
+        (_bullet_pos_x >= enemy_position.x) &&
+        (_bullet_pos_y <= enemy_position.y + 11) &&
+        (_bullet_pos_y >= enemy_position.y)
+    ) {
         eny.erase();
     }
 }
@@ -87,3 +87,10 @@
     _position.x = pos.x;
     _position.y = pos.y;
 }
+
+// The get_end_game function can be called in the engine to check if the game should end or not (if the doodler has collided
+// with the enemy)
+bool Floors::get_end_game()
+{
+    return end_game;
+}
\ No newline at end of file