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@10:58cf89dd878c, 2019-05-06 (annotated)
- Committer:
- joshdavy
- Date:
- Mon May 06 10:11:42 2019 +0000
- Revision:
- 10:58cf89dd878c
- Parent:
- 9:96969b1c6bde
- Child:
- 11:db27d3838514
Main game done. Documentation to be done next.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| joshdavy | 3:b34685dbdb8d | 1 | #include "Player.h" |
| joshdavy | 3:b34685dbdb8d | 2 | |
| joshdavy | 3:b34685dbdb8d | 3 | |
| joshdavy | 3:b34685dbdb8d | 4 | |
| joshdavy | 3:b34685dbdb8d | 5 | Player::Player() |
| joshdavy | 3:b34685dbdb8d | 6 | { |
| joshdavy | 8:21b6d4dbce44 | 7 | |
| joshdavy | 3:b34685dbdb8d | 8 | } |
| joshdavy | 3:b34685dbdb8d | 9 | |
| joshdavy | 3:b34685dbdb8d | 10 | Player::~Player() |
| joshdavy | 3:b34685dbdb8d | 11 | { |
| joshdavy | 3:b34685dbdb8d | 12 | |
| joshdavy | 3:b34685dbdb8d | 13 | } |
| joshdavy | 3:b34685dbdb8d | 14 | |
| joshdavy | 10:58cf89dd878c | 15 | void Player::init(Vector2D pos) |
| joshdavy | 4:afbf3dd71403 | 16 | { |
| joshdavy | 10:58cf89dd878c | 17 | _height = PLAYER_HEIGHT; |
| joshdavy | 10:58cf89dd878c | 18 | _width = PLAYER_WIDTH; |
| joshdavy | 10:58cf89dd878c | 19 | _pos = pos; |
| joshdavy | 10:58cf89dd878c | 20 | _initial_pos = pos; |
| joshdavy | 8:21b6d4dbce44 | 21 | _bitmap = (int *) player_bitmap_left; // Default |
| joshdavy | 10:58cf89dd878c | 22 | _orientation = 1; // Upright |
| joshdavy | 10:58cf89dd878c | 23 | _direction = -1; // Left |
| joshdavy | 4:afbf3dd71403 | 24 | } |
| joshdavy | 4:afbf3dd71403 | 25 | |
| joshdavy | 9:96969b1c6bde | 26 | void Player::check_out_of_range() |
| joshdavy | 9:96969b1c6bde | 27 | { |
| joshdavy | 9:96969b1c6bde | 28 | if (_pos.x < -5 || _pos.x > 89 || _pos.y < -5 || _pos.y > 53) { |
| joshdavy | 9:96969b1c6bde | 29 | _pos = _initial_pos; |
| joshdavy | 9:96969b1c6bde | 30 | _orientation = 1; |
| joshdavy | 9:96969b1c6bde | 31 | } |
| joshdavy | 9:96969b1c6bde | 32 | } |
| joshdavy | 9:96969b1c6bde | 33 | |
| joshdavy | 10:58cf89dd878c | 34 | bool Player::check_goal_reached(Vector2D goal) |
| joshdavy | 9:96969b1c6bde | 35 | { |
| joshdavy | 9:96969b1c6bde | 36 | bool goal_reached = false; |
| joshdavy | 9:96969b1c6bde | 37 | int x_distance = abs(goal.x - _pos.x); |
| joshdavy | 9:96969b1c6bde | 38 | int y_distance = abs(goal.y - _pos.y); |
| joshdavy | 10:58cf89dd878c | 39 | |
| joshdavy | 9:96969b1c6bde | 40 | if (x_distance < 2 && y_distance < 6) { |
| joshdavy | 9:96969b1c6bde | 41 | goal_reached = true; |
| joshdavy | 9:96969b1c6bde | 42 | } |
| joshdavy | 9:96969b1c6bde | 43 | return goal_reached; |
| joshdavy | 9:96969b1c6bde | 44 | } |
| joshdavy | 3:b34685dbdb8d | 45 | |
| joshdavy | 8:21b6d4dbce44 | 46 | bool Player::can_move_down(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 47 | { |
| joshdavy | 8:21b6d4dbce44 | 48 | bool can_move_down = true; |
| joshdavy | 8:21b6d4dbce44 | 49 | |
| joshdavy | 8:21b6d4dbce44 | 50 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 51 | if (_pos.x + _width > blocks[i].first.x && |
| joshdavy | 8:21b6d4dbce44 | 52 | _pos.x < blocks[i].second.x) { |
| joshdavy | 8:21b6d4dbce44 | 53 | |
| joshdavy | 8:21b6d4dbce44 | 54 | if ( (_pos.y + _height) == blocks[i].first.y ) { |
| joshdavy | 8:21b6d4dbce44 | 55 | can_move_down = false; |
| joshdavy | 8:21b6d4dbce44 | 56 | } |
| joshdavy | 8:21b6d4dbce44 | 57 | } |
| joshdavy | 8:21b6d4dbce44 | 58 | } |
| joshdavy | 8:21b6d4dbce44 | 59 | return can_move_down; |
| joshdavy | 8:21b6d4dbce44 | 60 | } |
| joshdavy | 8:21b6d4dbce44 | 61 | |
| joshdavy | 8:21b6d4dbce44 | 62 | bool Player::can_move_up(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 63 | { |
| joshdavy | 8:21b6d4dbce44 | 64 | bool can_move_up = true; |
| joshdavy | 8:21b6d4dbce44 | 65 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 66 | if (_pos.x + _width > blocks[i].first.x && |
| joshdavy | 8:21b6d4dbce44 | 67 | _pos.x < blocks[i].second.x) { |
| joshdavy | 8:21b6d4dbce44 | 68 | |
| joshdavy | 8:21b6d4dbce44 | 69 | if ( (_pos.y) == blocks[i].second.y ) { |
| joshdavy | 8:21b6d4dbce44 | 70 | can_move_up = false; |
| joshdavy | 8:21b6d4dbce44 | 71 | } |
| joshdavy | 8:21b6d4dbce44 | 72 | } |
| joshdavy | 8:21b6d4dbce44 | 73 | } |
| joshdavy | 8:21b6d4dbce44 | 74 | return can_move_up; |
| joshdavy | 8:21b6d4dbce44 | 75 | } |
| joshdavy | 8:21b6d4dbce44 | 76 | |
| joshdavy | 8:21b6d4dbce44 | 77 | bool Player::can_move_left(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 78 | { |
| joshdavy | 8:21b6d4dbce44 | 79 | bool can_move_left = true; |
| joshdavy | 8:21b6d4dbce44 | 80 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 81 | if (_pos.y + _height > blocks[i].first.y && |
| joshdavy | 8:21b6d4dbce44 | 82 | _pos.y < blocks[i].second.y) { |
| joshdavy | 8:21b6d4dbce44 | 83 | |
| joshdavy | 8:21b6d4dbce44 | 84 | if ( (_pos.x) == blocks[i].second.x ) { |
| joshdavy | 8:21b6d4dbce44 | 85 | can_move_left = false; |
| joshdavy | 8:21b6d4dbce44 | 86 | } |
| joshdavy | 8:21b6d4dbce44 | 87 | } |
| joshdavy | 8:21b6d4dbce44 | 88 | } |
| joshdavy | 8:21b6d4dbce44 | 89 | return can_move_left; |
| joshdavy | 8:21b6d4dbce44 | 90 | } |
| joshdavy | 8:21b6d4dbce44 | 91 | |
| joshdavy | 8:21b6d4dbce44 | 92 | bool Player::can_move_right(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 93 | { |
| joshdavy | 8:21b6d4dbce44 | 94 | bool can_move_right = true; |
| joshdavy | 8:21b6d4dbce44 | 95 | |
| joshdavy | 8:21b6d4dbce44 | 96 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 97 | if (_pos.y + _height > blocks[i].first.y && |
| joshdavy | 8:21b6d4dbce44 | 98 | _pos.y < blocks[i].second.y) { |
| joshdavy | 8:21b6d4dbce44 | 99 | |
| joshdavy | 8:21b6d4dbce44 | 100 | if ( (_pos.x + _width) == blocks[i].first.x ) { |
| joshdavy | 8:21b6d4dbce44 | 101 | can_move_right = false; |
| joshdavy | 8:21b6d4dbce44 | 102 | } |
| joshdavy | 8:21b6d4dbce44 | 103 | } |
| joshdavy | 8:21b6d4dbce44 | 104 | } |
| joshdavy | 8:21b6d4dbce44 | 105 | return can_move_right; |
| joshdavy | 8:21b6d4dbce44 | 106 | } |
| joshdavy | 8:21b6d4dbce44 | 107 | |
| joshdavy | 10:58cf89dd878c | 108 | void Player::update_sprite(int orientation, int direction) |
| joshdavy | 8:21b6d4dbce44 | 109 | { |
| joshdavy | 10:58cf89dd878c | 110 | // If poiting left and upright |
| joshdavy | 10:58cf89dd878c | 111 | if (direction == -1 && orientation == 1) { |
| joshdavy | 8:21b6d4dbce44 | 112 | _bitmap = (int *) player_bitmap_left; |
| joshdavy | 8:21b6d4dbce44 | 113 | } |
| joshdavy | 10:58cf89dd878c | 114 | // If poiting right and upright |
| joshdavy | 10:58cf89dd878c | 115 | if (direction == 1 && orientation == 1) { |
| joshdavy | 8:21b6d4dbce44 | 116 | _bitmap = (int *) player_bitmap_right; |
| joshdavy | 8:21b6d4dbce44 | 117 | } |
| joshdavy | 10:58cf89dd878c | 118 | // If poiting left and upside down |
| joshdavy | 10:58cf89dd878c | 119 | if (direction == -1 && orientation == -1) { |
| joshdavy | 8:21b6d4dbce44 | 120 | _bitmap = (int *) player_bitmap_left_flipped; |
| joshdavy | 8:21b6d4dbce44 | 121 | } |
| joshdavy | 10:58cf89dd878c | 122 | // If poiting right and upside down |
| joshdavy | 10:58cf89dd878c | 123 | if (direction == 1 && orientation == -1) { |
| joshdavy | 8:21b6d4dbce44 | 124 | _bitmap = (int *) player_bitmap_right_flipped; |
| joshdavy | 8:21b6d4dbce44 | 125 | } |
| joshdavy | 8:21b6d4dbce44 | 126 | |
| joshdavy | 10:58cf89dd878c | 127 | } |
| joshdavy | 8:21b6d4dbce44 | 128 | |
| joshdavy | 10:58cf89dd878c | 129 | void Player::process_inputs(Gamepad &pad,Block blocks [], int number_of_blocks) |
| joshdavy | 10:58cf89dd878c | 130 | { |
| joshdavy | 10:58cf89dd878c | 131 | // If A pressed and touching a block vertically |
| joshdavy | 8:21b6d4dbce44 | 132 | if (pad.check_event(Gamepad::A_PRESSED) && |
| joshdavy | 10:58cf89dd878c | 133 | (!can_move_down(blocks,number_of_blocks) || |
| joshdavy | 10:58cf89dd878c | 134 | !can_move_up(blocks,number_of_blocks)) |
| joshdavy | 9:96969b1c6bde | 135 | ) { |
| joshdavy | 10:58cf89dd878c | 136 | // then flip the orientation |
| joshdavy | 8:21b6d4dbce44 | 137 | if (_orientation == 1) { |
| joshdavy | 8:21b6d4dbce44 | 138 | _orientation = -1; |
| joshdavy | 8:21b6d4dbce44 | 139 | } else { |
| joshdavy | 8:21b6d4dbce44 | 140 | _orientation = 1; |
| joshdavy | 8:21b6d4dbce44 | 141 | } |
| joshdavy | 8:21b6d4dbce44 | 142 | } |
| joshdavy | 10:58cf89dd878c | 143 | // If joystick is held left and not touching a block on the left |
| joshdavy | 10:58cf89dd878c | 144 | if (pad.get_coord().x < -0.7f && can_move_left(blocks,number_of_blocks)) { |
| joshdavy | 10:58cf89dd878c | 145 | // then move left and set the direction facing to -1 (left) |
| joshdavy | 10:58cf89dd878c | 146 | _pos.x -= SPEED; |
| joshdavy | 10:58cf89dd878c | 147 | _direction = -1; |
| joshdavy | 10:58cf89dd878c | 148 | } |
| joshdavy | 10:58cf89dd878c | 149 | // If joystick is held right and not touching a block on the right |
| joshdavy | 10:58cf89dd878c | 150 | if (pad.get_coord().x > 0.7f && can_move_right(blocks,number_of_blocks)) { |
| joshdavy | 10:58cf89dd878c | 151 | // then move right and set the direction facing to 1 (right) |
| joshdavy | 10:58cf89dd878c | 152 | _pos.x += SPEED; |
| joshdavy | 10:58cf89dd878c | 153 | _direction = 1; |
| joshdavy | 10:58cf89dd878c | 154 | } |
| joshdavy | 10:58cf89dd878c | 155 | } |
| joshdavy | 8:21b6d4dbce44 | 156 | |
| joshdavy | 10:58cf89dd878c | 157 | void Player::gravity(Block blocks [], int number_of_blocks) |
| joshdavy | 10:58cf89dd878c | 158 | { |
| joshdavy | 10:58cf89dd878c | 159 | // If upright and can move down gravity increases the position |
| joshdavy | 8:21b6d4dbce44 | 160 | if (_orientation == 1 && can_move_down( blocks, |
| joshdavy | 9:96969b1c6bde | 161 | number_of_blocks)) { |
| joshdavy | 9:96969b1c6bde | 162 | _pos.y += GRAVITY; |
| joshdavy | 8:21b6d4dbce44 | 163 | } |
| joshdavy | 10:58cf89dd878c | 164 | // If upside down and can move up gravity decreases the position |
| joshdavy | 9:96969b1c6bde | 165 | if (_orientation == -1 && can_move_up(blocks, |
| joshdavy | 9:96969b1c6bde | 166 | number_of_blocks)) { |
| joshdavy | 9:96969b1c6bde | 167 | _pos.y -= GRAVITY; |
| joshdavy | 8:21b6d4dbce44 | 168 | } |
| joshdavy | 9:96969b1c6bde | 169 | |
| joshdavy | 10:58cf89dd878c | 170 | } |
| joshdavy | 10:58cf89dd878c | 171 | void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks) |
| joshdavy | 10:58cf89dd878c | 172 | { |
| joshdavy | 10:58cf89dd878c | 173 | process_inputs(pad,blocks,number_of_blocks); |
| joshdavy | 10:58cf89dd878c | 174 | gravity(blocks,number_of_blocks); |
| joshdavy | 10:58cf89dd878c | 175 | check_out_of_range(); |
| joshdavy | 10:58cf89dd878c | 176 | update_sprite(_orientation,_direction); |
| joshdavy | 10:58cf89dd878c | 177 | |
| joshdavy | 10:58cf89dd878c | 178 | |
| joshdavy | 8:21b6d4dbce44 | 179 | |
| joshdavy | 8:21b6d4dbce44 | 180 | } |
| joshdavy | 8:21b6d4dbce44 | 181 | |
| joshdavy | 8:21b6d4dbce44 | 182 | |
| joshdavy | 8:21b6d4dbce44 | 183 |