ELEC2645 (2018/19) / Mbed 2 deprecated el17lw

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Engine.h Source File

Engine.h

00001 #ifndef ENGINE_H
00002 #define ENGINE_H
00003 
00004 #include "mbed.h"
00005 #include "N5110.h"
00006 #include "Gamepad.h"
00007 #include "Skateboarder.h"
00008 #include "Platforms.h"
00009 #include "Coin.h"
00010 #include "Fire.h"
00011 #include <cstdlib>
00012 #include <ctime>
00013 
00014 /** Input struct */
00015 struct Input {
00016   Vector2D coord; /**< Vector 2D for joystick coords */
00017   bool A_flag; /**< Boolean flag for the A button */
00018   };
00019 
00020 /** Engine Class
00021 * @brief Handles the game mechanics in a game engine: reads inputs, updates game and LCD 
00022 * @author Lewis Wooltorton
00023 * @date March 2019
00024 
00025 @code
00026 
00027 #include "mbed.h"
00028 #include "N5110.h"
00029 #include "Gamepad.h"
00030 #include "Engine.h"
00031 
00032 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00033 Gamepad gamepad;
00034 Engine _game_engine;
00035 
00036 int _game_counter;
00037 bool _start_platform_flag;
00038 int _speed_divider;
00039 int _player_score;
00040 
00041 int main() {
00042   _game_engine.init();
00043   while(1) {
00044     
00045     // Run all reset checks.
00046     _game_engine.check_reset(lcd, gamepad);  // Resets the game if skater has 
00047     // died.
00048     _start_platform_flag = _game_engine.get_start_platform_flag();
00049     if (_game_counter == 100) _game_counter = 0;  // Count from 0 to 99.
00050     
00051     // Run the engine.
00052     _game_engine.read_input(gamepad);
00053     _game_engine.set_level_condition();  // Determines if the skater is under 
00054     // upper platforms.
00055     _game_engine.process_y(gamepad);
00056     _game_engine.process_x(_game_counter);
00057     _game_engine.process_sprite();
00058     _game_engine.check_coin_collision(gamepad);
00059     _game_engine.check_fire_collision(gamepad, _game_counter);
00060     _game_engine.update_lcd(lcd, _game_counter);
00061     
00062     // Update the game counter and player score.
00063     if (_game_counter % _speed_divider == 0) 
00064     _game_engine.generate_level(_game_counter);  // Level speed is determined by 
00065     // speed divider.
00066     _game_counter++;
00067     _player_score = _game_engine.get_player_score();
00068     _speed_divider = int(-0.25*_player_score + 10);  // Speed divider is 
00069     // dependent on how many coins you have
00070   }
00071 }
00072 
00073 @endcode
00074 */
00075 
00076 class Engine {
00077  public:
00078   // Constructor and destructor.
00079   /**
00080   * @brief Constructor 
00081   * @details Non user specified.
00082   */
00083   Engine();
00084   /**
00085   * @brief Destructor 
00086   * @details Non user specified.
00087   */
00088   ~Engine();
00089   
00090   // Mutators.
00091   /**
00092   * @brief Initalises the Game Engine object.
00093   */ 
00094   void init(); 
00095   /** 
00096   * @brief Sets the input variables.
00097   * @param &gamepad * @details The gamepad object from Gamepad class
00098   */
00099   void read_input(Gamepad &gamepad); // call set input
00100   /**
00101   * @brief Sets the level condition of the Skateboarder.
00102   */
00103   void set_level_condition();
00104   
00105   // Accessors.
00106   /**
00107   * @brief Gets the start platform flag.
00108   * @returns The start platform flag for printing the start platform sprites
00109   */
00110   bool get_start_platform_flag();
00111   /**
00112   * @brief Gets the player score.
00113   * @returns The amount of coins the player has collected 
00114   */
00115   int get_player_score();
00116   
00117   // Member Methods.
00118   /**
00119   * @brief Processes y coordinate of the Skateboarder.
00120   * @param &gamepad @details The gamepad object from Gamepad class
00121   */
00122   void process_y(Gamepad &gamepad);
00123   /**
00124   * @brief Processes x coordinate of the Skateboarder.
00125   * @param game_counter @details A counter that increments every loop iteration from 0 to 99
00126   */
00127   void process_x(int game_counter);
00128   /**
00129   * @brief Processes the Skateboarder sprite.
00130   */
00131   void process_sprite();
00132   /**
00133   * @brief Updates the LCD display.
00134   * @param &lcd @details The lcd object from the N5110 class
00135   * @param game_counter @details A counter that increments every loop iteration from 0 to 99
00136   */
00137   void update_lcd(N5110 &lcd, int game_counter);
00138   /**
00139   * @brief Generates components of the level: fire, platforms, coin and background.
00140   * @param game_counter @details A counter that increments every loop iteration from 0 to 99
00141   */
00142   void generate_level(int game_counter);
00143   /**
00144   * @brief Checks if the game needs to be reset and resets if it does.
00145   * @param &lcd @details The lcd object from the N5110 class
00146   * @param &gamepad @details The gamepad object from Gamepad class
00147   */
00148   void check_reset(N5110 &lcd, Gamepad &gamepad);
00149   /**
00150   * @brief Checks if the Skateboarder has collected a coin and increments player score if it has.
00151   * @param &gamepad @details The gamepad object from Gamepad class
00152   */
00153   void check_coin_collision(Gamepad &gamepad);
00154   /**
00155   * @brief Checks if the Skateboarder has touched the fire and initiates reset if it has.
00156   * @param &gamepad @details The gamepad object from Gamepad class
00157   * @param game_counter @details A counter that increments every loop iteration from 0 to 99
00158   */
00159   void check_fire_collision(Gamepad &gamepad, int game_counter);
00160     
00161  private: 
00162   void reset_skater();
00163   void reset_engine();
00164   void set_fall_flag();
00165   void generate_lower_lines();
00166   void generate_upper_lines();
00167   void generate_fire(int game_counter);
00168   void execute_dying_sequence(N5110 &lcd, Gamepad &gamepad);
00169   void draw_screen_fire(int game_counter, N5110 &lcd);
00170   
00171   Input _input;
00172   Skateboarder _skater;
00173   Coin _coin;
00174   Fire _fire;
00175   int _fire_y;
00176   int _moving_counter;
00177   int _jump_counter;
00178   int _skater_x; 
00179   int _skater_y;
00180   int _level_condition;
00181   bool _fall_flag;
00182   Skate_direction _skater_direction;
00183   Sprite_value _skater_sprite;
00184   Platforms _lower_platforms;
00185   Platforms _upper_platforms;
00186   Line _lower_line_1;
00187   Line _lower_line_2;
00188   Line _lower_line_3;
00189   Line _upper_line_1;
00190   Line _upper_line_2;
00191   Line _upper_line_3;
00192   int _length_1;
00193   int _length_2;
00194   int _length_3; 
00195   bool _start_platform_flag;
00196   bool _coin_collision_flag;
00197   int _player_score;
00198   int _speed_divider;
00199   int _fire_height;
00200 };
00201 #endif
00202