Steven Mahasin / Mbed 2 deprecated DreamDungeon

Dependencies:   mbed MotionSensor

Committer:
el17sm
Date:
Thu May 09 07:47:58 2019 +0000
Revision:
52:7d05e5472022
Parent:
51:4d0cd75e7ed3
Child:
57:1c12361b6e3d
Player commenting done

Who changed what in which revision?

UserRevisionLine numberNew 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 16:ddb203a74dfc 7 hp = 3;
el17sm 23:5a8f75e93508 8 attack = 1;
el17sm 51:4d0cd75e7ed3 9 face = 2;
el17sm 51:4d0cd75e7ed3 10
el17sm 10:1a3499f6b583 11 hitbox.width = 6;
el17sm 10:1a3499f6b583 12 hitbox.height = 5;
el17sm 51:4d0cd75e7ed3 13
el17sm 10:1a3499f6b583 14 position.x = pos_x;
el17sm 10:1a3499f6b583 15 position.y = pos_y;
el17sm 51:4d0cd75e7ed3 16
el17sm 10:1a3499f6b583 17 sprite_size.width = 6;
el17sm 10:1a3499f6b583 18 sprite_size.height = 12;
el17sm 10:1a3499f6b583 19 sprite_size.offset_x = 0;
el17sm 35:06cd6be999ad 20 sprite_size.offset_y = -7;
el17sm 51:4d0cd75e7ed3 21
el17sm 12:a1c1991835ca 22 frame.count = 0;
el17sm 12:a1c1991835ca 23 frame.number = 0;
el17sm 12:a1c1991835ca 24 frame.max = 4;
el17sm 51:4d0cd75e7ed3 25
el17sm 22:7abf4581bc9b 26 for (int i = 0; i < bullets_max; i++) {
el17sm 22:7abf4581bc9b 27 valid_bullets[i] = false;
el17sm 22:7abf4581bc9b 28 }
el17sm 33:4f3948dcd2f7 29
el17sm 33:4f3948dcd2f7 30 invulnerability_counter = INVULNERABILITY_PERIOD;
el17sm 22:7abf4581bc9b 31
el17sm 16:ddb203a74dfc 32 // Upgradable status
el17sm 23:5a8f75e93508 33 fire_rate_delay = 30;
el17sm 51:4d0cd75e7ed3 34 fire_rate_counter = fire_rate_delay;
el17sm 23:5a8f75e93508 35 velocity = 0.7;
el17sm 23:5a8f75e93508 36 _bullet_speed = 1;
el17sm 27:a1b41626f57c 37 }
el17sm 27:a1b41626f57c 38
el17sm 27:a1b41626f57c 39 Player::~Player()
el17sm 27:a1b41626f57c 40 {
el17sm 29:6b8411bb040a 41 delete_bullets();
el17sm 10:1a3499f6b583 42 }
el17sm 10:1a3499f6b583 43
el17sm 13:d04a6caba40d 44 // Accessors
el17sm 22:7abf4581bc9b 45 int Player::get_bullet_speed()
el17sm 22:7abf4581bc9b 46 {
el17sm 22:7abf4581bc9b 47 return _bullet_speed;
el17sm 29:6b8411bb040a 48 }
el17sm 23:5a8f75e93508 49 int Player::get_hearts_width()
el17sm 23:5a8f75e93508 50 {
el17sm 27:a1b41626f57c 51 return 9;
el17sm 23:5a8f75e93508 52 }
el17sm 23:5a8f75e93508 53 int Player::get_hearts_height()
el17sm 23:5a8f75e93508 54 {
el17sm 27:a1b41626f57c 55 return 9;
el17sm 23:5a8f75e93508 56 }
el17sm 33:4f3948dcd2f7 57 char * Player::get_hearts_sprite()
el17sm 23:5a8f75e93508 58 {
el17sm 33:4f3948dcd2f7 59 return (char *) sprite_heart;
el17sm 23:5a8f75e93508 60 }
el17sm 13:d04a6caba40d 61
el17sm 13:d04a6caba40d 62 // Functions
el17sm 52:7d05e5472022 63 void Player::move(float mapped_x, float mapped_y, char * map, bool * doorways) // Update all bullet movement and player movement, also takes care of animation
el17sm 22:7abf4581bc9b 64 {
el17sm 30:ec915d24d3e9 65 move_player(mapped_x, mapped_y, map, doorways);
el17sm 30:ec915d24d3e9 66 move_bullets();
el17sm 52:7d05e5472022 67 increment_frames(mapped_x, mapped_y); // Sets the face of the person, and increment frame count
el17sm 52:7d05e5472022 68 invulnerability_counter++; // for damage checking
el17sm 30:ec915d24d3e9 69 }
el17sm 30:ec915d24d3e9 70
el17sm 52:7d05e5472022 71 void Player::move_player(float mapped_x, float mapped_y, char * map, bool * doorways) // Moves the player unless if the player walks onto a wall
el17sm 30:ec915d24d3e9 72 {
el17sm 41:0697508a28ba 73 update_prev_pos();
el17sm 41:0697508a28ba 74 position.y -= velocity*mapped_y;
el17sm 41:0697508a28ba 75 position.x += velocity*mapped_x;
el17sm 41:0697508a28ba 76
el17sm 41:0697508a28ba 77 undo_move_x(entity_to_map_collision_test(position.x, prev_pos.y, map, doorways));
el17sm 41:0697508a28ba 78 undo_move_y(entity_to_map_collision_test(prev_pos.x, position.y, map, doorways));
el17sm 30:ec915d24d3e9 79 }
el17sm 30:ec915d24d3e9 80
el17sm 52:7d05e5472022 81 void Player::move_bullets() // For each bullet, move them
el17sm 30:ec915d24d3e9 82 {
el17sm 30:ec915d24d3e9 83 for (int i = 0; i < bullets_max; i++) {
el17sm 30:ec915d24d3e9 84 if (valid_bullets[i]) {
el17sm 30:ec915d24d3e9 85 bullets_array[i]->move(get_bullet_speed(), 0, 0, (bool *) 0);
el17sm 30:ec915d24d3e9 86 }
el17sm 30:ec915d24d3e9 87 }
el17sm 30:ec915d24d3e9 88 }
el17sm 30:ec915d24d3e9 89
el17sm 30:ec915d24d3e9 90 void Player::increment_frames(float mapped_x, float mapped_y)
el17sm 30:ec915d24d3e9 91 {
el17sm 52:7d05e5472022 92 if (abs(mapped_x) + abs(mapped_y) > 0.1f) { // If player is moving
el17sm 22:7abf4581bc9b 93 if (mapped_y < 0 && abs(mapped_y) > abs(mapped_x)) {
el17sm 10:1a3499f6b583 94 face = 2;
el17sm 22:7abf4581bc9b 95 } else if (mapped_y > 0 && abs(mapped_y) > abs(mapped_x)) {
el17sm 10:1a3499f6b583 96 face = 0;
el17sm 22:7abf4581bc9b 97 } else if (mapped_x > 0 && abs(mapped_x) > abs(mapped_y)) {
el17sm 10:1a3499f6b583 98 face = 1;
el17sm 22:7abf4581bc9b 99 } else if (mapped_x < 0 && abs(mapped_x) > abs(mapped_y)) {
el17sm 10:1a3499f6b583 100 face = 3;
el17sm 10:1a3499f6b583 101 }
el17sm 52:7d05e5472022 102 if (frame.number < frame.max) { // Animate frames by incrementing and reseting frames
el17sm 12:a1c1991835ca 103 frame.count++;
el17sm 22:7abf4581bc9b 104 } else {
el17sm 12:a1c1991835ca 105 frame.count = 0;
el17sm 12:a1c1991835ca 106 }
el17sm 22:7abf4581bc9b 107 } else {
el17sm 52:7d05e5472022 108 frame.count = 0; // If the player is not moving, don't animate
el17sm 12:a1c1991835ca 109 }
el17sm 52:7d05e5472022 110 frame.number = (frame.count/8) % frame.max; // Frame number is used in chosing sprite-frame for animation; the constant 8 is the number of frames per sprite-frame
el17sm 23:5a8f75e93508 111 }
el17sm 23:5a8f75e93508 112
el17sm 52:7d05e5472022 113 void Player::take_damage(int damage) // Takes damage unless if player just got damaged within invulnerability period
el17sm 23:5a8f75e93508 114 {
el17sm 28:98848e6a77a2 115 if (damage < 0){
el17sm 28:98848e6a77a2 116 hp -= damage;
el17sm 28:98848e6a77a2 117 }
el17sm 33:4f3948dcd2f7 118 else if (invulnerability_counter >= INVULNERABILITY_PERIOD) {
el17sm 23:5a8f75e93508 119 hp -= damage;
el17sm 23:5a8f75e93508 120 invulnerability_counter = 0;
el17sm 23:5a8f75e93508 121 }
el17sm 52:7d05e5472022 122 if (hp > 5) { // Max HP is a constant 5, this might be an upgradable status later
el17sm 28:98848e6a77a2 123 hp = 5;
el17sm 28:98848e6a77a2 124 }
el17sm 12:a1c1991835ca 125 }
el17sm 30:ec915d24d3e9 126
el17sm 52:7d05e5472022 127 bool Player::delete_out_of_bounds_bullets(char * map, bool * doorways) // Attempts to delete bullets that are out of bounds or colliding with the wall, returns true if any bullets are deleted
el17sm 30:ec915d24d3e9 128 {
el17sm 30:ec915d24d3e9 129 bool result = false;
el17sm 30:ec915d24d3e9 130 for (int i = 0; i < bullets_max; i++) {
el17sm 30:ec915d24d3e9 131 if((valid_bullets[i]) && (bullets_array[i]->out_of_bounds_check(map, doorways))) {
el17sm 30:ec915d24d3e9 132 valid_bullets[i] = false;
el17sm 30:ec915d24d3e9 133 delete bullets_array[i];
el17sm 30:ec915d24d3e9 134 result = true;
el17sm 30:ec915d24d3e9 135 }
el17sm 30:ec915d24d3e9 136 }
el17sm 30:ec915d24d3e9 137 return result;
el17sm 30:ec915d24d3e9 138 }
el17sm 30:ec915d24d3e9 139
el17sm 30:ec915d24d3e9 140 void Player::draw(N5110 &lcd)
el17sm 30:ec915d24d3e9 141 {
el17sm 30:ec915d24d3e9 142 draw_player(lcd);
el17sm 30:ec915d24d3e9 143 }
el17sm 30:ec915d24d3e9 144
el17sm 30:ec915d24d3e9 145 void Player::draw_player(N5110 &lcd)
el17sm 30:ec915d24d3e9 146 {
el17sm 35:06cd6be999ad 147 lcd.drawSpriteTransparent(position.x+sprite_size.offset_x,
el17sm 35:06cd6be999ad 148 position.y+sprite_size.offset_y,
el17sm 30:ec915d24d3e9 149 sprite_size.height,
el17sm 30:ec915d24d3e9 150 sprite_size.width,
el17sm 30:ec915d24d3e9 151 get_frame());
el17sm 30:ec915d24d3e9 152 }
el17sm 30:ec915d24d3e9 153
el17sm 40:cbcbf6fc1421 154 void Player::draw_bullets(N5110 &lcd, int j)
el17sm 30:ec915d24d3e9 155 {
el17sm 30:ec915d24d3e9 156 for (int i = 0; i < bullets_max; i++) {
el17sm 40:cbcbf6fc1421 157 if ((valid_bullets[i]) && (bullets_array[i]->get_pos_y() == j)) {
el17sm 32:fe6359ef9916 158 bullets_array[i]->draw(lcd);
el17sm 30:ec915d24d3e9 159 }
el17sm 30:ec915d24d3e9 160 }
el17sm 30:ec915d24d3e9 161 }
el17sm 30:ec915d24d3e9 162
el17sm 52:7d05e5472022 163 void Player::delete_bullets() // Delete all bullets, normally used in unloading
el17sm 29:6b8411bb040a 164 {
el17sm 29:6b8411bb040a 165 for (int i = 0; i < bullets_max; i++) {
el17sm 29:6b8411bb040a 166 if (valid_bullets[i]) {
el17sm 29:6b8411bb040a 167 delete bullets_array[i];
el17sm 29:6b8411bb040a 168 valid_bullets[i] = false;
el17sm 29:6b8411bb040a 169 }
el17sm 29:6b8411bb040a 170 }
el17sm 29:6b8411bb040a 171 }
el17sm 12:a1c1991835ca 172
el17sm 52:7d05e5472022 173 char * Player::get_frame() // Returns the current frame's sprite pointer
el17sm 22:7abf4581bc9b 174 {
el17sm 33:4f3948dcd2f7 175 if ((invulnerability_counter < INVULNERABILITY_PERIOD) && (invulnerability_counter % 10 <= 4)) {
el17sm 33:4f3948dcd2f7 176 return (char*) sprite_transparent_player;
el17sm 23:5a8f75e93508 177 }
el17sm 33:4f3948dcd2f7 178 return (char *) sprite_player[face][frame.number];
el17sm 10:1a3499f6b583 179 }
el17sm 10:1a3499f6b583 180
el17sm 52:7d05e5472022 181 void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X) // Summons new bullets and overloads the player face when buttons are pressed
el17sm 22:7abf4581bc9b 182 {
el17sm 15:44d5cc33d389 183 fire_rate_counter++;
el17sm 22:7abf4581bc9b 184 if (button_Y) {
el17sm 10:1a3499f6b583 185 face = 0;
el17sm 22:7abf4581bc9b 186 } else if (button_B) {
el17sm 10:1a3499f6b583 187 face = 1;
el17sm 22:7abf4581bc9b 188 } else if (button_A) {
el17sm 10:1a3499f6b583 189 face = 2;
el17sm 22:7abf4581bc9b 190 } else if (button_X) {
el17sm 10:1a3499f6b583 191 face = 3;
el17sm 10:1a3499f6b583 192 }
el17sm 22:7abf4581bc9b 193 if (button_Y || button_B || button_A || button_X) {
el17sm 22:7abf4581bc9b 194 for (int i = 0; i < bullets_max; i++) {
el17sm 52:7d05e5472022 195 if (!valid_bullets[i] && (fire_rate_counter >= fire_rate_delay)) { // waits until fire_rate_delay is done before creating a bullet in an invalid slot of bullet_array
el17sm 41:0697508a28ba 196 bullets_array[i] = new Bullets(position.x+2, position.y+2, face);
el17sm 14:3361879490b2 197 valid_bullets[i] = true;
el17sm 15:44d5cc33d389 198 fire_rate_counter = 0;
el17sm 14:3361879490b2 199 break;
el17sm 14:3361879490b2 200 }
el17sm 13:d04a6caba40d 201 }
el17sm 13:d04a6caba40d 202 }
el17sm 10:1a3499f6b583 203 }