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@38:09832e662803, 2019-05-07 (annotated)
- Committer:
- el17sm
- Date:
- Tue May 07 09:09:38 2019 +0000
- Revision:
- 38:09832e662803
- Parent:
- 37:a404860171a9
Attempted creation of mob Cowboy
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 | 38:09832e662803 | 7 | _type = 0; |
| 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 | 35:06cd6be999ad | 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 | 33:4f3948dcd2f7 | 27 | |
| el17sm | 33:4f3948dcd2f7 | 28 | invulnerability_counter = INVULNERABILITY_PERIOD; |
| el17sm | 22:7abf4581bc9b | 29 | |
| el17sm | 16:ddb203a74dfc | 30 | // Upgradable status |
| el17sm | 23:5a8f75e93508 | 31 | fire_rate_delay = 30; |
| el17sm | 23:5a8f75e93508 | 32 | velocity = 0.7; |
| el17sm | 23:5a8f75e93508 | 33 | _bullet_speed = 1; |
| el17sm | 27:a1b41626f57c | 34 | } |
| el17sm | 27:a1b41626f57c | 35 | |
| el17sm | 27:a1b41626f57c | 36 | Player::~Player() |
| el17sm | 27:a1b41626f57c | 37 | { |
| el17sm | 29:6b8411bb040a | 38 | delete_bullets(); |
| el17sm | 10:1a3499f6b583 | 39 | } |
| el17sm | 10:1a3499f6b583 | 40 | |
| el17sm | 13:d04a6caba40d | 41 | // Accessors |
| el17sm | 22:7abf4581bc9b | 42 | int Player::get_bullet_speed() |
| el17sm | 22:7abf4581bc9b | 43 | { |
| el17sm | 22:7abf4581bc9b | 44 | return _bullet_speed; |
| el17sm | 29:6b8411bb040a | 45 | } |
| el17sm | 23:5a8f75e93508 | 46 | int Player::get_hearts_width() |
| el17sm | 23:5a8f75e93508 | 47 | { |
| el17sm | 27:a1b41626f57c | 48 | return 9; |
| el17sm | 23:5a8f75e93508 | 49 | } |
| el17sm | 23:5a8f75e93508 | 50 | int Player::get_hearts_height() |
| el17sm | 23:5a8f75e93508 | 51 | { |
| el17sm | 27:a1b41626f57c | 52 | return 9; |
| el17sm | 23:5a8f75e93508 | 53 | } |
| el17sm | 33:4f3948dcd2f7 | 54 | char * Player::get_hearts_sprite() |
| el17sm | 23:5a8f75e93508 | 55 | { |
| el17sm | 33:4f3948dcd2f7 | 56 | return (char *) sprite_heart; |
| el17sm | 23:5a8f75e93508 | 57 | } |
| el17sm | 13:d04a6caba40d | 58 | |
| el17sm | 13:d04a6caba40d | 59 | // Functions |
| el17sm | 34:1d5b4da3935e | 60 | void Player::move(float mapped_x, float mapped_y, char * map, bool * doorways) |
| el17sm | 22:7abf4581bc9b | 61 | { |
| el17sm | 30:ec915d24d3e9 | 62 | move_player(mapped_x, mapped_y, map, doorways); |
| el17sm | 30:ec915d24d3e9 | 63 | move_bullets(); |
| el17sm | 30:ec915d24d3e9 | 64 | increment_frames(mapped_x, mapped_y); |
| el17sm | 30:ec915d24d3e9 | 65 | invulnerability_counter++; |
| el17sm | 30:ec915d24d3e9 | 66 | } |
| el17sm | 30:ec915d24d3e9 | 67 | |
| el17sm | 34:1d5b4da3935e | 68 | void Player::move_player(float mapped_x, float mapped_y, char * map, bool * doorways) |
| el17sm | 30:ec915d24d3e9 | 69 | { |
| el17sm | 29:6b8411bb040a | 70 | if(!entity_to_map_collision_test(position.x + velocity*mapped_x, position.y, map, doorways)) { |
| el17sm | 16:ddb203a74dfc | 71 | position.x += velocity*mapped_x; |
| el17sm | 10:1a3499f6b583 | 72 | } |
| el17sm | 29:6b8411bb040a | 73 | if(!entity_to_map_collision_test(position.x, position.y - velocity*mapped_y, map, doorways)) { |
| el17sm | 16:ddb203a74dfc | 74 | position.y -= velocity*mapped_y; |
| el17sm | 10:1a3499f6b583 | 75 | } |
| el17sm | 10:1a3499f6b583 | 76 | moving = false; |
| el17sm | 30:ec915d24d3e9 | 77 | } |
| el17sm | 30:ec915d24d3e9 | 78 | |
| el17sm | 30:ec915d24d3e9 | 79 | void Player::move_bullets() |
| el17sm | 30:ec915d24d3e9 | 80 | { |
| el17sm | 30:ec915d24d3e9 | 81 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 30:ec915d24d3e9 | 82 | if (valid_bullets[i]) { |
| el17sm | 30:ec915d24d3e9 | 83 | bullets_array[i]->move(get_bullet_speed(), 0, 0, (bool *) 0); |
| el17sm | 30:ec915d24d3e9 | 84 | } |
| el17sm | 30:ec915d24d3e9 | 85 | } |
| el17sm | 30:ec915d24d3e9 | 86 | } |
| el17sm | 30:ec915d24d3e9 | 87 | |
| el17sm | 30:ec915d24d3e9 | 88 | void Player::increment_frames(float mapped_x, float mapped_y) |
| el17sm | 30:ec915d24d3e9 | 89 | { |
| el17sm | 22:7abf4581bc9b | 90 | if (abs(mapped_x) + abs(mapped_y) > 0.1f) { |
| el17sm | 10:1a3499f6b583 | 91 | moving = true; |
| el17sm | 22:7abf4581bc9b | 92 | if (mapped_y < 0 && abs(mapped_y) > abs(mapped_x)) { |
| el17sm | 10:1a3499f6b583 | 93 | face = 2; |
| el17sm | 22:7abf4581bc9b | 94 | } else if (mapped_y > 0 && abs(mapped_y) > abs(mapped_x)) { |
| el17sm | 10:1a3499f6b583 | 95 | face = 0; |
| el17sm | 22:7abf4581bc9b | 96 | } else if (mapped_x > 0 && abs(mapped_x) > abs(mapped_y)) { |
| el17sm | 10:1a3499f6b583 | 97 | face = 1; |
| el17sm | 22:7abf4581bc9b | 98 | } else if (mapped_x < 0 && abs(mapped_x) > abs(mapped_y)) { |
| el17sm | 10:1a3499f6b583 | 99 | face = 3; |
| el17sm | 10:1a3499f6b583 | 100 | } |
| el17sm | 22:7abf4581bc9b | 101 | if (frame.number < frame.max) { |
| el17sm | 12:a1c1991835ca | 102 | frame.count++; |
| el17sm | 22:7abf4581bc9b | 103 | } else { |
| el17sm | 12:a1c1991835ca | 104 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 105 | } |
| el17sm | 22:7abf4581bc9b | 106 | } else { |
| el17sm | 12:a1c1991835ca | 107 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 108 | } |
| el17sm | 23:5a8f75e93508 | 109 | frame.number = (frame.count/8) % frame.max; |
| el17sm | 23:5a8f75e93508 | 110 | } |
| el17sm | 23:5a8f75e93508 | 111 | |
| el17sm | 23:5a8f75e93508 | 112 | void Player::take_damage(int damage) |
| el17sm | 23:5a8f75e93508 | 113 | { |
| el17sm | 28:98848e6a77a2 | 114 | if (damage < 0){ |
| el17sm | 28:98848e6a77a2 | 115 | hp -= damage; |
| el17sm | 28:98848e6a77a2 | 116 | } |
| el17sm | 33:4f3948dcd2f7 | 117 | else if (invulnerability_counter >= INVULNERABILITY_PERIOD) { |
| el17sm | 23:5a8f75e93508 | 118 | hp -= damage; |
| el17sm | 23:5a8f75e93508 | 119 | invulnerability_counter = 0; |
| el17sm | 23:5a8f75e93508 | 120 | } |
| el17sm | 28:98848e6a77a2 | 121 | if (hp > 5) { |
| el17sm | 28:98848e6a77a2 | 122 | hp = 5; |
| el17sm | 28:98848e6a77a2 | 123 | } |
| el17sm | 12:a1c1991835ca | 124 | } |
| el17sm | 30:ec915d24d3e9 | 125 | |
| el17sm | 34:1d5b4da3935e | 126 | bool Player::update_bullets(char * map, bool * doorways) |
| el17sm | 30:ec915d24d3e9 | 127 | { |
| el17sm | 30:ec915d24d3e9 | 128 | bool result = false; |
| el17sm | 30:ec915d24d3e9 | 129 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 30:ec915d24d3e9 | 130 | if((valid_bullets[i]) && (bullets_array[i]->out_of_bounds_check(map, doorways))) { |
| el17sm | 30:ec915d24d3e9 | 131 | valid_bullets[i] = false; |
| el17sm | 30:ec915d24d3e9 | 132 | delete bullets_array[i]; |
| el17sm | 30:ec915d24d3e9 | 133 | result = true; |
| el17sm | 30:ec915d24d3e9 | 134 | } |
| el17sm | 30:ec915d24d3e9 | 135 | } |
| el17sm | 30:ec915d24d3e9 | 136 | return result; |
| el17sm | 30:ec915d24d3e9 | 137 | } |
| el17sm | 30:ec915d24d3e9 | 138 | |
| el17sm | 30:ec915d24d3e9 | 139 | void Player::draw(N5110 &lcd) |
| el17sm | 30:ec915d24d3e9 | 140 | { |
| el17sm | 38:09832e662803 | 141 | // Drawing Player |
| el17sm | 35:06cd6be999ad | 142 | lcd.drawSpriteTransparent(position.x+sprite_size.offset_x, |
| el17sm | 35:06cd6be999ad | 143 | position.y+sprite_size.offset_y, |
| el17sm | 30:ec915d24d3e9 | 144 | sprite_size.height, |
| el17sm | 30:ec915d24d3e9 | 145 | sprite_size.width, |
| el17sm | 30:ec915d24d3e9 | 146 | get_frame()); |
| el17sm | 38:09832e662803 | 147 | // Drawing bullets |
| el17sm | 30:ec915d24d3e9 | 148 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 30:ec915d24d3e9 | 149 | if (valid_bullets[i]) { |
| el17sm | 32:fe6359ef9916 | 150 | bullets_array[i]->draw(lcd); |
| el17sm | 30:ec915d24d3e9 | 151 | } |
| el17sm | 30:ec915d24d3e9 | 152 | } |
| el17sm | 30:ec915d24d3e9 | 153 | } |
| el17sm | 30:ec915d24d3e9 | 154 | |
| el17sm | 29:6b8411bb040a | 155 | void Player::delete_bullets() |
| el17sm | 29:6b8411bb040a | 156 | { |
| el17sm | 29:6b8411bb040a | 157 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 29:6b8411bb040a | 158 | if (valid_bullets[i]) { |
| el17sm | 29:6b8411bb040a | 159 | delete bullets_array[i]; |
| el17sm | 29:6b8411bb040a | 160 | valid_bullets[i] = false; |
| el17sm | 29:6b8411bb040a | 161 | } |
| el17sm | 29:6b8411bb040a | 162 | } |
| el17sm | 29:6b8411bb040a | 163 | } |
| el17sm | 12:a1c1991835ca | 164 | |
| el17sm | 33:4f3948dcd2f7 | 165 | char * Player::get_frame() |
| el17sm | 22:7abf4581bc9b | 166 | { |
| el17sm | 33:4f3948dcd2f7 | 167 | if ((invulnerability_counter < INVULNERABILITY_PERIOD) && (invulnerability_counter % 10 <= 4)) { |
| el17sm | 33:4f3948dcd2f7 | 168 | return (char*) sprite_transparent_player; |
| el17sm | 23:5a8f75e93508 | 169 | } |
| el17sm | 33:4f3948dcd2f7 | 170 | return (char *) sprite_player[face][frame.number]; |
| el17sm | 10:1a3499f6b583 | 171 | } |
| el17sm | 10:1a3499f6b583 | 172 | |
| el17sm | 22:7abf4581bc9b | 173 | void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X) |
| el17sm | 22:7abf4581bc9b | 174 | { |
| el17sm | 15:44d5cc33d389 | 175 | fire_rate_counter++; |
| el17sm | 22:7abf4581bc9b | 176 | if (button_Y) { |
| el17sm | 10:1a3499f6b583 | 177 | face = 0; |
| el17sm | 22:7abf4581bc9b | 178 | } else if (button_B) { |
| el17sm | 10:1a3499f6b583 | 179 | face = 1; |
| el17sm | 22:7abf4581bc9b | 180 | } else if (button_A) { |
| el17sm | 10:1a3499f6b583 | 181 | face = 2; |
| el17sm | 22:7abf4581bc9b | 182 | } else if (button_X) { |
| el17sm | 10:1a3499f6b583 | 183 | face = 3; |
| el17sm | 10:1a3499f6b583 | 184 | } |
| el17sm | 22:7abf4581bc9b | 185 | if (button_Y || button_B || button_A || button_X) { |
| el17sm | 22:7abf4581bc9b | 186 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 22:7abf4581bc9b | 187 | if (!valid_bullets[i] && (fire_rate_counter >= fire_rate_delay)) { |
| el17sm | 15:44d5cc33d389 | 188 | bullets_array[i] = new Bullets(position.x+2, position.y-2, face); |
| el17sm | 14:3361879490b2 | 189 | valid_bullets[i] = true; |
| el17sm | 15:44d5cc33d389 | 190 | fire_rate_counter = 0; |
| el17sm | 14:3361879490b2 | 191 | break; |
| el17sm | 14:3361879490b2 | 192 | } |
| el17sm | 13:d04a6caba40d | 193 | } |
| el17sm | 13:d04a6caba40d | 194 | } |
| el17sm | 10:1a3499f6b583 | 195 | } |