Class used to run the maze game loop.

Committer:
el15mh
Date:
Thu May 04 14:02:05 2017 +0000
Revision:
5:3635319c64b8
Parent:
4:93fd2f3bd0de
Doxygen commenting added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15mh 0:afee1085c5ef 1 #include "MazeEngine.h"
el15mh 0:afee1085c5ef 2
el15mh 5:3635319c64b8 3 /**
el15mh 5:3635319c64b8 4 * @details - constructor
el15mh 5:3635319c64b8 5 */
el15mh 0:afee1085c5ef 6 MazeEngine::MazeEngine()
el15mh 0:afee1085c5ef 7 {
el15mh 0:afee1085c5ef 8
el15mh 0:afee1085c5ef 9 }
el15mh 0:afee1085c5ef 10
el15mh 5:3635319c64b8 11 /**
el15mh 5:3635319c64b8 12 * @details - constructor
el15mh 5:3635319c64b8 13 */
el15mh 0:afee1085c5ef 14 MazeEngine::~MazeEngine()
el15mh 0:afee1085c5ef 15 {
el15mh 0:afee1085c5ef 16
el15mh 0:afee1085c5ef 17 }
el15mh 0:afee1085c5ef 18
el15mh 5:3635319c64b8 19 /**
el15mh 5:3635319c64b8 20 * @details - Initialises the game with specified paramters
el15mh 5:3635319c64b8 21 */
el15mh 1:5a44ce88c5e2 22 void MazeEngine::init(int mazeIndex,
el15mh 1:5a44ce88c5e2 23 int x,
el15mh 1:5a44ce88c5e2 24 int y,
el15mh 1:5a44ce88c5e2 25 int radius,
el15mh 1:5a44ce88c5e2 26 bool control,
el15mh 1:5a44ce88c5e2 27 bool colour)
el15mh 0:afee1085c5ef 28 {
el15mh 0:afee1085c5ef 29 // maze parameters
el15mh 0:afee1085c5ef 30 _mazeIndex = mazeIndex;
el15mh 0:afee1085c5ef 31
el15mh 1:5a44ce88c5e2 32 // method of controlling
el15mh 1:5a44ce88c5e2 33 _control = control;
el15mh 1:5a44ce88c5e2 34 // _control = false; // set as false just for debugging
el15mh 1:5a44ce88c5e2 35
el15mh 1:5a44ce88c5e2 36 // style of ball
el15mh 1:5a44ce88c5e2 37 _colour = colour;
el15mh 1:5a44ce88c5e2 38
el15mh 0:afee1085c5ef 39 // ball parameters
el15mh 0:afee1085c5ef 40 _radius = radius;
el15mh 0:afee1085c5ef 41 _x = x;
el15mh 0:afee1085c5ef 42 _y = y;
el15mh 0:afee1085c5ef 43
el15mh 0:afee1085c5ef 44 // initialise ball & maze with parameters
el15mh 1:5a44ce88c5e2 45 _ball.init(_x, _y, _radius, _colour);
el15mh 0:afee1085c5ef 46 _maze.init(_mazeIndex);
el15mh 0:afee1085c5ef 47 }
el15mh 0:afee1085c5ef 48
el15mh 5:3635319c64b8 49 /**
el15mh 5:3635319c64b8 50 * @details - Reads input from chosen device
el15mh 5:3635319c64b8 51 */
el15mh 0:afee1085c5ef 52 void MazeEngine::readInput(Gamepad &pad, FXOS8700CQ &device)
el15mh 0:afee1085c5ef 53 {
el15mh 1:5a44ce88c5e2 54 if (_control){ // if control is true (default), input comes from joystick
el15mh 1:5a44ce88c5e2 55 readJoystickInput(pad);
el15mh 1:5a44ce88c5e2 56 }
el15mh 1:5a44ce88c5e2 57 else { // if control is false, input comes from accelerometer
el15mh 1:5a44ce88c5e2 58 readAccelerometer(device);
el15mh 1:5a44ce88c5e2 59 }
el15mh 1:5a44ce88c5e2 60 }
el15mh 1:5a44ce88c5e2 61
el15mh 5:3635319c64b8 62 /**
el15mh 5:3635319c64b8 63 * @details - Reads joystick input
el15mh 5:3635319c64b8 64 */
el15mh 1:5a44ce88c5e2 65 void MazeEngine::readJoystickInput(Gamepad &pad)
el15mh 1:5a44ce88c5e2 66 {
el15mh 1:5a44ce88c5e2 67 // position is a 2D struct for (x,y)
el15mh 1:5a44ce88c5e2 68 position = pad.get_mapped_coord();
el15mh 1:5a44ce88c5e2 69
el15mh 1:5a44ce88c5e2 70 // printf("Joystick inputs: %.2f, %.2f \n", position.x, position.y);
el15mh 1:5a44ce88c5e2 71 }
el15mh 1:5a44ce88c5e2 72
el15mh 5:3635319c64b8 73 /**
el15mh 5:3635319c64b8 74 * @details - Reads accelerometer input
el15mh 5:3635319c64b8 75 */
el15mh 1:5a44ce88c5e2 76 void MazeEngine::readAccelerometer(FXOS8700CQ &device)
el15mh 1:5a44ce88c5e2 77 {
el15mh 1:5a44ce88c5e2 78 // acquire values from device
el15mh 1:5a44ce88c5e2 79 Data values = device.get_values();
el15mh 1:5a44ce88c5e2 80
el15mh 1:5a44ce88c5e2 81 // values are between 0 and 90°
el15mh 1:5a44ce88c5e2 82 float pitch = device.getPitchAngle();
el15mh 1:5a44ce88c5e2 83 float roll = device.getRollAngle();
el15mh 1:5a44ce88c5e2 84
el15mh 4:93fd2f3bd0de 85 position.x = pitch / -60; // divide by 60 for increase sensitivity
el15mh 4:93fd2f3bd0de 86 position.y = roll / -60;
el15mh 0:afee1085c5ef 87 }
el15mh 0:afee1085c5ef 88
el15mh 5:3635319c64b8 89 /**
el15mh 5:3635319c64b8 90 * @details - Checks for wall collisions
el15mh 5:3635319c64b8 91 */
el15mh 1:5a44ce88c5e2 92 void MazeEngine::checkWallCollision(N5110 &lcd)
el15mh 0:afee1085c5ef 93 {
el15mh 1:5a44ce88c5e2 94 // acquire position of ball
el15mh 1:5a44ce88c5e2 95 // if position on each side of ball is a pixel
el15mh 1:5a44ce88c5e2 96 // block movement in that direction
el15mh 1:5a44ce88c5e2 97
el15mh 1:5a44ce88c5e2 98 Vector2D _position = _ball.getPosition();
el15mh 1:5a44ce88c5e2 99 Vector2D _velocity = _ball.getVelocity();
el15mh 4:93fd2f3bd0de 100
el15mh 2:cbce5d35e7d6 101 // printf("position = (%f, %f) \n", _position.x, _position.y);
el15mh 1:5a44ce88c5e2 102
el15mh 2:cbce5d35e7d6 103 int leftSide = int(_position.x - _radius);
el15mh 2:cbce5d35e7d6 104 int rightSide = int(_position.x + _radius);
el15mh 2:cbce5d35e7d6 105 int topSide = int(_position.y - _radius);
el15mh 2:cbce5d35e7d6 106 int lowerSide = int (_position.y + _radius);
el15mh 1:5a44ce88c5e2 107
el15mh 2:cbce5d35e7d6 108 // check position of ball
el15mh 5:3635319c64b8 109 // if distance between outermost pixel and centre != original distance
el15mh 2:cbce5d35e7d6 110 // there must be a pixel in adjacent position
el15mh 2:cbce5d35e7d6 111 if (leftSide + _mazeArray[leftSide - 1][int(_position.y)] != leftSide){
el15mh 2:cbce5d35e7d6 112
el15mh 2:cbce5d35e7d6 113 // set velocity to 0
el15mh 2:cbce5d35e7d6 114 _velocity.x = 0;
el15mh 2:cbce5d35e7d6 115 // ball can't move any further left
el15mh 2:cbce5d35e7d6 116 _position.x += 1;
el15mh 4:93fd2f3bd0de 117 // printf("left side hit \n");
el15mh 1:5a44ce88c5e2 118 }
el15mh 0:afee1085c5ef 119
el15mh 2:cbce5d35e7d6 120 if (rightSide + _mazeArray[rightSide + 1][int(_position.y)] != rightSide){
el15mh 2:cbce5d35e7d6 121
el15mh 2:cbce5d35e7d6 122 _velocity.x = 0;
el15mh 2:cbce5d35e7d6 123 _position.x -= 1;
el15mh 4:93fd2f3bd0de 124 // printf("right side hit \n");
el15mh 1:5a44ce88c5e2 125 }
el15mh 1:5a44ce88c5e2 126
el15mh 2:cbce5d35e7d6 127
el15mh 2:cbce5d35e7d6 128 if (topSide + _mazeArray[int(_position.x)][topSide - 1] != topSide){
el15mh 2:cbce5d35e7d6 129
el15mh 1:5a44ce88c5e2 130 _velocity.y = 0;
el15mh 2:cbce5d35e7d6 131 _position.y += 1;
el15mh 4:93fd2f3bd0de 132 // printf("top side hit \n");
el15mh 1:5a44ce88c5e2 133 }
el15mh 1:5a44ce88c5e2 134
el15mh 2:cbce5d35e7d6 135 if (lowerSide + _mazeArray[int(_position.x)][lowerSide + 1] != lowerSide){
el15mh 2:cbce5d35e7d6 136
el15mh 1:5a44ce88c5e2 137 _velocity.y = 0;
el15mh 2:cbce5d35e7d6 138 _position.y -= 1;
el15mh 4:93fd2f3bd0de 139 // printf("low side hit \n");
el15mh 1:5a44ce88c5e2 140 }
el15mh 1:5a44ce88c5e2 141
el15mh 2:cbce5d35e7d6 142 _ball.setPosition(_position);
el15mh 1:5a44ce88c5e2 143 _ball.setVelocity(_velocity);
el15mh 1:5a44ce88c5e2 144 }
el15mh 1:5a44ce88c5e2 145
el15mh 5:3635319c64b8 146 /**
el15mh 5:3635319c64b8 147 * @details - updates the game
el15mh 5:3635319c64b8 148 */
el15mh 1:5a44ce88c5e2 149 void MazeEngine::update(N5110 &lcd)
el15mh 1:5a44ce88c5e2 150 {
el15mh 3:4b82cb2e0618 151 // check if ball is in goal before moving to next position
el15mh 3:4b82cb2e0618 152 if (checkGoal()){
el15mh 3:4b82cb2e0618 153
el15mh 4:93fd2f3bd0de 154 // printf("Goal reached");
el15mh 3:4b82cb2e0618 155 return;
el15mh 3:4b82cb2e0618 156 }
el15mh 3:4b82cb2e0618 157
el15mh 1:5a44ce88c5e2 158 // changes the location of the ball according to inputs
el15mh 1:5a44ce88c5e2 159 _ball.update(position);
el15mh 1:5a44ce88c5e2 160
el15mh 1:5a44ce88c5e2 161 // reverts the changes if the proposed new position sends ball
el15mh 1:5a44ce88c5e2 162 // into the wall
el15mh 3:4b82cb2e0618 163 checkWallCollision(lcd);
el15mh 4:93fd2f3bd0de 164
el15mh 2:cbce5d35e7d6 165 }
el15mh 2:cbce5d35e7d6 166
el15mh 5:3635319c64b8 167 /**
el15mh 5:3635319c64b8 168 * @details - Acquires maze array used for wall collision checks
el15mh 5:3635319c64b8 169 */
el15mh 2:cbce5d35e7d6 170 void MazeEngine::getMazeArray(N5110 &lcd)
el15mh 2:cbce5d35e7d6 171 {
el15mh 2:cbce5d35e7d6 172 for (int i = 0; i < 84; i++){
el15mh 2:cbce5d35e7d6 173 for (int j = 0; j < 48; j++){
el15mh 2:cbce5d35e7d6 174
el15mh 2:cbce5d35e7d6 175 int pixel = lcd.getPixel(i, j);
el15mh 2:cbce5d35e7d6 176
el15mh 2:cbce5d35e7d6 177 if (pixel == 1){
el15mh 2:cbce5d35e7d6 178
el15mh 2:cbce5d35e7d6 179 _mazeArray[i][j] = 1;
el15mh 2:cbce5d35e7d6 180 }
el15mh 2:cbce5d35e7d6 181 else {
el15mh 2:cbce5d35e7d6 182
el15mh 2:cbce5d35e7d6 183 _mazeArray[i][j] = 0;
el15mh 2:cbce5d35e7d6 184 }
el15mh 2:cbce5d35e7d6 185 }
el15mh 2:cbce5d35e7d6 186
el15mh 2:cbce5d35e7d6 187 }
el15mh 0:afee1085c5ef 188 }
el15mh 0:afee1085c5ef 189
el15mh 5:3635319c64b8 190 /**
el15mh 5:3635319c64b8 191 * @details - Draws ball and maze
el15mh 5:3635319c64b8 192 */
el15mh 0:afee1085c5ef 193 void MazeEngine::draw(N5110 &lcd)
el15mh 0:afee1085c5ef 194 {
el15mh 4:93fd2f3bd0de 195 // mazes are drawn according to selected maze index (default = 0)
el15mh 2:cbce5d35e7d6 196 _maze.draw(lcd);
el15mh 4:93fd2f3bd0de 197 // array acquired for wall collisions
el15mh 2:cbce5d35e7d6 198 getMazeArray(lcd);
el15mh 2:cbce5d35e7d6 199
el15mh 0:afee1085c5ef 200 _ball.draw(lcd);
el15mh 0:afee1085c5ef 201 }
el15mh 0:afee1085c5ef 202
el15mh 5:3635319c64b8 203 /**
el15mh 5:3635319c64b8 204 * @details - Checks if goal has been achieved.
el15mh 5:3635319c64b8 205 */
el15mh 3:4b82cb2e0618 206 bool MazeEngine::checkGoal()
el15mh 3:4b82cb2e0618 207 {
el15mh 3:4b82cb2e0618 208 Vector2D position = _ball.getPosition();
el15mh 3:4b82cb2e0618 209
el15mh 4:93fd2f3bd0de 210 // completion when ball goes outside map for all mazes apart
el15mh 4:93fd2f3bd0de 211 // from extreme (mazeIndex 9)
el15mh 4:93fd2f3bd0de 212 if (_mazeIndex == 9){
el15mh 3:4b82cb2e0618 213
el15mh 4:93fd2f3bd0de 214 // check central region for goal
el15mh 4:93fd2f3bd0de 215 for (int i = 41; i < 46; i++){
el15mh 4:93fd2f3bd0de 216 for (int j = 22; j < 25; j++){
el15mh 3:4b82cb2e0618 217
el15mh 4:93fd2f3bd0de 218 if ((position.x == i) &&
el15mh 4:93fd2f3bd0de 219 (position.y == j)) {
el15mh 3:4b82cb2e0618 220
el15mh 3:4b82cb2e0618 221 printf("Goal reached");
el15mh 3:4b82cb2e0618 222 _goal = true;
el15mh 3:4b82cb2e0618 223
el15mh 4:93fd2f3bd0de 224 return _goal;
el15mh 3:4b82cb2e0618 225 }
el15mh 3:4b82cb2e0618 226 }
el15mh 3:4b82cb2e0618 227 }
el15mh 3:4b82cb2e0618 228 }
el15mh 3:4b82cb2e0618 229
el15mh 3:4b82cb2e0618 230 else {
el15mh 5:3635319c64b8 231
el15mh 4:93fd2f3bd0de 232 if (position.x > 86){
el15mh 4:93fd2f3bd0de 233
el15mh 4:93fd2f3bd0de 234 // printf("Goal reached");
el15mh 4:93fd2f3bd0de 235 _goal = true;
el15mh 4:93fd2f3bd0de 236 return _goal;
el15mh 3:4b82cb2e0618 237 }
el15mh 3:4b82cb2e0618 238 }
el15mh 3:4b82cb2e0618 239
el15mh 3:4b82cb2e0618 240 return _goal;
el15mh 3:4b82cb2e0618 241 }
el15mh 3:4b82cb2e0618 242
el15mh 3:4b82cb2e0618 243
el15mh 3:4b82cb2e0618 244
el15mh 3:4b82cb2e0618 245
el15mh 3:4b82cb2e0618 246