Class used to run the maze game loop.
MazeEngine.cpp
- Committer:
- el15mh
- Date:
- 2017-04-21
- Revision:
- 3:4b82cb2e0618
- Parent:
- 2:cbce5d35e7d6
- Child:
- 4:93fd2f3bd0de
File content as of revision 3:4b82cb2e0618:
/* @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 / -30; // divide by 30 for increase sensitivity position.y = roll / -30; } 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(); // Vector2D position; // printf("position = (%f, %f) \n", _position.x, _position.y); int leftSide = int(_position.x - _radius); int rightSide = int(_position.x + _radius); int topSide = int(_position.y - _radius); int lowerSide = int (_position.y + _radius); // 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 (rightSide + _mazeArray[rightSide + 1][int(_position.y)] != rightSide){ _velocity.x = 0; _position.x -= 1; printf("right side hit \n"); } if (topSide + _mazeArray[int(_position.x)][topSide - 1] != topSide){ _velocity.y = 0; _position.y += 1; 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"); } _ball.setPosition(_position); _ball.setVelocity(_velocity); } 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); } 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; } } } } 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; }