Class used to run the maze game loop.

Committer:
el15mh
Date:
Fri Apr 21 12:47:52 2017 +0000
Revision:
3:4b82cb2e0618
Parent:
2:cbce5d35e7d6
Child:
4:93fd2f3bd0de
goal check implemented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15mh 0:afee1085c5ef 1 /*
el15mh 0:afee1085c5ef 2
el15mh 0:afee1085c5ef 3 @file mazeEngine.cpp
el15mh 0:afee1085c5ef 4
el15mh 0:afee1085c5ef 5 (c) Max Houghton 19.03.2017
el15mh 0:afee1085c5ef 6 Roller Maze Project, ELEC2645, Univeristy of Leeds
el15mh 0:afee1085c5ef 7
el15mh 0:afee1085c5ef 8 */
el15mh 0:afee1085c5ef 9
el15mh 0:afee1085c5ef 10 #include "MazeEngine.h"
el15mh 0:afee1085c5ef 11
el15mh 0:afee1085c5ef 12 MazeEngine::MazeEngine()
el15mh 0:afee1085c5ef 13 {
el15mh 0:afee1085c5ef 14
el15mh 0:afee1085c5ef 15 }
el15mh 0:afee1085c5ef 16
el15mh 0:afee1085c5ef 17 MazeEngine::~MazeEngine()
el15mh 0:afee1085c5ef 18 {
el15mh 0:afee1085c5ef 19
el15mh 0:afee1085c5ef 20 }
el15mh 0:afee1085c5ef 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 0:afee1085c5ef 49 void MazeEngine::readInput(Gamepad &pad, FXOS8700CQ &device)
el15mh 0:afee1085c5ef 50 {
el15mh 1:5a44ce88c5e2 51 if (_control){ // if control is true (default), input comes from joystick
el15mh 1:5a44ce88c5e2 52 readJoystickInput(pad);
el15mh 1:5a44ce88c5e2 53 }
el15mh 1:5a44ce88c5e2 54 else { // if control is false, input comes from accelerometer
el15mh 1:5a44ce88c5e2 55 readAccelerometer(device);
el15mh 1:5a44ce88c5e2 56 }
el15mh 1:5a44ce88c5e2 57 }
el15mh 1:5a44ce88c5e2 58
el15mh 1:5a44ce88c5e2 59 void MazeEngine::readJoystickInput(Gamepad &pad)
el15mh 1:5a44ce88c5e2 60 {
el15mh 1:5a44ce88c5e2 61 // position is a 2D struct for (x,y)
el15mh 1:5a44ce88c5e2 62 position = pad.get_mapped_coord();
el15mh 1:5a44ce88c5e2 63
el15mh 1:5a44ce88c5e2 64 // printf("Joystick inputs: %.2f, %.2f \n", position.x, position.y);
el15mh 1:5a44ce88c5e2 65 }
el15mh 1:5a44ce88c5e2 66
el15mh 1:5a44ce88c5e2 67 void MazeEngine::readAccelerometer(FXOS8700CQ &device)
el15mh 1:5a44ce88c5e2 68 {
el15mh 1:5a44ce88c5e2 69 // acquire values from device
el15mh 1:5a44ce88c5e2 70 Data values = device.get_values();
el15mh 1:5a44ce88c5e2 71
el15mh 1:5a44ce88c5e2 72 // values are between 0 and 90°
el15mh 1:5a44ce88c5e2 73 float pitch = device.getPitchAngle();
el15mh 1:5a44ce88c5e2 74 float roll = device.getRollAngle();
el15mh 1:5a44ce88c5e2 75
el15mh 3:4b82cb2e0618 76 position.x = pitch / -30; // divide by 30 for increase sensitivity
el15mh 3:4b82cb2e0618 77 position.y = roll / -30;
el15mh 0:afee1085c5ef 78 }
el15mh 0:afee1085c5ef 79
el15mh 1:5a44ce88c5e2 80 void MazeEngine::checkWallCollision(N5110 &lcd)
el15mh 0:afee1085c5ef 81 {
el15mh 1:5a44ce88c5e2 82 // acquire position of ball
el15mh 1:5a44ce88c5e2 83 // if position on each side of ball is a pixel
el15mh 1:5a44ce88c5e2 84 // block movement in that direction
el15mh 1:5a44ce88c5e2 85
el15mh 1:5a44ce88c5e2 86 Vector2D _position = _ball.getPosition();
el15mh 1:5a44ce88c5e2 87 Vector2D _velocity = _ball.getVelocity();
el15mh 3:4b82cb2e0618 88
el15mh 3:4b82cb2e0618 89 // Vector2D position;
el15mh 3:4b82cb2e0618 90
el15mh 2:cbce5d35e7d6 91 // printf("position = (%f, %f) \n", _position.x, _position.y);
el15mh 1:5a44ce88c5e2 92
el15mh 2:cbce5d35e7d6 93 int leftSide = int(_position.x - _radius);
el15mh 2:cbce5d35e7d6 94 int rightSide = int(_position.x + _radius);
el15mh 2:cbce5d35e7d6 95 int topSide = int(_position.y - _radius);
el15mh 2:cbce5d35e7d6 96 int lowerSide = int (_position.y + _radius);
el15mh 1:5a44ce88c5e2 97
el15mh 2:cbce5d35e7d6 98 // check position of ball
el15mh 2:cbce5d35e7d6 99 // if distance between outermost pixel and centre changes
el15mh 2:cbce5d35e7d6 100 // there must be a pixel in adjacent position
el15mh 2:cbce5d35e7d6 101 if (leftSide + _mazeArray[leftSide - 1][int(_position.y)] != leftSide){
el15mh 2:cbce5d35e7d6 102
el15mh 2:cbce5d35e7d6 103 // set velocity to 0
el15mh 2:cbce5d35e7d6 104 _velocity.x = 0;
el15mh 2:cbce5d35e7d6 105 // ball can't move any further left
el15mh 2:cbce5d35e7d6 106 _position.x += 1;
el15mh 2:cbce5d35e7d6 107 printf("left side hit \n");
el15mh 1:5a44ce88c5e2 108 }
el15mh 0:afee1085c5ef 109
el15mh 2:cbce5d35e7d6 110 if (rightSide + _mazeArray[rightSide + 1][int(_position.y)] != rightSide){
el15mh 2:cbce5d35e7d6 111
el15mh 2:cbce5d35e7d6 112 _velocity.x = 0;
el15mh 2:cbce5d35e7d6 113 _position.x -= 1;
el15mh 2:cbce5d35e7d6 114 printf("right side hit \n");
el15mh 1:5a44ce88c5e2 115 }
el15mh 1:5a44ce88c5e2 116
el15mh 2:cbce5d35e7d6 117
el15mh 2:cbce5d35e7d6 118 if (topSide + _mazeArray[int(_position.x)][topSide - 1] != topSide){
el15mh 2:cbce5d35e7d6 119
el15mh 1:5a44ce88c5e2 120 _velocity.y = 0;
el15mh 2:cbce5d35e7d6 121 _position.y += 1;
el15mh 2:cbce5d35e7d6 122 printf("top side hit \n");
el15mh 1:5a44ce88c5e2 123 }
el15mh 1:5a44ce88c5e2 124
el15mh 2:cbce5d35e7d6 125 if (lowerSide + _mazeArray[int(_position.x)][lowerSide + 1] != lowerSide){
el15mh 2:cbce5d35e7d6 126
el15mh 1:5a44ce88c5e2 127 _velocity.y = 0;
el15mh 2:cbce5d35e7d6 128 _position.y -= 1;
el15mh 2:cbce5d35e7d6 129 printf("low side hit \n");
el15mh 1:5a44ce88c5e2 130 }
el15mh 1:5a44ce88c5e2 131
el15mh 2:cbce5d35e7d6 132 _ball.setPosition(_position);
el15mh 1:5a44ce88c5e2 133 _ball.setVelocity(_velocity);
el15mh 1:5a44ce88c5e2 134 }
el15mh 1:5a44ce88c5e2 135
el15mh 1:5a44ce88c5e2 136
el15mh 1:5a44ce88c5e2 137 void MazeEngine::update(N5110 &lcd)
el15mh 1:5a44ce88c5e2 138 {
el15mh 3:4b82cb2e0618 139 // check if ball is in goal before moving to next position
el15mh 3:4b82cb2e0618 140 if (checkGoal()){
el15mh 3:4b82cb2e0618 141
el15mh 3:4b82cb2e0618 142 return;
el15mh 3:4b82cb2e0618 143 }
el15mh 3:4b82cb2e0618 144
el15mh 1:5a44ce88c5e2 145 // changes the location of the ball according to inputs
el15mh 1:5a44ce88c5e2 146 _ball.update(position);
el15mh 1:5a44ce88c5e2 147
el15mh 1:5a44ce88c5e2 148 // reverts the changes if the proposed new position sends ball
el15mh 1:5a44ce88c5e2 149 // into the wall
el15mh 3:4b82cb2e0618 150 checkWallCollision(lcd);
el15mh 3:4b82cb2e0618 151
el15mh 2:cbce5d35e7d6 152 }
el15mh 2:cbce5d35e7d6 153
el15mh 2:cbce5d35e7d6 154 void MazeEngine::getMazeArray(N5110 &lcd)
el15mh 2:cbce5d35e7d6 155 {
el15mh 2:cbce5d35e7d6 156 for (int i = 0; i < 84; i++){
el15mh 2:cbce5d35e7d6 157 for (int j = 0; j < 48; j++){
el15mh 2:cbce5d35e7d6 158
el15mh 2:cbce5d35e7d6 159 int pixel = lcd.getPixel(i, j);
el15mh 2:cbce5d35e7d6 160
el15mh 2:cbce5d35e7d6 161 if (pixel == 1){
el15mh 2:cbce5d35e7d6 162
el15mh 2:cbce5d35e7d6 163 _mazeArray[i][j] = 1;
el15mh 2:cbce5d35e7d6 164 }
el15mh 2:cbce5d35e7d6 165 else {
el15mh 2:cbce5d35e7d6 166
el15mh 2:cbce5d35e7d6 167 _mazeArray[i][j] = 0;
el15mh 2:cbce5d35e7d6 168 }
el15mh 2:cbce5d35e7d6 169 }
el15mh 2:cbce5d35e7d6 170
el15mh 2:cbce5d35e7d6 171 }
el15mh 0:afee1085c5ef 172 }
el15mh 0:afee1085c5ef 173
el15mh 0:afee1085c5ef 174 void MazeEngine::draw(N5110 &lcd)
el15mh 0:afee1085c5ef 175 {
el15mh 3:4b82cb2e0618 176 if (checkGoal()){
el15mh 3:4b82cb2e0618 177
el15mh 3:4b82cb2e0618 178 lcd.printString(" MAZE", 0, 1);
el15mh 3:4b82cb2e0618 179 lcd.printString(" COMPLETED", 0, 2);
el15mh 3:4b82cb2e0618 180
el15mh 3:4b82cb2e0618 181 wait(2);
el15mh 3:4b82cb2e0618 182
el15mh 3:4b82cb2e0618 183 return;
el15mh 3:4b82cb2e0618 184 }
el15mh 3:4b82cb2e0618 185
el15mh 2:cbce5d35e7d6 186 _maze.draw(lcd);
el15mh 2:cbce5d35e7d6 187 getMazeArray(lcd);
el15mh 2:cbce5d35e7d6 188
el15mh 3:4b82cb2e0618 189 _maze.drawGoal(lcd);
el15mh 0:afee1085c5ef 190 _ball.draw(lcd);
el15mh 0:afee1085c5ef 191 }
el15mh 0:afee1085c5ef 192
el15mh 3:4b82cb2e0618 193 bool MazeEngine::checkGoal()
el15mh 3:4b82cb2e0618 194 {
el15mh 3:4b82cb2e0618 195 Vector2D position = _ball.getPosition();
el15mh 3:4b82cb2e0618 196
el15mh 3:4b82cb2e0618 197 // check for each maze index centre of ball according to location of goal on each map
el15mh 3:4b82cb2e0618 198 if (_mazeIndex == 0){
el15mh 3:4b82cb2e0618 199
el15mh 3:4b82cb2e0618 200 // check goal region
el15mh 3:4b82cb2e0618 201 for (int i = 78; i < 81; i++){
el15mh 3:4b82cb2e0618 202 for (int j = 42; j < 45; j++){
el15mh 3:4b82cb2e0618 203
el15mh 3:4b82cb2e0618 204 if ((position.x == i) && (position.y == j)){
el15mh 3:4b82cb2e0618 205
el15mh 3:4b82cb2e0618 206 printf("Goal reached");
el15mh 3:4b82cb2e0618 207 _goal = true;
el15mh 3:4b82cb2e0618 208 }
el15mh 3:4b82cb2e0618 209 }
el15mh 3:4b82cb2e0618 210 }
el15mh 3:4b82cb2e0618 211 }
el15mh 3:4b82cb2e0618 212
el15mh 3:4b82cb2e0618 213 else if (_mazeIndex == 1){
el15mh 3:4b82cb2e0618 214
el15mh 3:4b82cb2e0618 215 // check goal region
el15mh 3:4b82cb2e0618 216 for (int i = 77; i < 80; i++){
el15mh 3:4b82cb2e0618 217 for (int j = 3; j < 7; j++){
el15mh 3:4b82cb2e0618 218
el15mh 3:4b82cb2e0618 219 if ((position.x == i) && (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 3:4b82cb2e0618 224 }
el15mh 3:4b82cb2e0618 225 }
el15mh 3:4b82cb2e0618 226
el15mh 3:4b82cb2e0618 227 }
el15mh 3:4b82cb2e0618 228
el15mh 3:4b82cb2e0618 229 else if (_mazeIndex == 2){
el15mh 3:4b82cb2e0618 230
el15mh 3:4b82cb2e0618 231 // check goal region
el15mh 3:4b82cb2e0618 232 for (int i = 77; i < 80; i++){
el15mh 3:4b82cb2e0618 233 for (int j = 3; j < 6; j++){
el15mh 3:4b82cb2e0618 234
el15mh 3:4b82cb2e0618 235 if ((position.x == i) && (position.y == j)){
el15mh 3:4b82cb2e0618 236
el15mh 3:4b82cb2e0618 237 printf("Goal reached");
el15mh 3:4b82cb2e0618 238 _goal = true;
el15mh 3:4b82cb2e0618 239 }
el15mh 3:4b82cb2e0618 240 }
el15mh 3:4b82cb2e0618 241 }
el15mh 3:4b82cb2e0618 242
el15mh 3:4b82cb2e0618 243 }
el15mh 3:4b82cb2e0618 244
el15mh 3:4b82cb2e0618 245 else if (_mazeIndex == 3){
el15mh 3:4b82cb2e0618 246
el15mh 3:4b82cb2e0618 247 // check goal region
el15mh 3:4b82cb2e0618 248 for (int i = 76; i < 79; i++){
el15mh 3:4b82cb2e0618 249 for (int j = 40; j < 43; j++){
el15mh 3:4b82cb2e0618 250
el15mh 3:4b82cb2e0618 251 if ((position.x == i) && (position.y == j)){
el15mh 3:4b82cb2e0618 252
el15mh 3:4b82cb2e0618 253 printf("Goal reached");
el15mh 3:4b82cb2e0618 254 _goal = true;
el15mh 3:4b82cb2e0618 255 }
el15mh 3:4b82cb2e0618 256 }
el15mh 3:4b82cb2e0618 257 }
el15mh 3:4b82cb2e0618 258 }
el15mh 3:4b82cb2e0618 259
el15mh 3:4b82cb2e0618 260 else {
el15mh 3:4b82cb2e0618 261
el15mh 3:4b82cb2e0618 262 // check goal region
el15mh 3:4b82cb2e0618 263 for (int i = 42; i < 44; i++){
el15mh 3:4b82cb2e0618 264 for (int j = 23; j < 25; j++){
el15mh 3:4b82cb2e0618 265
el15mh 3:4b82cb2e0618 266 if ((position.x == i) && (position.y == j)){
el15mh 3:4b82cb2e0618 267
el15mh 3:4b82cb2e0618 268 printf("Goal reached");
el15mh 3:4b82cb2e0618 269 _goal = true;
el15mh 3:4b82cb2e0618 270 }
el15mh 3:4b82cb2e0618 271 }
el15mh 3:4b82cb2e0618 272 }
el15mh 3:4b82cb2e0618 273
el15mh 3:4b82cb2e0618 274 }
el15mh 3:4b82cb2e0618 275
el15mh 3:4b82cb2e0618 276 return _goal;
el15mh 3:4b82cb2e0618 277 }
el15mh 3:4b82cb2e0618 278
el15mh 3:4b82cb2e0618 279
el15mh 3:4b82cb2e0618 280
el15mh 3:4b82cb2e0618 281
el15mh 3:4b82cb2e0618 282