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@23:5a8f75e93508, 2019-04-25 (annotated)
- Committer:
- el17sm
- Date:
- Thu Apr 25 23:15:44 2019 +0000
- Revision:
- 23:5a8f75e93508
- Parent:
- 22:7abf4581bc9b
- Child:
- 26:abbc19edc5c1
Added Health;
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 | 10:1a3499f6b583 | 7 | moving = false; |
| el17sm | 10:1a3499f6b583 | 8 | face = 0; |
| el17sm | 16:ddb203a74dfc | 9 | hp = 3; |
| el17sm | 23:5a8f75e93508 | 10 | attack = 1; |
| el17sm | 10:1a3499f6b583 | 11 | hitbox.width = 6; |
| el17sm | 10:1a3499f6b583 | 12 | hitbox.height = 5; |
| el17sm | 10:1a3499f6b583 | 13 | position.x = pos_x; |
| el17sm | 10:1a3499f6b583 | 14 | position.y = pos_y; |
| el17sm | 10:1a3499f6b583 | 15 | sprite_size.width = 6; |
| el17sm | 10:1a3499f6b583 | 16 | sprite_size.height = 12; |
| el17sm | 10:1a3499f6b583 | 17 | sprite_size.offset_x = 0; |
| el17sm | 10:1a3499f6b583 | 18 | sprite_size.offset_y = 7; |
| el17sm | 12:a1c1991835ca | 19 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 20 | frame.number = 0; |
| el17sm | 12:a1c1991835ca | 21 | frame.max = 4; |
| el17sm | 22:7abf4581bc9b | 22 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 22:7abf4581bc9b | 23 | valid_bullets[i] = false; |
| el17sm | 22:7abf4581bc9b | 24 | } |
| el17sm | 15:44d5cc33d389 | 25 | fire_rate_counter = 0; |
| el17sm | 23:5a8f75e93508 | 26 | invulnerability_counter = invulnerability_period; |
| el17sm | 22:7abf4581bc9b | 27 | |
| el17sm | 16:ddb203a74dfc | 28 | // Upgradable status |
| el17sm | 23:5a8f75e93508 | 29 | fire_rate_delay = 30; |
| el17sm | 23:5a8f75e93508 | 30 | velocity = 0.7; |
| el17sm | 23:5a8f75e93508 | 31 | _bullet_speed = 1; |
| el17sm | 23:5a8f75e93508 | 32 | |
| el17sm | 10:1a3499f6b583 | 33 | } |
| el17sm | 10:1a3499f6b583 | 34 | |
| el17sm | 13:d04a6caba40d | 35 | // Accessors |
| el17sm | 22:7abf4581bc9b | 36 | int Player::get_bullet_speed() |
| el17sm | 22:7abf4581bc9b | 37 | { |
| el17sm | 22:7abf4581bc9b | 38 | return _bullet_speed; |
| el17sm | 22:7abf4581bc9b | 39 | }; |
| el17sm | 23:5a8f75e93508 | 40 | int Player::get_hearts_width() |
| el17sm | 23:5a8f75e93508 | 41 | { |
| el17sm | 23:5a8f75e93508 | 42 | return hearts.width; |
| el17sm | 23:5a8f75e93508 | 43 | } |
| el17sm | 23:5a8f75e93508 | 44 | int Player::get_hearts_height() |
| el17sm | 23:5a8f75e93508 | 45 | { |
| el17sm | 23:5a8f75e93508 | 46 | return hearts.height; |
| el17sm | 23:5a8f75e93508 | 47 | } |
| el17sm | 23:5a8f75e93508 | 48 | int * Player::get_hearts_sprite() |
| el17sm | 23:5a8f75e93508 | 49 | { |
| el17sm | 23:5a8f75e93508 | 50 | return hearts.sprite; |
| el17sm | 23:5a8f75e93508 | 51 | } |
| el17sm | 13:d04a6caba40d | 52 | |
| el17sm | 13:d04a6caba40d | 53 | // Functions |
| el17sm | 22:7abf4581bc9b | 54 | void Player::move(float mapped_x, float mapped_y) |
| el17sm | 22:7abf4581bc9b | 55 | { |
| el17sm | 22:7abf4581bc9b | 56 | if(!matrix_collision_test(position.x + velocity*mapped_x, position.y, 0)) { |
| el17sm | 16:ddb203a74dfc | 57 | position.x += velocity*mapped_x; |
| el17sm | 10:1a3499f6b583 | 58 | } |
| el17sm | 22:7abf4581bc9b | 59 | if(!matrix_collision_test(position.x, position.y - velocity*mapped_y, 0)) { |
| el17sm | 16:ddb203a74dfc | 60 | position.y -= velocity*mapped_y; |
| el17sm | 10:1a3499f6b583 | 61 | } |
| el17sm | 10:1a3499f6b583 | 62 | moving = false; |
| el17sm | 22:7abf4581bc9b | 63 | if (abs(mapped_x) + abs(mapped_y) > 0.1f) { |
| el17sm | 10:1a3499f6b583 | 64 | moving = true; |
| el17sm | 22:7abf4581bc9b | 65 | if (mapped_y < 0 && abs(mapped_y) > abs(mapped_x)) { |
| el17sm | 10:1a3499f6b583 | 66 | face = 2; |
| el17sm | 22:7abf4581bc9b | 67 | } else if (mapped_y > 0 && abs(mapped_y) > abs(mapped_x)) { |
| el17sm | 10:1a3499f6b583 | 68 | face = 0; |
| el17sm | 22:7abf4581bc9b | 69 | } else if (mapped_x > 0 && abs(mapped_x) > abs(mapped_y)) { |
| el17sm | 10:1a3499f6b583 | 70 | face = 1; |
| el17sm | 22:7abf4581bc9b | 71 | } else if (mapped_x < 0 && abs(mapped_x) > abs(mapped_y)) { |
| el17sm | 10:1a3499f6b583 | 72 | face = 3; |
| el17sm | 10:1a3499f6b583 | 73 | } |
| el17sm | 22:7abf4581bc9b | 74 | if (frame.number < frame.max) { |
| el17sm | 12:a1c1991835ca | 75 | frame.count++; |
| el17sm | 22:7abf4581bc9b | 76 | } else { |
| el17sm | 12:a1c1991835ca | 77 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 78 | } |
| el17sm | 22:7abf4581bc9b | 79 | } else { |
| el17sm | 12:a1c1991835ca | 80 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 81 | } |
| el17sm | 23:5a8f75e93508 | 82 | frame.number = (frame.count/8) % frame.max; |
| el17sm | 23:5a8f75e93508 | 83 | invulnerability_counter++; |
| el17sm | 23:5a8f75e93508 | 84 | } |
| el17sm | 23:5a8f75e93508 | 85 | |
| el17sm | 23:5a8f75e93508 | 86 | void Player::take_damage(int damage) |
| el17sm | 23:5a8f75e93508 | 87 | { |
| el17sm | 23:5a8f75e93508 | 88 | if (invulnerability_counter >= invulnerability_period) { |
| el17sm | 23:5a8f75e93508 | 89 | hp -= damage; |
| el17sm | 23:5a8f75e93508 | 90 | invulnerability_counter = 0; |
| el17sm | 23:5a8f75e93508 | 91 | } |
| el17sm | 12:a1c1991835ca | 92 | } |
| el17sm | 12:a1c1991835ca | 93 | |
| el17sm | 22:7abf4581bc9b | 94 | int * Player::get_frame() |
| el17sm | 22:7abf4581bc9b | 95 | { |
| el17sm | 23:5a8f75e93508 | 96 | if ((invulnerability_counter < invulnerability_period) && (invulnerability_counter % 10 <= 4)) { |
| el17sm | 23:5a8f75e93508 | 97 | return (int*) sprite_transparent_player; |
| el17sm | 23:5a8f75e93508 | 98 | } |
| el17sm | 12:a1c1991835ca | 99 | return (int *) sprite_player[face][frame.number]; |
| el17sm | 10:1a3499f6b583 | 100 | } |
| el17sm | 10:1a3499f6b583 | 101 | |
| el17sm | 22:7abf4581bc9b | 102 | void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X) |
| el17sm | 22:7abf4581bc9b | 103 | { |
| el17sm | 15:44d5cc33d389 | 104 | fire_rate_counter++; |
| el17sm | 22:7abf4581bc9b | 105 | if (button_Y) { |
| el17sm | 10:1a3499f6b583 | 106 | face = 0; |
| el17sm | 22:7abf4581bc9b | 107 | } else if (button_B) { |
| el17sm | 10:1a3499f6b583 | 108 | face = 1; |
| el17sm | 22:7abf4581bc9b | 109 | } else if (button_A) { |
| el17sm | 10:1a3499f6b583 | 110 | face = 2; |
| el17sm | 22:7abf4581bc9b | 111 | } else if (button_X) { |
| el17sm | 10:1a3499f6b583 | 112 | face = 3; |
| el17sm | 10:1a3499f6b583 | 113 | } |
| el17sm | 22:7abf4581bc9b | 114 | if (button_Y || button_B || button_A || button_X) { |
| el17sm | 22:7abf4581bc9b | 115 | for (int i = 0; i < bullets_max; i++) { |
| el17sm | 22:7abf4581bc9b | 116 | if (!valid_bullets[i] && (fire_rate_counter >= fire_rate_delay)) { |
| el17sm | 15:44d5cc33d389 | 117 | bullets_array[i] = new Bullets(position.x+2, position.y-2, face); |
| el17sm | 14:3361879490b2 | 118 | valid_bullets[i] = true; |
| el17sm | 15:44d5cc33d389 | 119 | fire_rate_counter = 0; |
| el17sm | 14:3361879490b2 | 120 | break; |
| el17sm | 14:3361879490b2 | 121 | } |
| el17sm | 13:d04a6caba40d | 122 | } |
| el17sm | 13:d04a6caba40d | 123 | } |
| el17sm | 10:1a3499f6b583 | 124 | } |
| el17sm | 23:5a8f75e93508 | 125 | |
| el17sm | 23:5a8f75e93508 | 126 | void Player::update_hearts() |
| el17sm | 23:5a8f75e93508 | 127 | { |
| el17sm | 23:5a8f75e93508 | 128 | hearts.width = (hp * 10) - 1; |
| el17sm | 23:5a8f75e93508 | 129 | hearts.height = 9; |
| el17sm | 23:5a8f75e93508 | 130 | int hearts_sprite[hearts.height][hearts.width]; |
| el17sm | 23:5a8f75e93508 | 131 | for (int j = 0; j < 9; j++) { |
| el17sm | 23:5a8f75e93508 | 132 | for (int n = 0; n < hp; n++) { |
| el17sm | 23:5a8f75e93508 | 133 | for (int i = 0; i < 9; i++){ |
| el17sm | 23:5a8f75e93508 | 134 | hearts_sprite[j][i + (n * 10)] = sprite_heart[j][i]; |
| el17sm | 23:5a8f75e93508 | 135 | } |
| el17sm | 23:5a8f75e93508 | 136 | if (n != hp) { |
| el17sm | 23:5a8f75e93508 | 137 | hearts_sprite[j][9 + (n * 10)] = 2; |
| el17sm | 23:5a8f75e93508 | 138 | } |
| el17sm | 23:5a8f75e93508 | 139 | } |
| el17sm | 23:5a8f75e93508 | 140 | } |
| el17sm | 23:5a8f75e93508 | 141 | hearts.sprite = * hearts_sprite; |
| el17sm | 23:5a8f75e93508 | 142 | } |
| el17sm | 23:5a8f75e93508 | 143 | |
| el17sm | 23:5a8f75e93508 | 144 |