el15mh 200929957

Dependencies:   mbed

Committer:
el15mh
Date:
Thu May 04 14:43:29 2017 +0000
Revision:
9:960dfc71c224
Child:
10:989e5dbd12ee
Documented using doxygen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15mh 9:960dfc71c224 1 /** Maze Engine class
el15mh 9:960dfc71c224 2 @brief Class runs the game loop, drawing the mazes and checking for wall collisions and goals.
el15mh 9:960dfc71c224 3 @author Max Houghton
el15mh 9:960dfc71c224 4 @date March 3 2017
el15mh 9:960dfc71c224 5 */
el15mh 9:960dfc71c224 6
el15mh 9:960dfc71c224 7 #ifndef MAZEENGINE_H
el15mh 9:960dfc71c224 8 #define MAZEENGINE_H
el15mh 9:960dfc71c224 9
el15mh 9:960dfc71c224 10 #include "mbed.h"
el15mh 9:960dfc71c224 11 #include "math.h"
el15mh 9:960dfc71c224 12 #include "N5110.h"
el15mh 9:960dfc71c224 13 #include "Gamepad.h"
el15mh 9:960dfc71c224 14 #include "FXOS8700CQ.h"
el15mh 9:960dfc71c224 15 #include "Ball.h"
el15mh 9:960dfc71c224 16 #include "Maze.h"
el15mh 9:960dfc71c224 17
el15mh 9:960dfc71c224 18 class MazeEngine
el15mh 9:960dfc71c224 19 {
el15mh 9:960dfc71c224 20
el15mh 9:960dfc71c224 21 public:
el15mh 9:960dfc71c224 22
el15mh 9:960dfc71c224 23 /**
el15mh 9:960dfc71c224 24 * @details - constructor
el15mh 9:960dfc71c224 25 */
el15mh 9:960dfc71c224 26 MazeEngine();
el15mh 9:960dfc71c224 27
el15mh 9:960dfc71c224 28 /**
el15mh 9:960dfc71c224 29 * @details - destructor
el15mh 9:960dfc71c224 30 */
el15mh 9:960dfc71c224 31 ~MazeEngine();
el15mh 9:960dfc71c224 32
el15mh 9:960dfc71c224 33 /** Initiase Game
el15mh 9:960dfc71c224 34 *
el15mh 9:960dfc71c224 35 * @details - Initiases the game with the appropriate different parameters chosen by user
el15mh 9:960dfc71c224 36 * @param - mazeIndex - selects the maze to be drawn
el15mh 9:960dfc71c224 37 * @param - x - specifies the x coordinate for the starting position of the ball
el15mh 9:960dfc71c224 38 * @param - y - specifies the y coordinate for the starting position of the ball
el15mh 9:960dfc71c224 39 * @param - radius - specifies size of the ball to be drawn
el15mh 9:960dfc71c224 40 * @param - control - selects the ball control method; joystick or accelerometer
el15mh 9:960dfc71c224 41 * @param - colour - specifies if the ball is either transparent or filled
el15mh 9:960dfc71c224 42 */
el15mh 9:960dfc71c224 43 void init(int mazeIndex, // sets the difficulty of the maze
el15mh 9:960dfc71c224 44 int x, // x coordinate of ball centre
el15mh 9:960dfc71c224 45 int y, // y coordinate of ball centre
el15mh 9:960dfc71c224 46 int radius, // radius of the ball
el15mh 9:960dfc71c224 47 bool control, // option for controlling ball
el15mh 9:960dfc71c224 48 bool colour); // option for style of ball
el15mh 9:960dfc71c224 49
el15mh 9:960dfc71c224 50 /** Read Input
el15mh 9:960dfc71c224 51 *
el15mh 9:960dfc71c224 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 9:960dfc71c224 53 * @param - pad - Gamepad Library.
el15mh 9:960dfc71c224 54 * @param - device - FXOS8700CQ Library.
el15mh 9:960dfc71c224 55 */
el15mh 9:960dfc71c224 56 void readInput(Gamepad &pad, FXOS8700CQ &device);
el15mh 9:960dfc71c224 57
el15mh 9:960dfc71c224 58 /** Read Joystick Input
el15mh 9:960dfc71c224 59 *
el15mh 9:960dfc71c224 60 * @param - pad - Gamepad Library.
el15mh 9:960dfc71c224 61 * 2D Vector position (struct) is filled with (x,y) values using get_mapped_coord()
el15mh 9:960dfc71c224 62 * function from Gamepad library.
el15mh 9:960dfc71c224 63 * Values are between -1 and 1.
el15mh 9:960dfc71c224 64 * These are they used as the ball's position in the next update of the loop.
el15mh 9:960dfc71c224 65 */
el15mh 9:960dfc71c224 66 void readJoystickInput(Gamepad &pad);
el15mh 9:960dfc71c224 67
el15mh 9:960dfc71c224 68 /** Read Accelerometer
el15mh 9:960dfc71c224 69 *
el15mh 9:960dfc71c224 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 9:960dfc71c224 71 * @param - device - the FXOS8700CQ Library.
el15mh 9:960dfc71c224 72 */
el15mh 9:960dfc71c224 73 void readAccelerometer(FXOS8700CQ &device);
el15mh 9:960dfc71c224 74
el15mh 9:960dfc71c224 75 /** Update
el15mh 9:960dfc71c224 76 *
el15mh 9:960dfc71c224 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 9:960dfc71c224 78 * @param - position - 2D Vector struct holding ball position.
el15mh 9:960dfc71c224 79 */
el15mh 9:960dfc71c224 80 void update(N5110 &lcd);
el15mh 9:960dfc71c224 81
el15mh 9:960dfc71c224 82 /** Draw
el15mh 9:960dfc71c224 83 *
el15mh 9:960dfc71c224 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 9:960dfc71c224 85 * @param - lcd - N5110 Library.
el15mh 9:960dfc71c224 86 */
el15mh 9:960dfc71c224 87 void draw(N5110 &lcd);
el15mh 9:960dfc71c224 88
el15mh 9:960dfc71c224 89 /** Check Wall Collision
el15mh 9:960dfc71c224 90 *
el15mh 9:960dfc71c224 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 9:960dfc71c224 92 * @param - lcd - N5110 Library used to create lcd buffer for maze and ball.
el15mh 9:960dfc71c224 93 */
el15mh 9:960dfc71c224 94 void checkWallCollision(N5110 &lcd);
el15mh 9:960dfc71c224 95
el15mh 9:960dfc71c224 96 /** Check Goal
el15mh 9:960dfc71c224 97 *
el15mh 9:960dfc71c224 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 9:960dfc71c224 99 * @param - lcd - N5110 Library.
el15mh 9:960dfc71c224 100 */
el15mh 9:960dfc71c224 101 bool checkGoal();
el15mh 9:960dfc71c224 102
el15mh 9:960dfc71c224 103 /** Get Maze Array
el15mh 9:960dfc71c224 104 *
el15mh 9:960dfc71c224 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 9:960dfc71c224 106 * @param - lcd - N5110 Library.
el15mh 9:960dfc71c224 107 */
el15mh 9:960dfc71c224 108 void getMazeArray(N5110 &lcd);
el15mh 9:960dfc71c224 109
el15mh 9:960dfc71c224 110 /** Goal
el15mh 9:960dfc71c224 111 *
el15mh 9:960dfc71c224 112 * @param - _goal - Boolean variable stating whether the goal has been achieved or not.
el15mh 9:960dfc71c224 113 */
el15mh 9:960dfc71c224 114 bool _goal;
el15mh 9:960dfc71c224 115
el15mh 9:960dfc71c224 116 private:
el15mh 9:960dfc71c224 117
el15mh 9:960dfc71c224 118 /**
el15mh 9:960dfc71c224 119 * @param - _maze - Create instance of Maze class.
el15mh 9:960dfc71c224 120 */
el15mh 9:960dfc71c224 121 Maze _maze;
el15mh 9:960dfc71c224 122
el15mh 9:960dfc71c224 123 /**
el15mh 9:960dfc71c224 124 * @param - _ball_ - Create instance of Ball class.
el15mh 9:960dfc71c224 125 */
el15mh 9:960dfc71c224 126 Ball _ball;
el15mh 9:960dfc71c224 127
el15mh 9:960dfc71c224 128 /**
el15mh 9:960dfc71c224 129 * @param - position - 2D Vector struct storing the position of the ball.
el15mh 9:960dfc71c224 130 */
el15mh 9:960dfc71c224 131 Vector2D position;
el15mh 9:960dfc71c224 132
el15mh 9:960dfc71c224 133 /**
el15mh 9:960dfc71c224 134 * @param - _mazeIndex - Integer variable to store maze index. This is used to draw the appropriate maze.
el15mh 9:960dfc71c224 135 */
el15mh 9:960dfc71c224 136 int _mazeIndex;
el15mh 9:960dfc71c224 137
el15mh 9:960dfc71c224 138 /**
el15mh 9:960dfc71c224 139 * @param - _x - coordinate for centre of the ball.
el15mh 9:960dfc71c224 140 */
el15mh 9:960dfc71c224 141 int _x;
el15mh 9:960dfc71c224 142
el15mh 9:960dfc71c224 143 /**
el15mh 9:960dfc71c224 144 * @param - _y - coordinate for centre of the ball.
el15mh 9:960dfc71c224 145 */
el15mh 9:960dfc71c224 146 int _y;
el15mh 9:960dfc71c224 147
el15mh 9:960dfc71c224 148 /**
el15mh 9:960dfc71c224 149 * @param - _radius - value of the size of the ball
el15mh 9:960dfc71c224 150 */
el15mh 9:960dfc71c224 151 int _radius;
el15mh 9:960dfc71c224 152
el15mh 9:960dfc71c224 153 /**
el15mh 9:960dfc71c224 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 9:960dfc71c224 155 */
el15mh 9:960dfc71c224 156 bool _control;
el15mh 9:960dfc71c224 157
el15mh 9:960dfc71c224 158 /**
el15mh 9:960dfc71c224 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 9:960dfc71c224 160 */
el15mh 9:960dfc71c224 161 bool _colour;
el15mh 9:960dfc71c224 162
el15mh 9:960dfc71c224 163 /**
el15mh 9:960dfc71c224 164 * @param - _mazArray[][] - 2D Float Array storing the locations of the turned on pixels. Used for wall collision checks.
el15mh 9:960dfc71c224 165 */
el15mh 9:960dfc71c224 166 float _mazeArray[84][48];
el15mh 9:960dfc71c224 167 };
el15mh 9:960dfc71c224 168
el15mh 9:960dfc71c224 169 #endif