Class used to run the maze game loop.
Revision 5:3635319c64b8, committed 2017-05-04
- Comitter:
- el15mh
- Date:
- Thu May 04 14:02:05 2017 +0000
- Parent:
- 4:93fd2f3bd0de
- Commit message:
- Doxygen commenting added
Changed in this revision
MazeEngine.cpp | Show annotated file Show diff for this revision Revisions of this file |
MazeEngine.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 93fd2f3bd0de -r 3635319c64b8 MazeEngine.cpp --- a/MazeEngine.cpp Wed May 03 21:13:52 2017 +0000 +++ b/MazeEngine.cpp Thu May 04 14:02:05 2017 +0000 @@ -1,24 +1,24 @@ -/* - - @file mazeEngine.cpp - - (c) Max Houghton 19.03.2017 - Roller Maze Project, ELEC2645, Univeristy of Leeds - - */ - #include "MazeEngine.h" +/** + * @details - constructor + */ MazeEngine::MazeEngine() { } +/** + * @details - constructor + */ MazeEngine::~MazeEngine() { } +/** + * @details - Initialises the game with specified paramters + */ void MazeEngine::init(int mazeIndex, int x, int y, @@ -46,6 +46,9 @@ _maze.init(_mazeIndex); } +/** + * @details - Reads input from chosen device + */ void MazeEngine::readInput(Gamepad &pad, FXOS8700CQ &device) { if (_control){ // if control is true (default), input comes from joystick @@ -56,6 +59,9 @@ } } +/** + * @details - Reads joystick input + */ void MazeEngine::readJoystickInput(Gamepad &pad) { // position is a 2D struct for (x,y) @@ -64,6 +70,9 @@ // printf("Joystick inputs: %.2f, %.2f \n", position.x, position.y); } +/** + * @details - Reads accelerometer input + */ void MazeEngine::readAccelerometer(FXOS8700CQ &device) { // acquire values from device @@ -77,6 +86,9 @@ position.y = roll / -60; } +/** + * @details - Checks for wall collisions + */ void MazeEngine::checkWallCollision(N5110 &lcd) { // acquire position of ball @@ -94,7 +106,7 @@ int lowerSide = int (_position.y + _radius); // check position of ball - // if distance between outermost pixel and centre changes + // if distance between outermost pixel and centre != original distance // there must be a pixel in adjacent position if (leftSide + _mazeArray[leftSide - 1][int(_position.y)] != leftSide){ @@ -131,7 +143,9 @@ _ball.setVelocity(_velocity); } - +/** + * @details - updates the game + */ void MazeEngine::update(N5110 &lcd) { // check if ball is in goal before moving to next position @@ -150,6 +164,9 @@ } +/** + * @details - Acquires maze array used for wall collision checks + */ void MazeEngine::getMazeArray(N5110 &lcd) { for (int i = 0; i < 84; i++){ @@ -170,6 +187,9 @@ } } +/** +* @details - Draws ball and maze +*/ void MazeEngine::draw(N5110 &lcd) { // mazes are drawn according to selected maze index (default = 0) @@ -180,6 +200,9 @@ _ball.draw(lcd); } +/** + * @details - Checks if goal has been achieved. + */ bool MazeEngine::checkGoal() { Vector2D position = _ball.getPosition(); @@ -205,6 +228,7 @@ } else { + if (position.x > 86){ // printf("Goal reached");
diff -r 93fd2f3bd0de -r 3635319c64b8 MazeEngine.h --- a/MazeEngine.h Wed May 03 21:13:52 2017 +0000 +++ b/MazeEngine.h Thu May 04 14:02:05 2017 +0000 @@ -1,10 +1,7 @@ -/* - - @file mazeEngine.h - - (c) Max Houghton 19.03.2017 - Roller Maze Project, ELEC2645, Univeristy of Leeds - +/** Maze Engine class + @brief Class runs the game loop, drawing the mazes and checking for wall collisions and goals. + @author Max Houghton + @date March 3 2017 */ #ifndef MAZEENGINE_H @@ -23,9 +20,26 @@ public: + /** + * @details - constructor + */ MazeEngine(); + + /** + * @details - destructor + */ ~MazeEngine(); + /** Initiase Game + * + * @details - Initiases the game with the appropriate different parameters chosen by user + * @param - mazeIndex - selects the maze to be drawn + * @param - x - specifies the x coordinate for the starting position of the ball + * @param - y - specifies the y coordinate for the starting position of the ball + * @param - radius - specifies size of the ball to be drawn + * @param - control - selects the ball control method; joystick or accelerometer + * @param - colour - specifies if the ball is either transparent or filled + */ void init(int mazeIndex, // sets the difficulty of the maze int x, // x coordinate of ball centre int y, // y coordinate of ball centre @@ -33,34 +47,122 @@ bool control, // option for controlling ball bool colour); // option for style of ball + /** Read Input + * + * @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. + * @param - pad - Gamepad Library. + * @param - device - FXOS8700CQ Library. + */ void readInput(Gamepad &pad, FXOS8700CQ &device); + + /** Read Joystick Input + * + * @param - pad - Gamepad Library. + * 2D Vector position (struct) is filled with (x,y) values using get_mapped_coord() + * function from Gamepad library. + * Values are between -1 and 1. + * These are they used as the ball's position in the next update of the loop. + */ void readJoystickInput(Gamepad &pad); + + /** Read Accelerometer + * + * @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. + * @param - device - the FXOS8700CQ Library. + */ void readAccelerometer(FXOS8700CQ &device); + + /** Update + * + * @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. + * @param - position - 2D Vector struct holding ball position. + */ void update(N5110 &lcd); + + /** Draw + * + * @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. + * @param - lcd - N5110 Library. + */ void draw(N5110 &lcd); + /** Check Wall Collision + * + * @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. + * @param - lcd - N5110 Library used to create lcd buffer for maze and ball. + */ void checkWallCollision(N5110 &lcd); + + /** Check Goal + * + * @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) + * @param - lcd - N5110 Library. + */ bool checkGoal(); + /** Get Maze Array + * + * @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 + * @param - lcd - N5110 Library. + */ void getMazeArray(N5110 &lcd); + /** Goal + * + * @param - _goal - Boolean variable stating whether the goal has been achieved or not. + */ bool _goal; private: + /** + * @param - _maze - Create instance of Maze class. + */ Maze _maze; + + /** + * @param - _ball_ - Create instance of Ball class. + */ Ball _ball; + /** + * @param - position - 2D Vector struct storing the position of the ball. + */ + Vector2D position; + + /** + * @param - _mazeIndex - Integer variable to store maze index. This is used to draw the appropriate maze. + */ int _mazeIndex; + + /** + * @param - _x - coordinate for centre of the ball. + */ int _x; + + /** + * @param - _y - coordinate for centre of the ball. + */ int _y; + + /** + * @param - _radius - value of the size of the ball + */ int _radius; - Vector2D position; + /** + * @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. + */ + bool _control; - bool _control; + /** + * @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. + */ bool _colour; + /** + * @param - _mazArray[][] - 2D Float Array storing the locations of the turned on pixels. Used for wall collision checks. + */ float _mazeArray[84][48]; };