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 #ifndef MAZEENGINE_H
el15mh 9:960dfc71c224 2 #define MAZEENGINE_H
el15mh 9:960dfc71c224 3
el15mh 9:960dfc71c224 4 #include "mbed.h"
el15mh 9:960dfc71c224 5 #include "math.h"
el15mh 9:960dfc71c224 6 #include "N5110.h"
el15mh 9:960dfc71c224 7 #include "Gamepad.h"
el15mh 9:960dfc71c224 8 #include "FXOS8700CQ.h"
el15mh 9:960dfc71c224 9 #include "Ball.h"
el15mh 9:960dfc71c224 10 #include "Maze.h"
el15mh 9:960dfc71c224 11
el15mh 10:989e5dbd12ee 12 /**
el15mh 10:989e5dbd12ee 13 @brief Maze Engine Class runs the game loop, drawing the mazes and checking for wall collisions and goals.
el15mh 10:989e5dbd12ee 14 @author Max Houghton
el15mh 10:989e5dbd12ee 15 @date March 3 2017
el15mh 10:989e5dbd12ee 16 */
el15mh 10:989e5dbd12ee 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 10:989e5dbd12ee 36 * @param mazeIndex - selects the maze to be drawn
el15mh 10:989e5dbd12ee 37 * @param x - specifies the x coordinate for the starting position of the ball
el15mh 10:989e5dbd12ee 38 * @param y - specifies the y coordinate for the starting position of the ball
el15mh 10:989e5dbd12ee 39 * @param radius - specifies size of the ball to be drawn
el15mh 10:989e5dbd12ee 40 * @param control - selects the ball control method; joystick or accelerometer
el15mh 10:989e5dbd12ee 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 10:989e5dbd12ee 53 * @param pad - Gamepad Library.
el15mh 10:989e5dbd12ee 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 10:989e5dbd12ee 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 10:989e5dbd12ee 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 10:989e5dbd12ee 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 10:989e5dbd12ee 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 10:989e5dbd12ee 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 10:989e5dbd12ee 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 10:989e5dbd12ee 106 * @param lcd - N5110 Library.
el15mh 10:989e5dbd12ee 107 * @return _goal
el15mh 9:960dfc71c224 108 */
el15mh 9:960dfc71c224 109 void getMazeArray(N5110 &lcd);
el15mh 9:960dfc71c224 110
el15mh 9:960dfc71c224 111 /** Goal
el15mh 9:960dfc71c224 112 *
el15mh 10:989e5dbd12ee 113 * @param _goal - Boolean variable stating whether the goal has been achieved or not.
el15mh 9:960dfc71c224 114 */
el15mh 9:960dfc71c224 115 bool _goal;
el15mh 9:960dfc71c224 116
el15mh 9:960dfc71c224 117 private:
el15mh 9:960dfc71c224 118
el15mh 9:960dfc71c224 119 /**
el15mh 10:989e5dbd12ee 120 * @param _maze - Create instance of Maze class.
el15mh 9:960dfc71c224 121 */
el15mh 9:960dfc71c224 122 Maze _maze;
el15mh 9:960dfc71c224 123
el15mh 9:960dfc71c224 124 /**
el15mh 10:989e5dbd12ee 125 * @param _ball_ - Create instance of Ball class.
el15mh 9:960dfc71c224 126 */
el15mh 9:960dfc71c224 127 Ball _ball;
el15mh 9:960dfc71c224 128
el15mh 9:960dfc71c224 129 /**
el15mh 10:989e5dbd12ee 130 * @param position - 2D Vector struct storing the position of the ball.
el15mh 9:960dfc71c224 131 */
el15mh 9:960dfc71c224 132 Vector2D position;
el15mh 9:960dfc71c224 133
el15mh 9:960dfc71c224 134 /**
el15mh 10:989e5dbd12ee 135 * @param _mazeIndex - Integer variable to store maze index. This is used to draw the appropriate maze.
el15mh 9:960dfc71c224 136 */
el15mh 9:960dfc71c224 137 int _mazeIndex;
el15mh 9:960dfc71c224 138
el15mh 9:960dfc71c224 139 /**
el15mh 10:989e5dbd12ee 140 * @param _x - coordinate for centre of the ball.
el15mh 9:960dfc71c224 141 */
el15mh 9:960dfc71c224 142 int _x;
el15mh 9:960dfc71c224 143
el15mh 9:960dfc71c224 144 /**
el15mh 10:989e5dbd12ee 145 * @param _y - coordinate for centre of the ball.
el15mh 9:960dfc71c224 146 */
el15mh 9:960dfc71c224 147 int _y;
el15mh 9:960dfc71c224 148
el15mh 9:960dfc71c224 149 /**
el15mh 10:989e5dbd12ee 150 * @param _radius - value of the size of the ball
el15mh 9:960dfc71c224 151 */
el15mh 9:960dfc71c224 152 int _radius;
el15mh 9:960dfc71c224 153
el15mh 9:960dfc71c224 154 /**
el15mh 10:989e5dbd12ee 155 * @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 156 */
el15mh 9:960dfc71c224 157 bool _control;
el15mh 9:960dfc71c224 158
el15mh 9:960dfc71c224 159 /**
el15mh 10:989e5dbd12ee 160 * @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 161 */
el15mh 9:960dfc71c224 162 bool _colour;
el15mh 9:960dfc71c224 163
el15mh 9:960dfc71c224 164 /**
el15mh 10:989e5dbd12ee 165 * @param _mazArray[][] - 2D Float Array storing the locations of the turned on pixels. Used for wall collision checks.
el15mh 9:960dfc71c224 166 */
el15mh 9:960dfc71c224 167 float _mazeArray[84][48];
el15mh 9:960dfc71c224 168 };
el15mh 9:960dfc71c224 169
el15mh 9:960dfc71c224 170 #endif