Class used to run the maze game loop.

MazeEngine.cpp

Committer:
el15mh
Date:
2017-04-17
Revision:
1:5a44ce88c5e2
Parent:
0:afee1085c5ef
Child:
2:cbce5d35e7d6

File content as of revision 1:5a44ce88c5e2:

/*
 
 @file mazeEngine.cpp
 
 (c) Max Houghton 19.03.2017
 Roller Maze Project, ELEC2645, Univeristy of Leeds
 
 */

#include "MazeEngine.h"

MazeEngine::MazeEngine()
{
    
}

MazeEngine::~MazeEngine()
{
    
}

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, _colour);
    _maze.init(_mazeIndex);
}

void MazeEngine::readInput(Gamepad &pad, FXOS8700CQ &device)
{
    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::checkWallCollision(N5110 &lcd)
{
    // 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)
{
    _ball.draw(lcd);
    _maze.draw(lcd);
}