Initial publish
Dependencies: mbed
Fork of el17dg by
game/player.h@40:e3bbda7444fa, 2019-05-07 (annotated)
- Committer:
- Noximilien
- Date:
- Tue May 07 15:22:35 2019 +0000
- Revision:
- 40:e3bbda7444fa
- Parent:
- 36:207ec7db8648
The Final, Submission Version. I have read and agreed to the academic integrity. SID:201160286
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Noximilien | 23:240bc00ef25b | 1 | #ifndef PLAYER_H |
Noximilien | 23:240bc00ef25b | 2 | #define PLAYER_H |
Noximilien | 23:240bc00ef25b | 3 | |
Noximilien | 31:becb8f6bf7b7 | 4 | /** |
Noximilien | 31:becb8f6bf7b7 | 5 | * Player Class |
Noximilien | 31:becb8f6bf7b7 | 6 | * @brief Manages player's ship. |
Noximilien | 29:579e00b7f118 | 7 | * @author Dmitrijs Griskovs |
Noximilien | 29:579e00b7f118 | 8 | * @date 15/04/2019 |
Noximilien | 29:579e00b7f118 | 9 | */ |
Noximilien | 33:c623c6d5ed16 | 10 | class Player : public GameObject { |
Noximilien | 23:240bc00ef25b | 11 | public: |
Noximilien | 32:5403bb974294 | 12 | /** |
Noximilien | 32:5403bb974294 | 13 | * @var static const int max_player_blasts variable; |
Noximilien | 32:5403bb974294 | 14 | * @brief Sets the limit of maximum player blasts on the screen. |
Noximilien | 32:5403bb974294 | 15 | */ |
Noximilien | 32:5403bb974294 | 16 | static const int max_player_blasts = 5; |
Noximilien | 35:172db1608332 | 17 | |
Noximilien | 32:5403bb974294 | 18 | |
Noximilien | 31:becb8f6bf7b7 | 19 | GameObject blasts[max_player_blasts]; |
Noximilien | 31:becb8f6bf7b7 | 20 | CircleBounds player_bounds; |
Noximilien | 31:becb8f6bf7b7 | 21 | CircleBounds blast_bounds; |
Noximilien | 31:becb8f6bf7b7 | 22 | Game force_shield_check; |
Noximilien | 31:becb8f6bf7b7 | 23 | /** |
Noximilien | 31:becb8f6bf7b7 | 24 | * @brief A constructor of the player's ship. |
Noximilien | 31:becb8f6bf7b7 | 25 | * @details A constructor for the palyer's sprite body circle area and the |
Noximilien | 31:becb8f6bf7b7 | 26 | * blast circle area. It sets the circle radius for collsion callculations. |
Noximilien | 31:becb8f6bf7b7 | 27 | */ |
Noximilien | 31:becb8f6bf7b7 | 28 | Player() { |
Noximilien | 31:becb8f6bf7b7 | 29 | player_bounds.center.x = 5; |
Noximilien | 34:754915ce9de5 | 30 | player_bounds.center.y = 7; |
Noximilien | 34:754915ce9de5 | 31 | player_bounds.radius = 5; |
Noximilien | 31:becb8f6bf7b7 | 32 | |
Noximilien | 31:becb8f6bf7b7 | 33 | blast_bounds.center.x = 0; |
Noximilien | 31:becb8f6bf7b7 | 34 | blast_bounds.center.y = 1; |
Noximilien | 31:becb8f6bf7b7 | 35 | blast_bounds.radius = 1; |
Noximilien | 31:becb8f6bf7b7 | 36 | } |
Noximilien | 23:240bc00ef25b | 37 | |
Noximilien | 31:becb8f6bf7b7 | 38 | /** |
Noximilien | 31:becb8f6bf7b7 | 39 | * @brief Draws the player's blasts on the screen |
Noximilien | 31:becb8f6bf7b7 | 40 | * @details This function Will draw every activated blast to the left with the blast_speed. |
Noximilien | 31:becb8f6bf7b7 | 41 | * It will deactivate blasts when they leave the screen, for future reuse. |
Noximilien | 31:becb8f6bf7b7 | 42 | * If the blast does miss the enemy and leaves the screen limits, the function will |
Noximilien | 31:becb8f6bf7b7 | 43 | * substract 10 points in game_score. |
Noximilien | 31:becb8f6bf7b7 | 44 | */ |
Noximilien | 23:240bc00ef25b | 45 | void updateAndDrawBlasts() { |
Noximilien | 23:240bc00ef25b | 46 | for (int i = 0; i < max_player_blasts; ++i) { |
Noximilien | 23:240bc00ef25b | 47 | if (blasts[i].active) { |
Noximilien | 23:240bc00ef25b | 48 | blasts[i].pos.x += blast_speed; |
Noximilien | 23:240bc00ef25b | 49 | if (blasts[i].pos.x >= screen_width){ |
Noximilien | 23:240bc00ef25b | 50 | blasts[i].active = false; |
Noximilien | 31:becb8f6bf7b7 | 51 | GameGlobals::game_score -= 10; |
Noximilien | 23:240bc00ef25b | 52 | } |
Noximilien | 34:754915ce9de5 | 53 | drawSprite(blasts[i].pos, blast_sprite); |
Noximilien | 23:240bc00ef25b | 54 | } |
Noximilien | 23:240bc00ef25b | 55 | } |
Noximilien | 23:240bc00ef25b | 56 | } |
Noximilien | 29:579e00b7f118 | 57 | |
Noximilien | 31:becb8f6bf7b7 | 58 | /** |
Noximilien | 31:becb8f6bf7b7 | 59 | * @brief Makes a blast active and gives it the start positions. |
Noximilien | 31:becb8f6bf7b7 | 60 | * @details This function searches the array for the inactive blasts, |
Noximilien | 31:becb8f6bf7b7 | 61 | * If a blast is not active, it will set it to active and will start drawing |
Noximilien | 31:becb8f6bf7b7 | 62 | * it accross the screen until it reaches the LCD border line. |
Noximilien | 31:becb8f6bf7b7 | 63 | */ |
Noximilien | 23:240bc00ef25b | 64 | void fireNewBlast() { |
Noximilien | 31:becb8f6bf7b7 | 65 | // Search the array of blasts if inactive we can use it. |
Noximilien | 23:240bc00ef25b | 66 | int found = -1; |
Noximilien | 23:240bc00ef25b | 67 | for (int i = 0; i < max_player_blasts; ++i) { |
Noximilien | 23:240bc00ef25b | 68 | if (!blasts[i].active) { |
Noximilien | 23:240bc00ef25b | 69 | found = i; |
Noximilien | 23:240bc00ef25b | 70 | break; |
Noximilien | 23:240bc00ef25b | 71 | } |
Noximilien | 29:579e00b7f118 | 72 | } |
Noximilien | 23:240bc00ef25b | 73 | if (found != -1) { |
Noximilien | 23:240bc00ef25b | 74 | blasts[found].active = true; |
Noximilien | 33:c623c6d5ed16 | 75 | blasts[found].pos.x = pos.x + spaceship1_width - 2; |
Noximilien | 33:c623c6d5ed16 | 76 | blasts[found].pos.y = pos.y + (spaceship1_height/2); |
Noximilien | 23:240bc00ef25b | 77 | } |
Noximilien | 29:579e00b7f118 | 78 | } |
Noximilien | 29:579e00b7f118 | 79 | |
Noximilien | 31:becb8f6bf7b7 | 80 | /** |
Noximilien | 31:becb8f6bf7b7 | 81 | * @brief Updates and draws player's ship (including with force shield sprite) positon. |
Noximilien | 31:becb8f6bf7b7 | 82 | * @details The function reads the analog input signal from joystick and |
Noximilien | 31:becb8f6bf7b7 | 83 | * moves the player's ship on the LCD accordingly.(move joystick, the ship |
Noximilien | 31:becb8f6bf7b7 | 84 | * moves up, move joystick right, the ship moves right). Also, this function checks |
Noximilien | 31:becb8f6bf7b7 | 85 | * whether the force shield was activated in game.cpp, if it was then the player's |
Noximilien | 31:becb8f6bf7b7 | 86 | * ship is replaced the exactky the same sip but with the added force shield. |
Noximilien | 31:becb8f6bf7b7 | 87 | */ |
Noximilien | 33:c623c6d5ed16 | 88 | void updateAndDraw() { |
Noximilien | 31:becb8f6bf7b7 | 89 | if(x_dir.read() > joy_threshold_max_x) { |
Noximilien | 33:c623c6d5ed16 | 90 | pos.x -= ship_speed; |
Noximilien | 31:becb8f6bf7b7 | 91 | } else if(x_dir.read() < joy_threshold_min_x) { |
Noximilien | 33:c623c6d5ed16 | 92 | pos.x += ship_speed; |
Noximilien | 23:240bc00ef25b | 93 | } |
Noximilien | 31:becb8f6bf7b7 | 94 | if(y_dir.read() > joy_threshold_max_y) { |
Noximilien | 33:c623c6d5ed16 | 95 | pos.y -= ship_speed; |
Noximilien | 31:becb8f6bf7b7 | 96 | } else if(y_dir.read() < joy_threshold_min_y) { |
Noximilien | 33:c623c6d5ed16 | 97 | pos.y += ship_speed; |
Noximilien | 23:240bc00ef25b | 98 | } |
Noximilien | 28:35af3843de8f | 99 | shipMovementLimits(); |
Noximilien | 33:c623c6d5ed16 | 100 | draw(); |
Noximilien | 33:c623c6d5ed16 | 101 | } |
Noximilien | 34:754915ce9de5 | 102 | /** |
Noximilien | 34:754915ce9de5 | 103 | * @brief Updates and draws player's ship and the force shield sprites. |
Noximilien | 34:754915ce9de5 | 104 | */ |
Noximilien | 33:c623c6d5ed16 | 105 | void draw() { |
Noximilien | 30:d454d0cb72bc | 106 | if (force_shield_check.forceShildActivate()){ |
Noximilien | 33:c623c6d5ed16 | 107 | drawSpriteOnTop(pos, player_spaceship1_shield_sprite); |
Noximilien | 31:becb8f6bf7b7 | 108 | } else { |
Noximilien | 33:c623c6d5ed16 | 109 | drawSpriteOnTop(pos, player_spaceship1_sprite); |
Noximilien | 30:d454d0cb72bc | 110 | } |
Noximilien | 28:35af3843de8f | 111 | } |
Noximilien | 28:35af3843de8f | 112 | private: |
Noximilien | 35:172db1608332 | 113 | /** |
Noximilien | 35:172db1608332 | 114 | * @var static const int ship_speed variable; |
Noximilien | 35:172db1608332 | 115 | * @brief Sets the speed of the player's ship. |
Noximilien | 35:172db1608332 | 116 | */ |
Noximilien | 35:172db1608332 | 117 | static const int ship_speed = 2; |
Noximilien | 35:172db1608332 | 118 | /** |
Noximilien | 35:172db1608332 | 119 | * @var static const int blast_speed variable; |
Noximilien | 35:172db1608332 | 120 | * @brief Sets the speed of the player's blast. |
Noximilien | 35:172db1608332 | 121 | */ |
Noximilien | 35:172db1608332 | 122 | static const int blast_speed = 5; |
Noximilien | 35:172db1608332 | 123 | |
Noximilien | 31:becb8f6bf7b7 | 124 | /** Prevents the player's ship to go beyond the playing zone limits.*/ |
Noximilien | 28:35af3843de8f | 125 | void shipMovementLimits(){ |
Noximilien | 29:579e00b7f118 | 126 | // Limits player ship on screen |
Noximilien | 33:c623c6d5ed16 | 127 | if (pos.x < game_area_x){ pos.x = game_area_x;} |
Noximilien | 33:c623c6d5ed16 | 128 | if (pos.y < game_area_y) { pos.y = game_area_y;} |
Noximilien | 23:240bc00ef25b | 129 | int max_player_x = game_area_x + game_area_width - spaceship1_width; |
Noximilien | 23:240bc00ef25b | 130 | int max_player_y = game_area_y + game_area_height - spaceship1_height; |
Noximilien | 33:c623c6d5ed16 | 131 | if (pos.x > max_player_x) pos.x = max_player_x; |
Noximilien | 33:c623c6d5ed16 | 132 | if (pos.y > max_player_y) pos.y = max_player_y; |
Noximilien | 25:749f1efc31fc | 133 | } |
Noximilien | 23:240bc00ef25b | 134 | }; |
Noximilien | 23:240bc00ef25b | 135 | #endif |