Class used to run the maze game loop.

Revision:
4:93fd2f3bd0de
Parent:
3:4b82cb2e0618
Child:
5:3635319c64b8
--- a/MazeEngine.cpp	Fri Apr 21 12:47:52 2017 +0000
+++ b/MazeEngine.cpp	Wed May 03 21:13:52 2017 +0000
@@ -73,8 +73,8 @@
     float pitch = device.getPitchAngle();
     float roll = device.getRollAngle();
     
-    position.x = pitch / -30;    // divide by 30 for increase sensitivity
-    position.y = roll / -30;
+    position.x = pitch / -60;    // divide by 60 for increase sensitivity
+    position.y = roll / -60;
 }
 
 void MazeEngine::checkWallCollision(N5110 &lcd)
@@ -85,9 +85,7 @@
     
     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);
@@ -104,14 +102,14 @@
         _velocity.x = 0;
         // ball can't move any further left
         _position.x += 1;
-        printf("left side hit \n");
+        // printf("left side hit \n");
     }
     
     if (rightSide + _mazeArray[rightSide + 1][int(_position.y)] != rightSide){
         
         _velocity.x = 0;
         _position.x -= 1;
-        printf("right side hit \n");
+        // printf("right side hit \n");
     }
     
     
@@ -119,14 +117,14 @@
         
         _velocity.y = 0;
         _position.y += 1;
-        printf("top side hit \n");
+        // printf("top side hit \n");
     }
     
     if (lowerSide + _mazeArray[int(_position.x)][lowerSide + 1] != lowerSide){
         
         _velocity.y = 0;
         _position.y -= 1;
-        printf("low side hit \n");
+        // printf("low side hit \n");
     }
     
     _ball.setPosition(_position);
@@ -139,6 +137,7 @@
     // check if ball is in goal before moving to next position
     if (checkGoal()){
         
+        // printf("Goal reached");
         return;
     }
     
@@ -148,7 +147,7 @@
     // reverts the changes if the proposed new position sends ball
     // into the wall
     checkWallCollision(lcd);
-
+    
 }
 
 void MazeEngine::getMazeArray(N5110 &lcd)
@@ -173,20 +172,11 @@
 
 void MazeEngine::draw(N5110 &lcd)
 {
-    if (checkGoal()){
-        
-        lcd.printString("     MAZE", 0, 1);
-        lcd.printString("   COMPLETED", 0, 2);
-        
-        wait(2);
-        
-        return;
-    }
-    
+    // mazes are drawn according to selected maze index (default = 0)
     _maze.draw(lcd);
+    // array acquired for wall collisions
     getMazeArray(lcd);
     
-    _maze.drawGoal(lcd);
     _ball.draw(lcd);
 }
 
@@ -194,83 +184,33 @@
 {
     Vector2D position = _ball.getPosition();
     
-    // check for each maze index centre of ball according to location of goal on each map
-    if (_mazeIndex == 0){
+    // completion when ball goes outside map for all mazes apart
+    // from extreme (mazeIndex 9)
+    if (_mazeIndex == 9){
         
-        // check goal region
-        for (int i = 78; i < 81; i++){
-            for (int j = 42; j < 45; j++){
+        // check central region for goal
+        for (int i = 41; i < 46; i++){
+            for (int j = 22; j < 25; 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)){
+                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;
+                    return _goal;
                 }
             }
         }
     }
     
     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;
-                }
-            }
+        if (position.x > 86){
+            
+            // printf("Goal reached");
+            _goal = true;
+            return _goal;
         }
-        
     }
     
     return _goal;