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.
Player/Player.cpp@14:1e6f74233e8e, 2019-05-08 (annotated)
- Committer:
- joshdavy
- Date:
- Wed May 08 14:41:56 2019 +0000
- Revision:
- 14:1e6f74233e8e
- Parent:
- 11:db27d3838514
Documentation test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| joshdavy | 3:b34685dbdb8d | 1 | #include "Player.h" |
| joshdavy | 11:db27d3838514 | 2 | /** |
| joshdavy | 11:db27d3838514 | 3 | * @brief Constructor (no paramateters) |
| joshdavy | 11:db27d3838514 | 4 | */ |
| joshdavy | 3:b34685dbdb8d | 5 | |
| joshdavy | 3:b34685dbdb8d | 6 | Player::Player() |
| joshdavy | 3:b34685dbdb8d | 7 | { |
| joshdavy | 8:21b6d4dbce44 | 8 | |
| joshdavy | 3:b34685dbdb8d | 9 | } |
| joshdavy | 11:db27d3838514 | 10 | /** |
| joshdavy | 11:db27d3838514 | 11 | * @brief Deconstructor |
| joshdavy | 11:db27d3838514 | 12 | */ |
| joshdavy | 3:b34685dbdb8d | 13 | Player::~Player() |
| joshdavy | 3:b34685dbdb8d | 14 | { |
| joshdavy | 3:b34685dbdb8d | 15 | |
| joshdavy | 3:b34685dbdb8d | 16 | } |
| joshdavy | 3:b34685dbdb8d | 17 | |
| joshdavy | 11:db27d3838514 | 18 | |
| joshdavy | 11:db27d3838514 | 19 | /** |
| joshdavy | 11:db27d3838514 | 20 | * @brief Initialises the player |
| joshdavy | 11:db27d3838514 | 21 | * @param pos @details The initial positon of the player |
| joshdavy | 11:db27d3838514 | 22 | */ |
| joshdavy | 10:58cf89dd878c | 23 | void Player::init(Vector2D pos) |
| joshdavy | 4:afbf3dd71403 | 24 | { |
| joshdavy | 10:58cf89dd878c | 25 | _height = PLAYER_HEIGHT; |
| joshdavy | 10:58cf89dd878c | 26 | _width = PLAYER_WIDTH; |
| joshdavy | 11:db27d3838514 | 27 | _pos = pos; // Changes with the player moving |
| joshdavy | 11:db27d3838514 | 28 | _initial_pos = pos; // Constant |
| joshdavy | 8:21b6d4dbce44 | 29 | _bitmap = (int *) player_bitmap_left; // Default |
| joshdavy | 10:58cf89dd878c | 30 | _orientation = 1; // Upright |
| joshdavy | 10:58cf89dd878c | 31 | _direction = -1; // Left |
| joshdavy | 4:afbf3dd71403 | 32 | } |
| joshdavy | 4:afbf3dd71403 | 33 | |
| joshdavy | 11:db27d3838514 | 34 | /** |
| joshdavy | 11:db27d3838514 | 35 | * @brief Checks if the player is off the screen, if so returns player |
| joshdavy | 11:db27d3838514 | 36 | * to initial position. |
| joshdavy | 11:db27d3838514 | 37 | */ |
| joshdavy | 9:96969b1c6bde | 38 | void Player::check_out_of_range() |
| joshdavy | 9:96969b1c6bde | 39 | { |
| joshdavy | 11:db27d3838514 | 40 | // If off the screen. |
| joshdavy | 9:96969b1c6bde | 41 | if (_pos.x < -5 || _pos.x > 89 || _pos.y < -5 || _pos.y > 53) { |
| joshdavy | 11:db27d3838514 | 42 | // Return to initial positon. |
| joshdavy | 9:96969b1c6bde | 43 | _pos = _initial_pos; |
| joshdavy | 9:96969b1c6bde | 44 | _orientation = 1; |
| joshdavy | 9:96969b1c6bde | 45 | } |
| joshdavy | 9:96969b1c6bde | 46 | } |
| joshdavy | 11:db27d3838514 | 47 | /** |
| joshdavy | 11:db27d3838514 | 48 | * @brief Returns true if the player has reached the goal. |
| joshdavy | 11:db27d3838514 | 49 | * @returns if the goal has been reached |
| joshdavy | 11:db27d3838514 | 50 | */ |
| joshdavy | 10:58cf89dd878c | 51 | bool Player::check_goal_reached(Vector2D goal) |
| joshdavy | 9:96969b1c6bde | 52 | { |
| joshdavy | 9:96969b1c6bde | 53 | bool goal_reached = false; |
| joshdavy | 11:db27d3838514 | 54 | // X and Y distance from the player to the goal. |
| joshdavy | 9:96969b1c6bde | 55 | int x_distance = abs(goal.x - _pos.x); |
| joshdavy | 9:96969b1c6bde | 56 | int y_distance = abs(goal.y - _pos.y); |
| joshdavy | 11:db27d3838514 | 57 | // If within 2 pixels in the X direction and 6 pixels in the Y direction |
| joshdavy | 9:96969b1c6bde | 58 | if (x_distance < 2 && y_distance < 6) { |
| joshdavy | 11:db27d3838514 | 59 | // Then the goal has been reached. |
| joshdavy | 9:96969b1c6bde | 60 | goal_reached = true; |
| joshdavy | 9:96969b1c6bde | 61 | } |
| joshdavy | 9:96969b1c6bde | 62 | return goal_reached; |
| joshdavy | 9:96969b1c6bde | 63 | } |
| joshdavy | 11:db27d3838514 | 64 | /** |
| joshdavy | 11:db27d3838514 | 65 | * @brief Returns true if the player can move vertically down |
| joshdavy | 11:db27d3838514 | 66 | * @returns True if can move down |
| joshdavy | 11:db27d3838514 | 67 | */ |
| joshdavy | 8:21b6d4dbce44 | 68 | bool Player::can_move_down(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 69 | { |
| joshdavy | 8:21b6d4dbce44 | 70 | bool can_move_down = true; |
| joshdavy | 11:db27d3838514 | 71 | // For every block |
| joshdavy | 8:21b6d4dbce44 | 72 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 11:db27d3838514 | 73 | // If the player is within the x range of the block |
| joshdavy | 8:21b6d4dbce44 | 74 | if (_pos.x + _width > blocks[i].first.x && |
| joshdavy | 8:21b6d4dbce44 | 75 | _pos.x < blocks[i].second.x) { |
| joshdavy | 11:db27d3838514 | 76 | // And the players bottom edge is equal to the blocks top edge |
| joshdavy | 8:21b6d4dbce44 | 77 | if ( (_pos.y + _height) == blocks[i].first.y ) { |
| joshdavy | 11:db27d3838514 | 78 | // cant move down |
| joshdavy | 8:21b6d4dbce44 | 79 | can_move_down = false; |
| joshdavy | 8:21b6d4dbce44 | 80 | } |
| joshdavy | 8:21b6d4dbce44 | 81 | } |
| joshdavy | 8:21b6d4dbce44 | 82 | } |
| joshdavy | 8:21b6d4dbce44 | 83 | return can_move_down; |
| joshdavy | 8:21b6d4dbce44 | 84 | } |
| joshdavy | 8:21b6d4dbce44 | 85 | |
| joshdavy | 11:db27d3838514 | 86 | /** |
| joshdavy | 11:db27d3838514 | 87 | * @brief Returns true if the player can move vertically up |
| joshdavy | 11:db27d3838514 | 88 | * @returns True if can move up |
| joshdavy | 11:db27d3838514 | 89 | */ |
| joshdavy | 8:21b6d4dbce44 | 90 | bool Player::can_move_up(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 91 | { |
| joshdavy | 8:21b6d4dbce44 | 92 | bool can_move_up = true; |
| joshdavy | 11:db27d3838514 | 93 | // For every block |
| joshdavy | 8:21b6d4dbce44 | 94 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 11:db27d3838514 | 95 | // If within the x range of the block |
| joshdavy | 8:21b6d4dbce44 | 96 | if (_pos.x + _width > blocks[i].first.x && |
| joshdavy | 8:21b6d4dbce44 | 97 | _pos.x < blocks[i].second.x) { |
| joshdavy | 11:db27d3838514 | 98 | // and the top edge of the player is equal to the bottom edge |
| joshdavy | 11:db27d3838514 | 99 | // of the block |
| joshdavy | 8:21b6d4dbce44 | 100 | if ( (_pos.y) == blocks[i].second.y ) { |
| joshdavy | 11:db27d3838514 | 101 | // cant move up |
| joshdavy | 8:21b6d4dbce44 | 102 | can_move_up = false; |
| joshdavy | 8:21b6d4dbce44 | 103 | } |
| joshdavy | 8:21b6d4dbce44 | 104 | } |
| joshdavy | 8:21b6d4dbce44 | 105 | } |
| joshdavy | 8:21b6d4dbce44 | 106 | return can_move_up; |
| joshdavy | 8:21b6d4dbce44 | 107 | } |
| joshdavy | 8:21b6d4dbce44 | 108 | |
| joshdavy | 11:db27d3838514 | 109 | /** |
| joshdavy | 11:db27d3838514 | 110 | * @brief Returns true if the player can move left |
| joshdavy | 11:db27d3838514 | 111 | * @returns True if can move left |
| joshdavy | 11:db27d3838514 | 112 | */ |
| joshdavy | 8:21b6d4dbce44 | 113 | bool Player::can_move_left(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 114 | { |
| joshdavy | 8:21b6d4dbce44 | 115 | bool can_move_left = true; |
| joshdavy | 11:db27d3838514 | 116 | // For every block |
| joshdavy | 8:21b6d4dbce44 | 117 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 11:db27d3838514 | 118 | // If within the y range of the block |
| joshdavy | 8:21b6d4dbce44 | 119 | if (_pos.y + _height > blocks[i].first.y && |
| joshdavy | 8:21b6d4dbce44 | 120 | _pos.y < blocks[i].second.y) { |
| joshdavy | 11:db27d3838514 | 121 | // And the left edge of the player is equal to the right edge of |
| joshdavy | 11:db27d3838514 | 122 | // the block |
| joshdavy | 8:21b6d4dbce44 | 123 | if ( (_pos.x) == blocks[i].second.x ) { |
| joshdavy | 11:db27d3838514 | 124 | // Cant move left. |
| joshdavy | 8:21b6d4dbce44 | 125 | can_move_left = false; |
| joshdavy | 8:21b6d4dbce44 | 126 | } |
| joshdavy | 8:21b6d4dbce44 | 127 | } |
| joshdavy | 8:21b6d4dbce44 | 128 | } |
| joshdavy | 8:21b6d4dbce44 | 129 | return can_move_left; |
| joshdavy | 8:21b6d4dbce44 | 130 | } |
| joshdavy | 11:db27d3838514 | 131 | /** |
| joshdavy | 11:db27d3838514 | 132 | * @brief Returns true if the player can move right |
| joshdavy | 11:db27d3838514 | 133 | * @returns True if can move right |
| joshdavy | 11:db27d3838514 | 134 | */ |
| joshdavy | 8:21b6d4dbce44 | 135 | bool Player::can_move_right(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 136 | { |
| joshdavy | 8:21b6d4dbce44 | 137 | bool can_move_right = true; |
| joshdavy | 11:db27d3838514 | 138 | // For every block |
| joshdavy | 8:21b6d4dbce44 | 139 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 11:db27d3838514 | 140 | // If within the y range of the block |
| joshdavy | 8:21b6d4dbce44 | 141 | if (_pos.y + _height > blocks[i].first.y && |
| joshdavy | 8:21b6d4dbce44 | 142 | _pos.y < blocks[i].second.y) { |
| joshdavy | 11:db27d3838514 | 143 | // And the right edge of the player is equal to the left edge of |
| joshdavy | 11:db27d3838514 | 144 | // the block |
| joshdavy | 8:21b6d4dbce44 | 145 | if ( (_pos.x + _width) == blocks[i].first.x ) { |
| joshdavy | 11:db27d3838514 | 146 | // cant move right |
| joshdavy | 8:21b6d4dbce44 | 147 | can_move_right = false; |
| joshdavy | 8:21b6d4dbce44 | 148 | } |
| joshdavy | 8:21b6d4dbce44 | 149 | } |
| joshdavy | 8:21b6d4dbce44 | 150 | } |
| joshdavy | 8:21b6d4dbce44 | 151 | return can_move_right; |
| joshdavy | 8:21b6d4dbce44 | 152 | } |
| joshdavy | 8:21b6d4dbce44 | 153 | |
| joshdavy | 11:db27d3838514 | 154 | /** |
| joshdavy | 11:db27d3838514 | 155 | * @brief Updates the sprite depending on the current orientation/direction |
| joshdavy | 11:db27d3838514 | 156 | * @param int orientation @details Current player orientation. |
| joshdavy | 11:db27d3838514 | 157 | * @param int direction @details Current player direction. |
| joshdavy | 11:db27d3838514 | 158 | */ |
| joshdavy | 10:58cf89dd878c | 159 | void Player::update_sprite(int orientation, int direction) |
| joshdavy | 8:21b6d4dbce44 | 160 | { |
| joshdavy | 10:58cf89dd878c | 161 | // If poiting left and upright |
| joshdavy | 10:58cf89dd878c | 162 | if (direction == -1 && orientation == 1) { |
| joshdavy | 8:21b6d4dbce44 | 163 | _bitmap = (int *) player_bitmap_left; |
| joshdavy | 8:21b6d4dbce44 | 164 | } |
| joshdavy | 10:58cf89dd878c | 165 | // If poiting right and upright |
| joshdavy | 10:58cf89dd878c | 166 | if (direction == 1 && orientation == 1) { |
| joshdavy | 8:21b6d4dbce44 | 167 | _bitmap = (int *) player_bitmap_right; |
| joshdavy | 8:21b6d4dbce44 | 168 | } |
| joshdavy | 10:58cf89dd878c | 169 | // If poiting left and upside down |
| joshdavy | 10:58cf89dd878c | 170 | if (direction == -1 && orientation == -1) { |
| joshdavy | 8:21b6d4dbce44 | 171 | _bitmap = (int *) player_bitmap_left_flipped; |
| joshdavy | 8:21b6d4dbce44 | 172 | } |
| joshdavy | 10:58cf89dd878c | 173 | // If poiting right and upside down |
| joshdavy | 10:58cf89dd878c | 174 | if (direction == 1 && orientation == -1) { |
| joshdavy | 8:21b6d4dbce44 | 175 | _bitmap = (int *) player_bitmap_right_flipped; |
| joshdavy | 8:21b6d4dbce44 | 176 | } |
| joshdavy | 8:21b6d4dbce44 | 177 | |
| joshdavy | 10:58cf89dd878c | 178 | } |
| joshdavy | 8:21b6d4dbce44 | 179 | |
| joshdavy | 11:db27d3838514 | 180 | /** |
| joshdavy | 11:db27d3838514 | 181 | * @brief Processes the gamepad buttons and updates the player accordingly |
| joshdavy | 11:db27d3838514 | 182 | * @param Gamepad pad @details The gamepad object |
| joshdavy | 11:db27d3838514 | 183 | * @param Block blocks [] @details The array of blocks |
| joshdavy | 11:db27d3838514 | 184 | * @param int number_of_blocks @details The number of blocks |
| joshdavy | 11:db27d3838514 | 185 | */ |
| joshdavy | 10:58cf89dd878c | 186 | void Player::process_inputs(Gamepad &pad,Block blocks [], int number_of_blocks) |
| joshdavy | 10:58cf89dd878c | 187 | { |
| joshdavy | 10:58cf89dd878c | 188 | // If A pressed and touching a block vertically |
| joshdavy | 8:21b6d4dbce44 | 189 | if (pad.check_event(Gamepad::A_PRESSED) && |
| joshdavy | 10:58cf89dd878c | 190 | (!can_move_down(blocks,number_of_blocks) || |
| joshdavy | 10:58cf89dd878c | 191 | !can_move_up(blocks,number_of_blocks)) |
| joshdavy | 9:96969b1c6bde | 192 | ) { |
| joshdavy | 10:58cf89dd878c | 193 | // then flip the orientation |
| joshdavy | 8:21b6d4dbce44 | 194 | if (_orientation == 1) { |
| joshdavy | 8:21b6d4dbce44 | 195 | _orientation = -1; |
| joshdavy | 8:21b6d4dbce44 | 196 | } else { |
| joshdavy | 8:21b6d4dbce44 | 197 | _orientation = 1; |
| joshdavy | 8:21b6d4dbce44 | 198 | } |
| joshdavy | 8:21b6d4dbce44 | 199 | } |
| joshdavy | 10:58cf89dd878c | 200 | // If joystick is held left and not touching a block on the left |
| joshdavy | 10:58cf89dd878c | 201 | if (pad.get_coord().x < -0.7f && can_move_left(blocks,number_of_blocks)) { |
| joshdavy | 10:58cf89dd878c | 202 | // then move left and set the direction facing to -1 (left) |
| joshdavy | 10:58cf89dd878c | 203 | _pos.x -= SPEED; |
| joshdavy | 10:58cf89dd878c | 204 | _direction = -1; |
| joshdavy | 10:58cf89dd878c | 205 | } |
| joshdavy | 10:58cf89dd878c | 206 | // If joystick is held right and not touching a block on the right |
| joshdavy | 10:58cf89dd878c | 207 | if (pad.get_coord().x > 0.7f && can_move_right(blocks,number_of_blocks)) { |
| joshdavy | 10:58cf89dd878c | 208 | // then move right and set the direction facing to 1 (right) |
| joshdavy | 10:58cf89dd878c | 209 | _pos.x += SPEED; |
| joshdavy | 10:58cf89dd878c | 210 | _direction = 1; |
| joshdavy | 10:58cf89dd878c | 211 | } |
| joshdavy | 10:58cf89dd878c | 212 | } |
| joshdavy | 8:21b6d4dbce44 | 213 | |
| joshdavy | 11:db27d3838514 | 214 | /** |
| joshdavy | 11:db27d3838514 | 215 | * @brief Updates the player position due to gravity |
| joshdavy | 11:db27d3838514 | 216 | * @param Block blocks [] @details The array of blocks |
| joshdavy | 11:db27d3838514 | 217 | * @param int number_of_blocks @details The number of blocks |
| joshdavy | 11:db27d3838514 | 218 | */ |
| joshdavy | 10:58cf89dd878c | 219 | void Player::gravity(Block blocks [], int number_of_blocks) |
| joshdavy | 10:58cf89dd878c | 220 | { |
| joshdavy | 10:58cf89dd878c | 221 | // If upright and can move down gravity increases the position |
| joshdavy | 8:21b6d4dbce44 | 222 | if (_orientation == 1 && can_move_down( blocks, |
| joshdavy | 9:96969b1c6bde | 223 | number_of_blocks)) { |
| joshdavy | 9:96969b1c6bde | 224 | _pos.y += GRAVITY; |
| joshdavy | 8:21b6d4dbce44 | 225 | } |
| joshdavy | 10:58cf89dd878c | 226 | // If upside down and can move up gravity decreases the position |
| joshdavy | 9:96969b1c6bde | 227 | if (_orientation == -1 && can_move_up(blocks, |
| joshdavy | 9:96969b1c6bde | 228 | number_of_blocks)) { |
| joshdavy | 9:96969b1c6bde | 229 | _pos.y -= GRAVITY; |
| joshdavy | 8:21b6d4dbce44 | 230 | } |
| joshdavy | 9:96969b1c6bde | 231 | |
| joshdavy | 10:58cf89dd878c | 232 | } |
| joshdavy | 11:db27d3838514 | 233 | /** |
| joshdavy | 11:db27d3838514 | 234 | * @brief Updates the player. Should be called every frame. |
| joshdavy | 11:db27d3838514 | 235 | * @param Block blocks [] @details The array of blocks |
| joshdavy | 11:db27d3838514 | 236 | * @param int number_of_blocks @details The number of blocks |
| joshdavy | 11:db27d3838514 | 237 | */ |
| joshdavy | 10:58cf89dd878c | 238 | void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks) |
| joshdavy | 10:58cf89dd878c | 239 | { |
| joshdavy | 10:58cf89dd878c | 240 | process_inputs(pad,blocks,number_of_blocks); |
| joshdavy | 10:58cf89dd878c | 241 | gravity(blocks,number_of_blocks); |
| joshdavy | 10:58cf89dd878c | 242 | check_out_of_range(); |
| joshdavy | 10:58cf89dd878c | 243 | update_sprite(_orientation,_direction); |
| joshdavy | 8:21b6d4dbce44 | 244 | } |
| joshdavy | 8:21b6d4dbce44 | 245 | |
| joshdavy | 8:21b6d4dbce44 | 246 | |
| joshdavy | 8:21b6d4dbce44 | 247 |