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@8:21b6d4dbce44, 2019-04-19 (annotated)
- Committer:
- joshdavy
- Date:
- Fri Apr 19 17:54:09 2019 +0000
- Revision:
- 8:21b6d4dbce44
- Parent:
- 7:68e06dda79f7
- Child:
- 9:96969b1c6bde
Main menu added.;
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 | 8:21b6d4dbce44 | 15 | void Player::init(int height,int width,Vector2D pos) |
| joshdavy | 4:afbf3dd71403 | 16 | { |
| joshdavy | 8:21b6d4dbce44 | 17 | _height = height; |
| joshdavy | 8:21b6d4dbce44 | 18 | _width = width; |
| joshdavy | 8:21b6d4dbce44 | 19 | _pos = pos; |
| joshdavy | 8:21b6d4dbce44 | 20 | _bitmap = (int *) player_bitmap_left; // Default |
| joshdavy | 8:21b6d4dbce44 | 21 | _orientation = 1; |
| joshdavy | 8:21b6d4dbce44 | 22 | _direction = -1; |
| joshdavy | 4:afbf3dd71403 | 23 | } |
| joshdavy | 4:afbf3dd71403 | 24 | |
| joshdavy | 3:b34685dbdb8d | 25 | |
| joshdavy | 8:21b6d4dbce44 | 26 | bool Player::can_move_down(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 27 | { |
| joshdavy | 8:21b6d4dbce44 | 28 | bool can_move_down = true; |
| joshdavy | 8:21b6d4dbce44 | 29 | |
| joshdavy | 8:21b6d4dbce44 | 30 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 31 | if (_pos.x + _width > blocks[i].first.x && |
| joshdavy | 8:21b6d4dbce44 | 32 | _pos.x < blocks[i].second.x) { |
| joshdavy | 8:21b6d4dbce44 | 33 | |
| joshdavy | 8:21b6d4dbce44 | 34 | if ( (_pos.y + _height) == blocks[i].first.y ) { |
| joshdavy | 8:21b6d4dbce44 | 35 | can_move_down = false; |
| joshdavy | 8:21b6d4dbce44 | 36 | } |
| joshdavy | 8:21b6d4dbce44 | 37 | } |
| joshdavy | 8:21b6d4dbce44 | 38 | } |
| joshdavy | 8:21b6d4dbce44 | 39 | return can_move_down; |
| joshdavy | 8:21b6d4dbce44 | 40 | } |
| joshdavy | 8:21b6d4dbce44 | 41 | |
| joshdavy | 8:21b6d4dbce44 | 42 | bool Player::can_move_up(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 43 | { |
| joshdavy | 8:21b6d4dbce44 | 44 | bool can_move_up = true; |
| joshdavy | 8:21b6d4dbce44 | 45 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 46 | if (_pos.x + _width > blocks[i].first.x && |
| joshdavy | 8:21b6d4dbce44 | 47 | _pos.x < blocks[i].second.x) { |
| joshdavy | 8:21b6d4dbce44 | 48 | |
| joshdavy | 8:21b6d4dbce44 | 49 | if ( (_pos.y) == blocks[i].second.y ) { |
| joshdavy | 8:21b6d4dbce44 | 50 | can_move_up = false; |
| joshdavy | 8:21b6d4dbce44 | 51 | } |
| joshdavy | 8:21b6d4dbce44 | 52 | } |
| joshdavy | 8:21b6d4dbce44 | 53 | } |
| joshdavy | 8:21b6d4dbce44 | 54 | return can_move_up; |
| joshdavy | 8:21b6d4dbce44 | 55 | } |
| joshdavy | 8:21b6d4dbce44 | 56 | |
| joshdavy | 8:21b6d4dbce44 | 57 | bool Player::can_move_left(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 58 | { |
| joshdavy | 8:21b6d4dbce44 | 59 | bool can_move_left = true; |
| joshdavy | 8:21b6d4dbce44 | 60 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 61 | if (_pos.y + _height > blocks[i].first.y && |
| joshdavy | 8:21b6d4dbce44 | 62 | _pos.y < blocks[i].second.y) { |
| joshdavy | 8:21b6d4dbce44 | 63 | |
| joshdavy | 8:21b6d4dbce44 | 64 | if ( (_pos.x) == blocks[i].second.x ) { |
| joshdavy | 8:21b6d4dbce44 | 65 | can_move_left = false; |
| joshdavy | 8:21b6d4dbce44 | 66 | } |
| joshdavy | 8:21b6d4dbce44 | 67 | } |
| joshdavy | 8:21b6d4dbce44 | 68 | } |
| joshdavy | 8:21b6d4dbce44 | 69 | return can_move_left; |
| joshdavy | 8:21b6d4dbce44 | 70 | } |
| joshdavy | 8:21b6d4dbce44 | 71 | |
| joshdavy | 8:21b6d4dbce44 | 72 | bool Player::can_move_right(Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 73 | { |
| joshdavy | 8:21b6d4dbce44 | 74 | bool can_move_right = true; |
| joshdavy | 8:21b6d4dbce44 | 75 | |
| joshdavy | 8:21b6d4dbce44 | 76 | for (int i = 0; i < number_of_blocks; i++) { |
| joshdavy | 8:21b6d4dbce44 | 77 | if (_pos.y + _height > blocks[i].first.y && |
| joshdavy | 8:21b6d4dbce44 | 78 | _pos.y < blocks[i].second.y) { |
| joshdavy | 8:21b6d4dbce44 | 79 | |
| joshdavy | 8:21b6d4dbce44 | 80 | if ( (_pos.x + _width) == blocks[i].first.x ) { |
| joshdavy | 8:21b6d4dbce44 | 81 | can_move_right = false; |
| joshdavy | 8:21b6d4dbce44 | 82 | } |
| joshdavy | 8:21b6d4dbce44 | 83 | } |
| joshdavy | 8:21b6d4dbce44 | 84 | } |
| joshdavy | 8:21b6d4dbce44 | 85 | return can_move_right; |
| joshdavy | 8:21b6d4dbce44 | 86 | } |
| joshdavy | 8:21b6d4dbce44 | 87 | |
| joshdavy | 8:21b6d4dbce44 | 88 | void Player::update(Gamepad &pad, Block blocks [],int number_of_blocks) |
| joshdavy | 8:21b6d4dbce44 | 89 | { |
| joshdavy | 8:21b6d4dbce44 | 90 | if (_direction == -1 && _orientation == 1) { |
| joshdavy | 8:21b6d4dbce44 | 91 | _bitmap = (int *) player_bitmap_left; |
| joshdavy | 8:21b6d4dbce44 | 92 | } |
| joshdavy | 8:21b6d4dbce44 | 93 | if (_direction == 1 && _orientation == 1) { |
| joshdavy | 8:21b6d4dbce44 | 94 | _bitmap = (int *) player_bitmap_right; |
| joshdavy | 8:21b6d4dbce44 | 95 | } |
| joshdavy | 8:21b6d4dbce44 | 96 | if (_direction == -1 && _orientation == -1) { |
| joshdavy | 8:21b6d4dbce44 | 97 | _bitmap = (int *) player_bitmap_left_flipped; |
| joshdavy | 8:21b6d4dbce44 | 98 | } |
| joshdavy | 8:21b6d4dbce44 | 99 | if (_direction == 1 && _orientation == -1) { |
| joshdavy | 8:21b6d4dbce44 | 100 | _bitmap = (int *) player_bitmap_right_flipped; |
| joshdavy | 8:21b6d4dbce44 | 101 | } |
| joshdavy | 8:21b6d4dbce44 | 102 | |
| joshdavy | 8:21b6d4dbce44 | 103 | |
| joshdavy | 8:21b6d4dbce44 | 104 | if (pad.check_event(Gamepad::A_PRESSED) && |
| joshdavy | 8:21b6d4dbce44 | 105 | (!can_move_down(blocks,number_of_blocks) || !can_move_up(blocks,number_of_blocks)) |
| joshdavy | 8:21b6d4dbce44 | 106 | ) { |
| joshdavy | 8:21b6d4dbce44 | 107 | |
| joshdavy | 8:21b6d4dbce44 | 108 | if (_orientation == 1) { |
| joshdavy | 8:21b6d4dbce44 | 109 | _orientation = -1; |
| joshdavy | 8:21b6d4dbce44 | 110 | } else { |
| joshdavy | 8:21b6d4dbce44 | 111 | _orientation = 1; |
| joshdavy | 8:21b6d4dbce44 | 112 | } |
| joshdavy | 8:21b6d4dbce44 | 113 | } |
| joshdavy | 8:21b6d4dbce44 | 114 | |
| joshdavy | 8:21b6d4dbce44 | 115 | if (_orientation == 1 && can_move_down( blocks, |
| joshdavy | 8:21b6d4dbce44 | 116 | number_of_blocks)) { |
| joshdavy | 8:21b6d4dbce44 | 117 | _pos.y += GRAVITY; |
| joshdavy | 8:21b6d4dbce44 | 118 | } |
| joshdavy | 8:21b6d4dbce44 | 119 | if (_orientation == -1 && can_move_up(blocks, |
| joshdavy | 8:21b6d4dbce44 | 120 | number_of_blocks)) { |
| joshdavy | 8:21b6d4dbce44 | 121 | _pos.y -= GRAVITY; |
| joshdavy | 8:21b6d4dbce44 | 122 | } |
| joshdavy | 3:b34685dbdb8d | 123 | |
| joshdavy | 8:21b6d4dbce44 | 124 | |
| joshdavy | 8:21b6d4dbce44 | 125 | if (pad.get_coord().x < -0.7f && can_move_left(blocks,number_of_blocks)) { |
| joshdavy | 8:21b6d4dbce44 | 126 | _pos.x -= 2; |
| joshdavy | 8:21b6d4dbce44 | 127 | _direction = -1; |
| joshdavy | 8:21b6d4dbce44 | 128 | } |
| joshdavy | 8:21b6d4dbce44 | 129 | if (pad.get_coord().x > 0.7f && can_move_right(blocks,number_of_blocks)) { |
| joshdavy | 8:21b6d4dbce44 | 130 | _pos.x += 2; |
| joshdavy | 8:21b6d4dbce44 | 131 | _direction = 1; |
| joshdavy | 8:21b6d4dbce44 | 132 | } |
| joshdavy | 8:21b6d4dbce44 | 133 | |
| joshdavy | 8:21b6d4dbce44 | 134 | |
| joshdavy | 8:21b6d4dbce44 | 135 | |
| joshdavy | 8:21b6d4dbce44 | 136 | |
| joshdavy | 8:21b6d4dbce44 | 137 | } |
| joshdavy | 8:21b6d4dbce44 | 138 | |
| joshdavy | 8:21b6d4dbce44 | 139 | |
| joshdavy | 8:21b6d4dbce44 | 140 |