el15mh 200929957

Dependencies:   mbed

Committer:
el15mh
Date:
Thu May 04 17:39:23 2017 +0000
Revision:
10:989e5dbd12ee
Parent:
9:960dfc71c224
Documented and final revision

Who changed what in which revision?

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