ELEC2645 (2018/19) / Mbed 2 deprecated el17lw

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Skateboarder.h Source File

Skateboarder.h

00001 #ifndef SKATEBOARDER_H
00002 #define SKATEBOARDER_H
00003 
00004 #include "mbed.h"
00005 #include "Gamepad.h"
00006 
00007 /** Enum for skater direction */
00008 enum Skate_direction {
00009   Left, /**< Facing left. */
00010   Right /**< Facing right. */
00011 };
00012 
00013 /** Enum for different sprites */    
00014 enum Sprite_value {
00015   Skate_right, /**< Moving right sprite. */
00016   Skate_left, /**< Moving left sprite. */
00017   Stand_left, /**< Standing and facing left sprite. */
00018   Stand_right, /**< Standing and facing right sprite. */
00019   Skate_duck_right, /**< Moving right and ducking sprite. */
00020   Skate_duck_left /**< Moving left and ducking sprite. */
00021 };
00022 
00023 /** Skateboarder Class
00024 * @brief Describes the characteristics of the skateboarder sprite * @author Lewis Wooltorton
00025 * @date March 2019
00026 
00027 @code
00028 
00029 #include "mbed.h"
00030 #include "N5110.h"
00031 #include "Gamepad.h"
00032 #include "mbed.h"
00033 #include "N5110.h"
00034 #include "Gamepad.h"
00035 #include "Skateboarder.h"
00036 #include <cstdlib>
00037 #include <ctime>
00038 
00039 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00040 Gamepad gamepad;
00041 Skateboarder _skater;
00042 
00043 int _moving_counter;
00044 int _jump_counter;
00045 int _skater_x; 
00046 int _skater_y;
00047 int _level_condition;
00048 bool _fall_flag;
00049 Skate_direction _skater_direction;
00050 Sprite_value _skater_sprite;
00051 bool _start_platform_flag;
00052 
00053 int main() {
00054   // Initialise external skater values.
00055   _skater_direction = Left;
00056   _start_platform_flag = true;  // For printing start text in EngineController.
00057   _skater.set_reset_flag(false);
00058   _moving_counter = 0;
00059   _jump_counter = 20;  // Start game falling onto the platform. 
00060   _fall_flag = false;
00061   while(1) {
00062     
00063     // Process Y coord.
00064     if (_fall_flag) {
00065       _skater.fall(_fall_flag, gamepad);
00066     } else {
00067       _skater.set_y_position(false, _jump_counter, _level_condition, 
00068                              gamepad);  // Not jumping as input flag is false.
00069     }
00070     _fall_flag = _skater.get_fall_flag();  // Update fall flag.
00071     _skater_y = _skater.get_y_position();
00072     _jump_counter = _skater.get_jump_counter();  // Update jump counter.
00073     
00074     // Process X coord.
00075     _skater.set_x_position_and_sprite(1, 
00076       _moving_counter, 
00077       _skater_direction,
00078       1);  // For input coords (1,1).
00079     _skater_x = _skater.get_x_position();
00080     _moving_counter = _skater.get_moving_counter();  // Update moving counter.
00081         
00082     //Update the direction.
00083     _skater_direction = _skater.get_direction();
00084     
00085     // Print sprite.
00086     lcd.drawSprite(_skater_x,_skater_y,17,10,
00087                 (int *)_skater.get_sprite(_skater_sprite)); 
00088   }  
00089 }
00090     
00091 @endcode
00092 */
00093 
00094 class Skateboarder {
00095  public:
00096   // Constructor and destructor.
00097   /**
00098   * @brief Constructor @details Non user specified.
00099   */
00100   Skateboarder();
00101   /**
00102   * @brief Destructor @details Non user specified.
00103   */
00104   ~Skateboarder();
00105   
00106   // Mutators.
00107   /**
00108   * @brief Sets the X coordinate of the Skateboarder.
00109   * @param joy_x @details The horizontal value of the joystick
00110   * @param moving_counter @details The counter that adds the displacement to the X coordinate from the centre position
00111   * @param @details The current direction the skater is facing from the enum class Skate_direction
00112   * @param @details The vertical value of the joystick
00113   */
00114   void set_x_position_and_sprite(
00115     float joy_x, 
00116     int moving_counter, 
00117     Skate_direction direction, 
00118     float joy_y);
00119   /** 
00120   * @brief Sets the Y coordinate of the Skateboarder.
00121   * @param jump @details A boolean flag that is true only if the A button is pressed
00122   * @param jump_counter @details The counter that adds the displacement to the Y coordinate from the current position
00123   * @param level_condition @details an integer that is 1 if the skateboarder is under / on top of the top platforms 
00124   * @param &gamepad @details The gamepad object from Gamepad class
00125   */
00126   void set_y_position(bool jump, int jump_counter, int level_condition, Gamepad &gamepad);
00127   /**
00128   * @brief Sets the reset flag of the Skateboarder.
00129   * @param flag @details A boolean flag that is assigned to the Skateboarder reset flag
00130   */
00131   void set_reset_flag(bool flag);
00132   
00133   // Accessors. 
00134   /**
00135   * @brief Gets the X coordinate.
00136   * @returns The X coordinate of the Skateboarder
00137   */
00138   int get_x_position();
00139   /**
00140   * @brief Gets the moving counter.
00141   * @returns The moving counter for the Skateboarder
00142   */
00143   int get_moving_counter();
00144   /**
00145   * @brief Gets the Y coordinate.
00146   * @returns The Y coordinate of the Skateboarder
00147   */
00148   int get_y_position();
00149   /** 
00150   * @brief Gets the jump counter.
00151   * @returns The jump counter for the Skateboarder
00152   */
00153   int get_jump_counter();
00154   /**
00155   * @brief Gets the fall flag.
00156   * @returns The fall flag of the Skateboarder
00157   */
00158   bool get_fall_flag();
00159   /**
00160   * @brief Gets the reset flag.
00161   * @returns The Reset flag of the Skateboarder
00162   */
00163   bool get_reset_flag();
00164   /**
00165   * @brief Gets the sprite value.
00166   * @returns The sprite value of the Skateboarder (from the enum class Sprite_value)
00167   */
00168   Sprite_value get_sprite_value();
00169   /**
00170   * @brief Gets the skate direction.
00171   * @returns The Skateboarders direction (from the enum class Skate_Direction)
00172   */
00173   Skate_direction get_direction();
00174   /**
00175   * @brief Gets the sprite.
00176   * @returns The Skateboarders sprite (an integer array)
00177   */ 
00178   int * get_sprite(Sprite_value sprite);
00179   
00180   // Member Methods.
00181   /**
00182   * @brief Executes the falling sequence for the Skateboarder.
00183   * @param fall_flag @details The boolean fall flag of the Skateboarder
00184   * @param &gamepad @details The gamepad object from Gamepad class
00185   */
00186   void fall(bool fall_flag, Gamepad &gamepad);
00187  
00188  private:
00189   void process_x_variables(float joy_x);
00190   void check_duck(float joy_y);
00191     
00192   int _x;
00193   int _moving_counter;
00194   int _y;
00195   int _level_condition;
00196   int _level;
00197   int _jump_counter;
00198   bool _fall_flag;
00199   bool _reset_flag;
00200   Skate_direction _skate_direction;
00201   Sprite_value _sprite_value;    
00202 };
00203 #endif