Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17dg by
player.h
00001 #ifndef PLAYER_H 00002 #define PLAYER_H 00003 00004 /** 00005 * Player Class 00006 * @brief Manages player's ship. 00007 * @author Dmitrijs Griskovs 00008 * @date 15/04/2019 00009 */ 00010 class Player : public GameObject { 00011 public: 00012 /** 00013 * @var static const int max_player_blasts variable; 00014 * @brief Sets the limit of maximum player blasts on the screen. 00015 */ 00016 static const int max_player_blasts = 5; 00017 00018 00019 GameObject blasts[max_player_blasts]; 00020 CircleBounds player_bounds; 00021 CircleBounds blast_bounds; 00022 Game force_shield_check; 00023 /** 00024 * @brief A constructor of the player's ship. 00025 * @details A constructor for the palyer's sprite body circle area and the 00026 * blast circle area. It sets the circle radius for collsion callculations. 00027 */ 00028 Player() { 00029 player_bounds.center.x = 5; 00030 player_bounds.center.y = 7; 00031 player_bounds.radius = 5; 00032 00033 blast_bounds.center.x = 0; 00034 blast_bounds.center.y = 1; 00035 blast_bounds.radius = 1; 00036 } 00037 00038 /** 00039 * @brief Draws the player's blasts on the screen 00040 * @details This function Will draw every activated blast to the left with the blast_speed. 00041 * It will deactivate blasts when they leave the screen, for future reuse. 00042 * If the blast does miss the enemy and leaves the screen limits, the function will 00043 * substract 10 points in game_score. 00044 */ 00045 void updateAndDrawBlasts() { 00046 for (int i = 0; i < max_player_blasts; ++i) { 00047 if (blasts[i].active) { 00048 blasts[i].pos.x += blast_speed; 00049 if (blasts[i].pos.x >= screen_width){ 00050 blasts[i].active = false; 00051 GameGlobals::game_score -= 10; 00052 } 00053 drawSprite(blasts[i].pos, blast_sprite); 00054 } 00055 } 00056 } 00057 00058 /** 00059 * @brief Makes a blast active and gives it the start positions. 00060 * @details This function searches the array for the inactive blasts, 00061 * If a blast is not active, it will set it to active and will start drawing 00062 * it accross the screen until it reaches the LCD border line. 00063 */ 00064 void fireNewBlast() { 00065 // Search the array of blasts if inactive we can use it. 00066 int found = -1; 00067 for (int i = 0; i < max_player_blasts; ++i) { 00068 if (!blasts[i].active) { 00069 found = i; 00070 break; 00071 } 00072 } 00073 if (found != -1) { 00074 blasts[found].active = true; 00075 blasts[found].pos.x = pos.x + spaceship1_width - 2; 00076 blasts[found].pos.y = pos.y + (spaceship1_height/2); 00077 } 00078 } 00079 00080 /** 00081 * @brief Updates and draws player's ship (including with force shield sprite) positon. 00082 * @details The function reads the analog input signal from joystick and 00083 * moves the player's ship on the LCD accordingly.(move joystick, the ship 00084 * moves up, move joystick right, the ship moves right). Also, this function checks 00085 * whether the force shield was activated in game.cpp, if it was then the player's 00086 * ship is replaced the exactky the same sip but with the added force shield. 00087 */ 00088 void updateAndDraw() { 00089 if(x_dir.read() > joy_threshold_max_x) { 00090 pos.x -= ship_speed; 00091 } else if(x_dir.read() < joy_threshold_min_x) { 00092 pos.x += ship_speed; 00093 } 00094 if(y_dir.read() > joy_threshold_max_y) { 00095 pos.y -= ship_speed; 00096 } else if(y_dir.read() < joy_threshold_min_y) { 00097 pos.y += ship_speed; 00098 } 00099 shipMovementLimits(); 00100 draw(); 00101 } 00102 /** 00103 * @brief Updates and draws player's ship and the force shield sprites. 00104 */ 00105 void draw() { 00106 if (force_shield_check.forceShildActivate()){ 00107 drawSpriteOnTop(pos, player_spaceship1_shield_sprite); 00108 } else { 00109 drawSpriteOnTop(pos, player_spaceship1_sprite); 00110 } 00111 } 00112 private: 00113 /** 00114 * @var static const int ship_speed variable; 00115 * @brief Sets the speed of the player's ship. 00116 */ 00117 static const int ship_speed = 2; 00118 /** 00119 * @var static const int blast_speed variable; 00120 * @brief Sets the speed of the player's blast. 00121 */ 00122 static const int blast_speed = 5; 00123 00124 /** Prevents the player's ship to go beyond the playing zone limits.*/ 00125 void shipMovementLimits(){ 00126 // Limits player ship on screen 00127 if (pos.x < game_area_x){ pos.x = game_area_x;} 00128 if (pos.y < game_area_y) { pos.y = game_area_y;} 00129 int max_player_x = game_area_x + game_area_width - spaceship1_width; 00130 int max_player_y = game_area_y + game_area_height - spaceship1_height; 00131 if (pos.x > max_player_x) pos.x = max_player_x; 00132 if (pos.y > max_player_y) pos.y = max_player_y; 00133 } 00134 }; 00135 #endif
Generated on Thu Jul 14 2022 14:15:02 by
1.7.2
