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