Class used to run the maze game loop.

Revision:
3:4b82cb2e0618
Parent:
2:cbce5d35e7d6
Child:
4:93fd2f3bd0de
--- a/MazeEngine.cpp	Wed Apr 19 20:16:56 2017 +0000
+++ b/MazeEngine.cpp	Fri Apr 21 12:47:52 2017 +0000
@@ -73,8 +73,8 @@
     float pitch = device.getPitchAngle();
     float roll = device.getRollAngle();
     
-    position.x = pitch / -60;    // divide by 60 for increase sensitivity
-    position.y = roll / -60;
+    position.x = pitch / -30;    // divide by 30 for increase sensitivity
+    position.y = roll / -30;
 }
 
 void MazeEngine::checkWallCollision(N5110 &lcd)
@@ -85,7 +85,9 @@
     
     Vector2D _position = _ball.getPosition();
     Vector2D _velocity = _ball.getVelocity();
-        
+    
+    // Vector2D position;
+    
     // printf("position = (%f, %f) \n", _position.x, _position.y);
     
     int leftSide = int(_position.x - _radius);
@@ -134,12 +136,19 @@
 
 void MazeEngine::update(N5110 &lcd)
 {
+    // check if ball is in goal before moving to next position
+    if (checkGoal()){
+        
+        return;
+    }
+    
     // changes the location of the ball according to inputs
     _ball.update(position);
     
     // reverts the changes if the proposed new position sends ball
     // into the wall
-    // checkWallCollision(lcd);
+    checkWallCollision(lcd);
+
 }
 
 void MazeEngine::getMazeArray(N5110 &lcd)
@@ -160,21 +169,114 @@
         }
         
     }
-    /*
-     for (int i = 0; i < 84; i++){
-     for (int j = 0; j < 48; j++){
-     
-     printf("%f", _mazeArray[i][j]);
-     }
-     }
-     */
 }
 
 void MazeEngine::draw(N5110 &lcd)
 {
+    if (checkGoal()){
+        
+        lcd.printString("     MAZE", 0, 1);
+        lcd.printString("   COMPLETED", 0, 2);
+        
+        wait(2);
+        
+        return;
+    }
+    
     _maze.draw(lcd);
     getMazeArray(lcd);
     
+    _maze.drawGoal(lcd);
     _ball.draw(lcd);
 }
 
+bool MazeEngine::checkGoal()
+{
+    Vector2D position = _ball.getPosition();
+    
+    // check for each maze index centre of ball according to location of goal on each map
+    if (_mazeIndex == 0){
+        
+        // check goal region
+        for (int i = 78; i < 81; i++){
+            for (int j = 42; j < 45; j++){
+                
+                if ((position.x == i) && (position.y == j)){
+                    
+                    printf("Goal reached");
+                    _goal = true;
+                }
+            }
+        }
+    }
+    
+    else if (_mazeIndex == 1){
+        
+        // check goal region
+        for (int i = 77; i < 80; i++){
+            for (int j = 3; j < 7; j++){
+                
+                if ((position.x == i) && (position.y == j)){
+                    
+                    printf("Goal reached");
+                    _goal = true;
+                }
+            }
+        }
+        
+    }
+    
+    else if (_mazeIndex == 2){
+        
+        // check goal region
+        for (int i = 77; i < 80; i++){
+            for (int j = 3; j < 6; j++){
+                
+                if ((position.x == i) && (position.y == j)){
+                    
+                    printf("Goal reached");
+                    _goal = true;
+                }
+            }
+        }
+        
+    }
+    
+    else if (_mazeIndex == 3){
+        
+        // check goal region
+        for (int i = 76; i < 79; i++){
+            for (int j = 40; j < 43; j++){
+                
+                if ((position.x == i) && (position.y == j)){
+                    
+                    printf("Goal reached");
+                    _goal = true;
+                }
+            }
+        }
+    }
+    
+    else {
+        
+        // check goal region
+        for (int i = 42; i < 44; i++){
+            for (int j = 23; j < 25; j++){
+                
+                if ((position.x == i) && (position.y == j)){
+                    
+                    printf("Goal reached");
+                    _goal = true;
+                }
+            }
+        }
+        
+    }
+    
+    return _goal;
+}
+
+
+
+
+