Class used to run the maze game loop.

Revision:
1:5a44ce88c5e2
Parent:
0:afee1085c5ef
Child:
2:cbce5d35e7d6
--- a/MazeEngine.cpp	Fri Apr 07 10:29:16 2017 +0000
+++ b/MazeEngine.cpp	Mon Apr 17 08:47:00 2017 +0000
@@ -19,38 +19,115 @@
     
 }
 
-void MazeEngine::init(int mazeIndex, int x, int y, int radius)
+void MazeEngine::init(int mazeIndex,
+                      int x,
+                      int y,
+                      int radius,
+                      bool control,
+                      bool colour)
 {
     // maze parameters
     _mazeIndex = mazeIndex;
     
+    // method of controlling
+    _control = control;
+    // _control = false;   // set as false just for debugging
+    
+    // style of ball
+    _colour = colour;
+    
     // ball parameters
     _radius = radius;
     _x = x;
     _y = y;
     
     // initialise ball & maze with parameters
-    _ball.init(_x, _y, _radius);
+    _ball.init(_x, _y, _radius, _colour);
     _maze.init(_mazeIndex);
 }
 
 void MazeEngine::readInput(Gamepad &pad, FXOS8700CQ &device)
 {
-    // acquire input from devices
-    _direction = pad.get_direction();   // direction is a 2D struct
-    _magnitude = pad.get_mag();
-    /*
-     else {
-     
-     _direction = devie.
-     }
-     */
+    if (_control){  // if control is true (default), input comes from joystick
+        readJoystickInput(pad);
+    }
+    else {  // if control is false, input comes from accelerometer
+        readAccelerometer(device);
+    }
+}
+
+void MazeEngine::readJoystickInput(Gamepad &pad)
+{
+    // position is a 2D struct for (x,y)
+    position = pad.get_mapped_coord();
+    
+    // printf("Joystick inputs: %.2f, %.2f \n", position.x, position.y);
+}
+
+void MazeEngine::readAccelerometer(FXOS8700CQ &device)
+{
+    // acquire values from device
+    Data values = device.get_values();
+    
+    // values are between 0 and 90°
+    float pitch = device.getPitchAngle();
+    float roll = device.getRollAngle();
+    
+    position.x = pitch / -60;    // divide by 60 for increase sensitivity
+    position.y = roll / -60;
 }
 
-void MazeEngine::update(Gamepad &pad, N5110 &lcd)
+void MazeEngine::checkWallCollision(N5110 &lcd)
 {
-    _ball.update();
+    // acquire position of ball
+    // if position on each side of ball is a pixel
+    // block movement in that direction
+    
+    Vector2D _position = _ball.getPosition();
+    Vector2D _velocity = _ball.getVelocity();
+    
+    int leftSide = _position.x - _radius;
+    int rightSide = _position.x + _radius;
+    int topSide = _position.y - _radius;
+    int lowerSide = _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");
+    }
     
+    if ((lowerSide + lowerPixel) != lowerSide ){
+        _velocity.y = 0;
+        printf("Low side hit wall");
+    }
+    
+    if ((leftSide + leftPixel) != leftSide){    // if the sum is changed by pixel it must be present
+        _velocity.y = 0;
+        printf("Left side hit wall");
+    }
+    
+    if ((rightSide + rightPixel) != rightSide ){
+        _velocity.y = 0;
+        printf("Right side hit wall");
+    }
+    
+    _ball.setVelocity(_velocity);
+}
+
+
+void MazeEngine::update(N5110 &lcd)
+{
+    // 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);
 }
 
 void MazeEngine::draw(N5110 &lcd)