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.
Dependencies: mbed MotionSensor
Entity/Player/Player.cpp@30:ec915d24d3e9, 2019-05-05 (annotated)
- Committer:
- el17sm
- Date:
- Sun May 05 18:04:43 2019 +0000
- Revision:
- 30:ec915d24d3e9
- Parent:
- 29:6b8411bb040a
- Child:
- 32:fe6359ef9916
Title Screen done;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17sm | 10:1a3499f6b583 | 1 | #include "Player.h" |
| el17sm | 10:1a3499f6b583 | 2 | #include "math.h" |
| el17sm | 10:1a3499f6b583 | 3 | |
| el17sm | 13:d04a6caba40d | 4 | // Constructor |
| el17sm | 22:7abf4581bc9b | 5 | Player::Player(float pos_x, float pos_y) |
| el17sm | 22:7abf4581bc9b | 6 | { |
| el17sm | 28:98848e6a77a2 | 7 | _damage_self_upon_collision = true; |
| el17sm | 10:1a3499f6b583 | 8 | moving = false; |
| el17sm | 10:1a3499f6b583 | 9 | face = 0; |
| el17sm | 16:ddb203a74dfc | 10 | hp = 3; |
| el17sm | 23:5a8f75e93508 | 11 | attack = 1; |
| el17sm | 10:1a3499f6b583 | 12 | hitbox.width = 6; |
| el17sm | 10:1a3499f6b583 | 13 | hitbox.height = 5; |
| el17sm | 10:1a3499f6b583 | 14 | position.x = pos_x; |
| el17sm | 10:1a3499f6b583 | 15 | position.y = pos_y; |
| el17sm | 10:1a3499f6b583 | 16 | sprite_size.width = 6; |
| el17sm | 10:1a3499f6b583 | 17 | sprite_size.height = 12; |
| el17sm | 10:1a3499f6b583 | 18 | sprite_size.offset_x = 0; |
| el17sm | 10:1a3499f6b583 | 19 | sprite_size.offset_y = 7; |
| el17sm | 12:a1c1991835ca | 20 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 21 | frame.number = 0; |
| el17sm | 12:a1c1991835ca | 22 | frame.max = 4; |
| el17sm | 22:7abf4581bc9b | 23 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 22:7abf4581bc9b | 24 | valid_bullets[i] = false; |
| el17sm | 22:7abf4581bc9b | 25 | } |
| el17sm | 15:44d5cc33d389 | 26 | fire_rate_counter = 0; |
| el17sm | 23:5a8f75e93508 | 27 | invulnerability_counter = invulnerability_period; |
| el17sm | 22:7abf4581bc9b | 28 | |
| el17sm | 16:ddb203a74dfc | 29 | // Upgradable status |
| el17sm | 23:5a8f75e93508 | 30 | fire_rate_delay = 30; |
| el17sm | 23:5a8f75e93508 | 31 | velocity = 0.7; |
| el17sm | 23:5a8f75e93508 | 32 | _bullet_speed = 1; |
| el17sm | 27:a1b41626f57c | 33 | } |
| el17sm | 27:a1b41626f57c | 34 | |
| el17sm | 27:a1b41626f57c | 35 | Player::~Player() |
| el17sm | 27:a1b41626f57c | 36 | { |
| el17sm | 29:6b8411bb040a | 37 | delete_bullets(); |
| el17sm | 10:1a3499f6b583 | 38 | } |
| el17sm | 10:1a3499f6b583 | 39 | |
| el17sm | 13:d04a6caba40d | 40 | // Accessors |
| el17sm | 22:7abf4581bc9b | 41 | int Player::get_bullet_speed() |
| el17sm | 22:7abf4581bc9b | 42 | { |
| el17sm | 22:7abf4581bc9b | 43 | return _bullet_speed; |
| el17sm | 29:6b8411bb040a | 44 | } |
| el17sm | 23:5a8f75e93508 | 45 | int Player::get_hearts_width() |
| el17sm | 23:5a8f75e93508 | 46 | { |
| el17sm | 27:a1b41626f57c | 47 | return 9; |
| el17sm | 23:5a8f75e93508 | 48 | } |
| el17sm | 23:5a8f75e93508 | 49 | int Player::get_hearts_height() |
| el17sm | 23:5a8f75e93508 | 50 | { |
| el17sm | 27:a1b41626f57c | 51 | return 9; |
| el17sm | 23:5a8f75e93508 | 52 | } |
| el17sm | 23:5a8f75e93508 | 53 | int * Player::get_hearts_sprite() |
| el17sm | 23:5a8f75e93508 | 54 | { |
| el17sm | 27:a1b41626f57c | 55 | return (int *) sprite_heart; |
| el17sm | 23:5a8f75e93508 | 56 | } |
| el17sm | 13:d04a6caba40d | 57 | |
| el17sm | 13:d04a6caba40d | 58 | // Functions |
| el17sm | 29:6b8411bb040a | 59 | void Player::move(float mapped_x, float mapped_y, int * map, bool * doorways) |
| el17sm | 22:7abf4581bc9b | 60 | { |
| el17sm | 30:ec915d24d3e9 | 61 | move_player(mapped_x, mapped_y, map, doorways); |
| el17sm | 30:ec915d24d3e9 | 62 | move_bullets(); |
| el17sm | 30:ec915d24d3e9 | 63 | increment_frames(mapped_x, mapped_y); |
| el17sm | 30:ec915d24d3e9 | 64 | invulnerability_counter++; |
| el17sm | 30:ec915d24d3e9 | 65 | } |
| el17sm | 30:ec915d24d3e9 | 66 | |
| el17sm | 30:ec915d24d3e9 | 67 | void Player::move_player(float mapped_x, float mapped_y, int * map, bool * doorways) |
| el17sm | 30:ec915d24d3e9 | 68 | { |
| el17sm | 29:6b8411bb040a | 69 | if(!entity_to_map_collision_test(position.x + velocity*mapped_x, position.y, map, doorways)) { |
| el17sm | 16:ddb203a74dfc | 70 | position.x += velocity*mapped_x; |
| el17sm | 10:1a3499f6b583 | 71 | } |
| el17sm | 29:6b8411bb040a | 72 | if(!entity_to_map_collision_test(position.x, position.y - velocity*mapped_y, map, doorways)) { |
| el17sm | 16:ddb203a74dfc | 73 | position.y -= velocity*mapped_y; |
| el17sm | 10:1a3499f6b583 | 74 | } |
| el17sm | 10:1a3499f6b583 | 75 | moving = false; |
| el17sm | 30:ec915d24d3e9 | 76 | } |
| el17sm | 30:ec915d24d3e9 | 77 | |
| el17sm | 30:ec915d24d3e9 | 78 | void Player::move_bullets() |
| el17sm | 30:ec915d24d3e9 | 79 | { |
| el17sm | 30:ec915d24d3e9 | 80 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 30:ec915d24d3e9 | 81 | if (valid_bullets[i]) { |
| el17sm | 30:ec915d24d3e9 | 82 | bullets_array[i]->move(get_bullet_speed(), 0, 0, (bool *) 0); |
| el17sm | 30:ec915d24d3e9 | 83 | } |
| el17sm | 30:ec915d24d3e9 | 84 | } |
| el17sm | 30:ec915d24d3e9 | 85 | } |
| el17sm | 30:ec915d24d3e9 | 86 | |
| el17sm | 30:ec915d24d3e9 | 87 | void Player::increment_frames(float mapped_x, float mapped_y) |
| el17sm | 30:ec915d24d3e9 | 88 | { |
| el17sm | 22:7abf4581bc9b | 89 | if (abs(mapped_x) + abs(mapped_y) > 0.1f) { |
| el17sm | 10:1a3499f6b583 | 90 | moving = true; |
| el17sm | 22:7abf4581bc9b | 91 | if (mapped_y < 0 && abs(mapped_y) > abs(mapped_x)) { |
| el17sm | 10:1a3499f6b583 | 92 | face = 2; |
| el17sm | 22:7abf4581bc9b | 93 | } else if (mapped_y > 0 && abs(mapped_y) > abs(mapped_x)) { |
| el17sm | 10:1a3499f6b583 | 94 | face = 0; |
| el17sm | 22:7abf4581bc9b | 95 | } else if (mapped_x > 0 && abs(mapped_x) > abs(mapped_y)) { |
| el17sm | 10:1a3499f6b583 | 96 | face = 1; |
| el17sm | 22:7abf4581bc9b | 97 | } else if (mapped_x < 0 && abs(mapped_x) > abs(mapped_y)) { |
| el17sm | 10:1a3499f6b583 | 98 | face = 3; |
| el17sm | 10:1a3499f6b583 | 99 | } |
| el17sm | 22:7abf4581bc9b | 100 | if (frame.number < frame.max) { |
| el17sm | 12:a1c1991835ca | 101 | frame.count++; |
| el17sm | 22:7abf4581bc9b | 102 | } else { |
| el17sm | 12:a1c1991835ca | 103 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 104 | } |
| el17sm | 22:7abf4581bc9b | 105 | } else { |
| el17sm | 12:a1c1991835ca | 106 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 107 | } |
| el17sm | 23:5a8f75e93508 | 108 | frame.number = (frame.count/8) % frame.max; |
| el17sm | 23:5a8f75e93508 | 109 | } |
| el17sm | 23:5a8f75e93508 | 110 | |
| el17sm | 23:5a8f75e93508 | 111 | void Player::take_damage(int damage) |
| el17sm | 23:5a8f75e93508 | 112 | { |
| el17sm | 28:98848e6a77a2 | 113 | if (damage < 0){ |
| el17sm | 28:98848e6a77a2 | 114 | hp -= damage; |
| el17sm | 28:98848e6a77a2 | 115 | } |
| el17sm | 28:98848e6a77a2 | 116 | else if (invulnerability_counter >= invulnerability_period) { |
| el17sm | 23:5a8f75e93508 | 117 | hp -= damage; |
| el17sm | 23:5a8f75e93508 | 118 | invulnerability_counter = 0; |
| el17sm | 23:5a8f75e93508 | 119 | } |
| el17sm | 28:98848e6a77a2 | 120 | if (hp > 5) { |
| el17sm | 28:98848e6a77a2 | 121 | hp = 5; |
| el17sm | 28:98848e6a77a2 | 122 | } |
| el17sm | 12:a1c1991835ca | 123 | } |
| el17sm | 30:ec915d24d3e9 | 124 | |
| el17sm | 30:ec915d24d3e9 | 125 | bool Player::update_bullets(int * map, bool * doorways) |
| el17sm | 30:ec915d24d3e9 | 126 | { |
| el17sm | 30:ec915d24d3e9 | 127 | bool result = false; |
| el17sm | 30:ec915d24d3e9 | 128 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 30:ec915d24d3e9 | 129 | if((valid_bullets[i]) && (bullets_array[i]->out_of_bounds_check(map, doorways))) { |
| el17sm | 30:ec915d24d3e9 | 130 | valid_bullets[i] = false; |
| el17sm | 30:ec915d24d3e9 | 131 | delete bullets_array[i]; |
| el17sm | 30:ec915d24d3e9 | 132 | result = true; |
| el17sm | 30:ec915d24d3e9 | 133 | } |
| el17sm | 30:ec915d24d3e9 | 134 | } |
| el17sm | 30:ec915d24d3e9 | 135 | return result; |
| el17sm | 30:ec915d24d3e9 | 136 | } |
| el17sm | 30:ec915d24d3e9 | 137 | |
| el17sm | 30:ec915d24d3e9 | 138 | void Player::draw(N5110 &lcd) |
| el17sm | 30:ec915d24d3e9 | 139 | { |
| el17sm | 30:ec915d24d3e9 | 140 | draw_player(lcd); |
| el17sm | 30:ec915d24d3e9 | 141 | draw_bullets(lcd); |
| el17sm | 30:ec915d24d3e9 | 142 | } |
| el17sm | 30:ec915d24d3e9 | 143 | |
| el17sm | 30:ec915d24d3e9 | 144 | void Player::draw_player(N5110 &lcd) |
| el17sm | 30:ec915d24d3e9 | 145 | { |
| el17sm | 30:ec915d24d3e9 | 146 | lcd.drawSpriteTransparent(position.x-sprite_size.offset_x, |
| el17sm | 30:ec915d24d3e9 | 147 | position.y-sprite_size.offset_y, |
| el17sm | 30:ec915d24d3e9 | 148 | sprite_size.height, |
| el17sm | 30:ec915d24d3e9 | 149 | sprite_size.width, |
| el17sm | 30:ec915d24d3e9 | 150 | get_frame()); |
| el17sm | 30:ec915d24d3e9 | 151 | } |
| el17sm | 30:ec915d24d3e9 | 152 | |
| el17sm | 30:ec915d24d3e9 | 153 | void Player::draw_bullets(N5110 &lcd) |
| el17sm | 30:ec915d24d3e9 | 154 | { |
| el17sm | 30:ec915d24d3e9 | 155 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 30:ec915d24d3e9 | 156 | if (valid_bullets[i]) { |
| el17sm | 30:ec915d24d3e9 | 157 | lcd.drawSpriteTransparent(bullets_array[i]->get_pos_x()-bullets_array[i]->get_offset_x(), |
| el17sm | 30:ec915d24d3e9 | 158 | bullets_array[i]->get_pos_y()-bullets_array[i]->get_offset_y(), |
| el17sm | 30:ec915d24d3e9 | 159 | bullets_array[i]->get_sprite_height(), |
| el17sm | 30:ec915d24d3e9 | 160 | bullets_array[i]->get_sprite_width(), |
| el17sm | 30:ec915d24d3e9 | 161 | bullets_array[i]->get_frame()); |
| el17sm | 30:ec915d24d3e9 | 162 | } |
| el17sm | 30:ec915d24d3e9 | 163 | } |
| el17sm | 30:ec915d24d3e9 | 164 | } |
| el17sm | 30:ec915d24d3e9 | 165 | |
| el17sm | 29:6b8411bb040a | 166 | void Player::delete_bullets() |
| el17sm | 29:6b8411bb040a | 167 | { |
| el17sm | 29:6b8411bb040a | 168 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 29:6b8411bb040a | 169 | if (valid_bullets[i]) { |
| el17sm | 29:6b8411bb040a | 170 | delete bullets_array[i]; |
| el17sm | 29:6b8411bb040a | 171 | valid_bullets[i] = false; |
| el17sm | 29:6b8411bb040a | 172 | } |
| el17sm | 29:6b8411bb040a | 173 | } |
| el17sm | 29:6b8411bb040a | 174 | } |
| el17sm | 12:a1c1991835ca | 175 | |
| el17sm | 22:7abf4581bc9b | 176 | int * Player::get_frame() |
| el17sm | 22:7abf4581bc9b | 177 | { |
| el17sm | 23:5a8f75e93508 | 178 | if ((invulnerability_counter < invulnerability_period) && (invulnerability_counter % 10 <= 4)) { |
| el17sm | 23:5a8f75e93508 | 179 | return (int*) sprite_transparent_player; |
| el17sm | 23:5a8f75e93508 | 180 | } |
| el17sm | 12:a1c1991835ca | 181 | return (int *) sprite_player[face][frame.number]; |
| el17sm | 10:1a3499f6b583 | 182 | } |
| el17sm | 10:1a3499f6b583 | 183 | |
| el17sm | 22:7abf4581bc9b | 184 | void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X) |
| el17sm | 22:7abf4581bc9b | 185 | { |
| el17sm | 15:44d5cc33d389 | 186 | fire_rate_counter++; |
| el17sm | 22:7abf4581bc9b | 187 | if (button_Y) { |
| el17sm | 10:1a3499f6b583 | 188 | face = 0; |
| el17sm | 22:7abf4581bc9b | 189 | } else if (button_B) { |
| el17sm | 10:1a3499f6b583 | 190 | face = 1; |
| el17sm | 22:7abf4581bc9b | 191 | } else if (button_A) { |
| el17sm | 10:1a3499f6b583 | 192 | face = 2; |
| el17sm | 22:7abf4581bc9b | 193 | } else if (button_X) { |
| el17sm | 10:1a3499f6b583 | 194 | face = 3; |
| el17sm | 10:1a3499f6b583 | 195 | } |
| el17sm | 22:7abf4581bc9b | 196 | if (button_Y || button_B || button_A || button_X) { |
| el17sm | 22:7abf4581bc9b | 197 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 22:7abf4581bc9b | 198 | if (!valid_bullets[i] && (fire_rate_counter >= fire_rate_delay)) { |
| el17sm | 15:44d5cc33d389 | 199 | bullets_array[i] = new Bullets(position.x+2, position.y-2, face); |
| el17sm | 14:3361879490b2 | 200 | valid_bullets[i] = true; |
| el17sm | 15:44d5cc33d389 | 201 | fire_rate_counter = 0; |
| el17sm | 14:3361879490b2 | 202 | break; |
| el17sm | 14:3361879490b2 | 203 | } |
| el17sm | 13:d04a6caba40d | 204 | } |
| el17sm | 13:d04a6caba40d | 205 | } |
| el17sm | 10:1a3499f6b583 | 206 | } |