Class used to run the maze game loop.

Revision:
2:cbce5d35e7d6
Parent:
1:5a44ce88c5e2
Child:
3:4b82cb2e0618
--- a/MazeEngine.cpp	Mon Apr 17 08:47:00 2017 +0000
+++ b/MazeEngine.cpp	Wed Apr 19 20:16:56 2017 +0000
@@ -85,37 +85,49 @@
     
     Vector2D _position = _ball.getPosition();
     Vector2D _velocity = _ball.getVelocity();
+        
+    // printf("position = (%f, %f) \n", _position.x, _position.y);
     
-    int leftSide = _position.x - _radius;
-    int rightSide = _position.x + _radius;
-    int topSide = _position.y - _radius;
-    int lowerSide = _position.y + _radius;
+    int leftSide = int(_position.x - _radius);
+    int rightSide = int(_position.x + _radius);
+    int topSide = int(_position.y - _radius);
+    int lowerSide = int (_position.y + _radius);
     
-    bool topPixel = lcd.getPixel(_position.x, topSide - 1);
-    bool lowerPixel = lcd.getPixel(_position.x, lowerSide + 1);
-    bool leftPixel = lcd.getPixel(_position.y, leftSide - 1);
-    bool rightPixel = lcd.getPixel(_position.y, rightSide + 1);
-    
-    if ((topSide + topPixel) != topSide){    // if the sum is changed by pixel it must be present
-        _velocity.y = 0;
-        printf("Top side hit wall");
+    // check position of ball
+    // if distance between outermost pixel and centre changes
+    // there must be a pixel in adjacent position
+    if (leftSide + _mazeArray[leftSide - 1][int(_position.y)] != leftSide){
+        
+        // set velocity to 0
+        _velocity.x = 0;
+        // ball can't move any further left
+        _position.x += 1;
+        printf("left side hit \n");
     }
     
-    if ((lowerSide + lowerPixel) != lowerSide ){
-        _velocity.y = 0;
-        printf("Low side hit wall");
+    if (rightSide + _mazeArray[rightSide + 1][int(_position.y)] != rightSide){
+        
+        _velocity.x = 0;
+        _position.x -= 1;
+        printf("right side hit \n");
     }
     
-    if ((leftSide + leftPixel) != leftSide){    // if the sum is changed by pixel it must be present
+    
+    if (topSide + _mazeArray[int(_position.x)][topSide - 1] != topSide){
+        
         _velocity.y = 0;
-        printf("Left side hit wall");
+        _position.y += 1;
+        printf("top side hit \n");
     }
     
-    if ((rightSide + rightPixel) != rightSide ){
+    if (lowerSide + _mazeArray[int(_position.x)][lowerSide + 1] != lowerSide){
+        
         _velocity.y = 0;
-        printf("Right side hit wall");
+        _position.y -= 1;
+        printf("low side hit \n");
     }
     
+    _ball.setPosition(_position);
     _ball.setVelocity(_velocity);
 }
 
@@ -127,13 +139,42 @@
     
     // reverts the changes if the proposed new position sends ball
     // into the wall
-    checkWallCollision(lcd);
+    // checkWallCollision(lcd);
+}
+
+void MazeEngine::getMazeArray(N5110 &lcd)
+{
+    for (int i = 0; i < 84; i++){
+        for (int j = 0; j < 48; j++){
+            
+            int pixel = lcd.getPixel(i, j);
+            
+            if (pixel == 1){
+                
+                _mazeArray[i][j] = 1;
+            }
+            else {
+                
+                _mazeArray[i][j] = 0;
+            }
+        }
+        
+    }
+    /*
+     for (int i = 0; i < 84; i++){
+     for (int j = 0; j < 48; j++){
+     
+     printf("%f", _mazeArray[i][j]);
+     }
+     }
+     */
 }
 
 void MazeEngine::draw(N5110 &lcd)
 {
+    _maze.draw(lcd);
+    getMazeArray(lcd);
+    
     _ball.draw(lcd);
-    _maze.draw(lcd);
 }
 
-