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 5:3635319c64b8 1 /** Maze Engine class
el15mh 5:3635319c64b8 2 @brief Class runs the game loop, drawing the mazes and checking for wall collisions and goals.
el15mh 5:3635319c64b8 3 @author Max Houghton
el15mh 5:3635319c64b8 4 @date March 3 2017
el15mh 1:5a44ce88c5e2 5 */
el15mh 0:afee1085c5ef 6
el15mh 0:afee1085c5ef 7 #ifndef MAZEENGINE_H
el15mh 0:afee1085c5ef 8 #define MAZEENGINE_H
el15mh 0:afee1085c5ef 9
el15mh 0:afee1085c5ef 10 #include "mbed.h"
el15mh 1:5a44ce88c5e2 11 #include "math.h"
el15mh 0:afee1085c5ef 12 #include "N5110.h"
el15mh 0:afee1085c5ef 13 #include "Gamepad.h"
el15mh 0:afee1085c5ef 14 #include "FXOS8700CQ.h"
el15mh 0:afee1085c5ef 15 #include "Ball.h"
el15mh 0:afee1085c5ef 16 #include "Maze.h"
el15mh 0:afee1085c5ef 17
el15mh 0:afee1085c5ef 18 class MazeEngine
el15mh 0:afee1085c5ef 19 {
el15mh 0:afee1085c5ef 20
el15mh 0:afee1085c5ef 21 public:
el15mh 0:afee1085c5ef 22
el15mh 5:3635319c64b8 23 /**
el15mh 5:3635319c64b8 24 * @details - constructor
el15mh 5:3635319c64b8 25 */
el15mh 0:afee1085c5ef 26 MazeEngine();
el15mh 5:3635319c64b8 27
el15mh 5:3635319c64b8 28 /**
el15mh 5:3635319c64b8 29 * @details - destructor
el15mh 5:3635319c64b8 30 */
el15mh 0:afee1085c5ef 31 ~MazeEngine();
el15mh 0:afee1085c5ef 32
el15mh 5:3635319c64b8 33 /** Initiase Game
el15mh 5:3635319c64b8 34 *
el15mh 5:3635319c64b8 35 * @details - Initiases the game with the appropriate different parameters chosen by user
el15mh 5:3635319c64b8 36 * @param - mazeIndex - selects the maze to be drawn
el15mh 5:3635319c64b8 37 * @param - x - specifies the x coordinate for the starting position of the ball
el15mh 5:3635319c64b8 38 * @param - y - specifies the y coordinate for the starting position of the ball
el15mh 5:3635319c64b8 39 * @param - radius - specifies size of the ball to be drawn
el15mh 5:3635319c64b8 40 * @param - control - selects the ball control method; joystick or accelerometer
el15mh 5:3635319c64b8 41 * @param - colour - specifies if the ball is either transparent or filled
el15mh 5:3635319c64b8 42 */
el15mh 1:5a44ce88c5e2 43 void init(int mazeIndex, // sets the difficulty of the maze
el15mh 1:5a44ce88c5e2 44 int x, // x coordinate of ball centre
el15mh 1:5a44ce88c5e2 45 int y, // y coordinate of ball centre
el15mh 1:5a44ce88c5e2 46 int radius, // radius of the ball
el15mh 1:5a44ce88c5e2 47 bool control, // option for controlling ball
el15mh 1:5a44ce88c5e2 48 bool colour); // option for style of ball
el15mh 1:5a44ce88c5e2 49
el15mh 5:3635319c64b8 50 /** Read Input
el15mh 5:3635319c64b8 51 *
el15mh 5:3635319c64b8 52 * @details - Reads the input from either the joystick or the accelerometer. If (boolean) control value is true, joystick is used, otherwise the accelerometer is used.
el15mh 5:3635319c64b8 53 * @param - pad - Gamepad Library.
el15mh 5:3635319c64b8 54 * @param - device - FXOS8700CQ Library.
el15mh 5:3635319c64b8 55 */
el15mh 0:afee1085c5ef 56 void readInput(Gamepad &pad, FXOS8700CQ &device);
el15mh 5:3635319c64b8 57
el15mh 5:3635319c64b8 58 /** Read Joystick Input
el15mh 5:3635319c64b8 59 *
el15mh 5:3635319c64b8 60 * @param - pad - Gamepad Library.
el15mh 5:3635319c64b8 61 * 2D Vector position (struct) is filled with (x,y) values using get_mapped_coord()
el15mh 5:3635319c64b8 62 * function from Gamepad library.
el15mh 5:3635319c64b8 63 * Values are between -1 and 1.
el15mh 5:3635319c64b8 64 * These are they used as the ball's position in the next update of the loop.
el15mh 5:3635319c64b8 65 */
el15mh 1:5a44ce88c5e2 66 void readJoystickInput(Gamepad &pad);
el15mh 5:3635319c64b8 67
el15mh 5:3635319c64b8 68 /** Read Accelerometer
el15mh 5:3635319c64b8 69 *
el15mh 5:3635319c64b8 70 * @details - Struct 'Data' is filled with values using get_values() function from FXOS8700CQ library. These data values are then used to calculate the pitch and roll for the device. Pitch and roll are both floats between -1 and 1. Pitch value equates to motion in the x axis, and roll in the y axis.
el15mh 5:3635319c64b8 71 * @param - device - the FXOS8700CQ Library.
el15mh 5:3635319c64b8 72 */
el15mh 1:5a44ce88c5e2 73 void readAccelerometer(FXOS8700CQ &device);
el15mh 5:3635319c64b8 74
el15mh 5:3635319c64b8 75 /** Update
el15mh 5:3635319c64b8 76 *
el15mh 5:3635319c64b8 77 * @details - Updates the game using the position parameters found previously. Initially checks for goal, if so, quit function. If no goal, the position of the ball is updated and the space around the ball is checked for the next loop.
el15mh 5:3635319c64b8 78 * @param - position - 2D Vector struct holding ball position.
el15mh 5:3635319c64b8 79 */
el15mh 1:5a44ce88c5e2 80 void update(N5110 &lcd);
el15mh 5:3635319c64b8 81
el15mh 5:3635319c64b8 82 /** Draw
el15mh 5:3635319c64b8 83 *
el15mh 5:3635319c64b8 84 * @details - Draws both the maze and the ball. Maze Array is used when performing wall collision checks. Acquired prior to drawing the ball to avoid confusion between ball edges and walls.
el15mh 5:3635319c64b8 85 * @param - lcd - N5110 Library.
el15mh 5:3635319c64b8 86 */
el15mh 0:afee1085c5ef 87 void draw(N5110 &lcd);
el15mh 3:4b82cb2e0618 88
el15mh 5:3635319c64b8 89 /** Check Wall Collision
el15mh 5:3635319c64b8 90 *
el15mh 5:3635319c64b8 91 * @details - Checks the position of the ball using the getPosition() function from the ball class. The distance between the centre and the outermost turned on pixel is calculated. If the value calculated is different than the radius value, there must be a pixel. Therefore stop movement in that direction.
el15mh 5:3635319c64b8 92 * @param - lcd - N5110 Library used to create lcd buffer for maze and ball.
el15mh 5:3635319c64b8 93 */
el15mh 1:5a44ce88c5e2 94 void checkWallCollision(N5110 &lcd);
el15mh 5:3635319c64b8 95
el15mh 5:3635319c64b8 96 /** Check Goal
el15mh 5:3635319c64b8 97 *
el15mh 5:3635319c64b8 98 * @details - Position of the ball is acquired using getPosition() function from Ball class. For extreme maze (index 9), goal location is at centre of maze. For all other mazes, goal is achieved when x coordinate of centre of ball exceeds (+ radius) the maximum value of the LCD buffer, 83. (i.e it has gone off the screen)
el15mh 5:3635319c64b8 99 * @param - lcd - N5110 Library.
el15mh 5:3635319c64b8 100 */
el15mh 4:93fd2f3bd0de 101 bool checkGoal();
el15mh 0:afee1085c5ef 102
el15mh 5:3635319c64b8 103 /** Get Maze Array
el15mh 5:3635319c64b8 104 *
el15mh 5:3635319c64b8 105 * @details - Array used for wall collision checking is aquired. Function scans through each pixel in the LCD and sets value in corresponding array to 1 if the pixel is turned on
el15mh 5:3635319c64b8 106 * @param - lcd - N5110 Library.
el15mh 5:3635319c64b8 107 */
el15mh 2:cbce5d35e7d6 108 void getMazeArray(N5110 &lcd);
el15mh 2:cbce5d35e7d6 109
el15mh 5:3635319c64b8 110 /** Goal
el15mh 5:3635319c64b8 111 *
el15mh 5:3635319c64b8 112 * @param - _goal - Boolean variable stating whether the goal has been achieved or not.
el15mh 5:3635319c64b8 113 */
el15mh 3:4b82cb2e0618 114 bool _goal;
el15mh 3:4b82cb2e0618 115
el15mh 0:afee1085c5ef 116 private:
el15mh 0:afee1085c5ef 117
el15mh 5:3635319c64b8 118 /**
el15mh 5:3635319c64b8 119 * @param - _maze - Create instance of Maze class.
el15mh 5:3635319c64b8 120 */
el15mh 0:afee1085c5ef 121 Maze _maze;
el15mh 5:3635319c64b8 122
el15mh 5:3635319c64b8 123 /**
el15mh 5:3635319c64b8 124 * @param - _ball_ - Create instance of Ball class.
el15mh 5:3635319c64b8 125 */
el15mh 0:afee1085c5ef 126 Ball _ball;
el15mh 0:afee1085c5ef 127
el15mh 5:3635319c64b8 128 /**
el15mh 5:3635319c64b8 129 * @param - position - 2D Vector struct storing the position of the ball.
el15mh 5:3635319c64b8 130 */
el15mh 5:3635319c64b8 131 Vector2D position;
el15mh 5:3635319c64b8 132
el15mh 5:3635319c64b8 133 /**
el15mh 5:3635319c64b8 134 * @param - _mazeIndex - Integer variable to store maze index. This is used to draw the appropriate maze.
el15mh 5:3635319c64b8 135 */
el15mh 0:afee1085c5ef 136 int _mazeIndex;
el15mh 5:3635319c64b8 137
el15mh 5:3635319c64b8 138 /**
el15mh 5:3635319c64b8 139 * @param - _x - coordinate for centre of the ball.
el15mh 5:3635319c64b8 140 */
el15mh 0:afee1085c5ef 141 int _x;
el15mh 5:3635319c64b8 142
el15mh 5:3635319c64b8 143 /**
el15mh 5:3635319c64b8 144 * @param - _y - coordinate for centre of the ball.
el15mh 5:3635319c64b8 145 */
el15mh 0:afee1085c5ef 146 int _y;
el15mh 5:3635319c64b8 147
el15mh 5:3635319c64b8 148 /**
el15mh 5:3635319c64b8 149 * @param - _radius - value of the size of the ball
el15mh 5:3635319c64b8 150 */
el15mh 0:afee1085c5ef 151 int _radius;
el15mh 0:afee1085c5ef 152
el15mh 5:3635319c64b8 153 /**
el15mh 5:3635319c64b8 154 * @param - _control - Boolean variable to state the control method. If _control is true, the joystick is used for an input, otherwise the accelerometer is used.
el15mh 5:3635319c64b8 155 */
el15mh 5:3635319c64b8 156 bool _control;
el15mh 0:afee1085c5ef 157
el15mh 5:3635319c64b8 158 /**
el15mh 5:3635319c64b8 159 * @param - _colour - Boolean variable to state the ball fill style. If _colour is true, the ball has a transparent fill, otherwise the ball has a solid fill.
el15mh 5:3635319c64b8 160 */
el15mh 1:5a44ce88c5e2 161 bool _colour;
el15mh 2:cbce5d35e7d6 162
el15mh 5:3635319c64b8 163 /**
el15mh 5:3635319c64b8 164 * @param - _mazArray[][] - 2D Float Array storing the locations of the turned on pixels. Used for wall collision checks.
el15mh 5:3635319c64b8 165 */
el15mh 2:cbce5d35e7d6 166 float _mazeArray[84][48];
el15mh 0:afee1085c5ef 167 };
el15mh 0:afee1085c5ef 168
el15mh 0:afee1085c5ef 169 #endif