James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ClassicEngine.h Source File

ClassicEngine.h

00001 #ifndef CLASSICENGINE_H
00002 #define CLASSICENGINE_H
00003 
00004 #include "mbed.h"
00005 #include "N5110.h"
00006 #include "Gamepad.h"
00007 #include "Ball.h"
00008 #include "Map.h"
00009 #include "Pause.h"
00010 
00011 /** ClassicEngine Class
00012 @brief Library to power the Classic game mode
00013 @brief Includes feature to check for and update high scores
00014 
00015 @author James Cummins
00016 
00017 @code
00018 
00019 #include "mbed.h"
00020 #include "ClassicEngine.h"
00021 
00022 ClassicEngine engine;
00023 Ball ball;
00024 Map map;
00025 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00026 FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
00027 Gamepad gamepad;
00028 
00029 int main(){
00030     int fps = 30;               //set the game mode frames per sec
00031     classic.init(ball, map);    //initialise the engine before use
00032     bool collision = false;     //create bool to check whether to continue
00033     while(!(collision)){
00034         
00035     //classic update contains methods for reading and updating map and ball
00036         classic.classic_update(ball, accelerometer, map);
00037     //use clear, classic draw and refresh together to re-render the screen each frame
00038         lcd.clear();
00039         classic.classic_draw(lcd, map, ball);
00040         lcd.refresh();
00041         wait(1/fps);
00042     //Check for successful completion of game and display appropriate message
00043         if(classic.finished()){         //check if the game has been completed
00044     //mode_complete includes feature to update high scores
00045             classic.mode_complete(lcd, gamepad, fps);
00046             break;
00047         }
00048     //Check for game failure and display appropriate message with option to
00049     //restart or go back
00050         if(map.check_wall_collision(gamepad, ball)){    //end the game if collision with wall
00051             collision = classic.mode_failed(lcd, gamepad, ball, map);   //gives the option to restart the game (and stay in while loop) or quit
00052     //reset the game mode to the beginning if user wants to play again
00053             if(!(collision)){ classic.init(ball, map); }
00054         }
00055     }
00056 }
00057 @endcode
00058 */
00059 
00060 
00061 class ClassicEngine {
00062 
00063 public:
00064     /** 
00065     * @brief Create an engine object for the Classic game mode
00066     */
00067     ClassicEngine();
00068     /** 
00069     * @brief Delete a Classic Engine object to free up memory
00070     */
00071     ~ClassicEngine();
00072     /** 
00073     * @brief Initialise the engine object with the start position of the game map
00074     * and fix the ball to the centre of the screen. Initialise frames counted to 0.
00075     * @param ball - Ball object to set the ball's position
00076     * @param map - Map object to set the map's initial position
00077     */
00078     void init(Ball &ball, Map &map);
00079     /** 
00080     * @brief Read the accelerometer input and update the map's motion using it.
00081     * Update the absolute position of the ball in relation to the map.
00082     * @param ball - Ball object to access ball's position
00083     * @param accelerometer - FXOS8700CQ object to read user's input motion
00084     * @param map - Map object to update map
00085     */
00086     void classic_update(Ball &ball, FXOS8700CQ &accelerometer, Map &map);
00087     /** 
00088     * @brief render the game mode contents on the LCD screen
00089     * @param lcd - N5110 object to access screen
00090     * @param map - Map object to draw Map in current position
00091     * @param ball - Ball object to draw ball in fixed position
00092     */
00093     void classic_draw(N5110 &lcd, Map &map, Ball &ball);
00094     /** 
00095     * @brief Check if the player has successfully reached the end of the game mode
00096     * @returns 
00097     *       true - ball has reached finish line
00098     *       false - ball has not reached finish line
00099     */
00100     bool finished();
00101     /** 
00102     * @brief Display the end of game mode screen with the player's completion time
00103     * @param lcd - N5110 object to draw display on LCD screen
00104     * @param gamepad - Gamepad object to check for user pushing button
00105     * @param fps - integer value of game's frames per second
00106     */
00107     void mode_complete(N5110 &lcd, Gamepad &gamepad, int fps);
00108     /** 
00109     * @brief Display game over screen and Check whether player wants to play again
00110     * returns
00111     *       true - player wants to return to start menu
00112     *       false - player wants to play again
00113     */
00114     bool mode_failed(N5110 &lcd, Gamepad &gamepad, Ball &ball, Map &map);
00115     
00116 private:
00117     //private member functions
00118     void write_high_scores(int time_taken);
00119     void read_high_scores();
00120     void check_high_score(int time_taken);
00121     
00122     //private member variables
00123     int _index_array[6];
00124     float _array_of_values[6];
00125     int _frames;
00126     Vector2D _ball_coord;   //this gives the current co-ord of the ball in relation to the screen
00127     Vector2D _map_coord;
00128     Vector2D _abs_ball_pos;     //this gives the co-ord of ball on map overall ( x between 0 and 500, y between 0 and 200)
00129 
00130 };
00131     
00132 #endif