Steven Mahasin / Mbed 2 deprecated DreamDungeon

Dependencies:   mbed MotionSensor

Committer:
el17sm
Date:
Mon Apr 29 10:39:09 2019 +0000
Revision:
27:a1b41626f57c
Parent:
26:abbc19edc5c1
Child:
28:98848e6a77a2
Functional Room; Great Success;

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 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 27:a1b41626f57c 32 }
el17sm 27:a1b41626f57c 33
el17sm 27:a1b41626f57c 34 Player::~Player()
el17sm 27:a1b41626f57c 35 {
el17sm 27:a1b41626f57c 36 for (int i = 0; i < bullets_max; i++) {
el17sm 27:a1b41626f57c 37 if (valid_bullets[i]) {
el17sm 27:a1b41626f57c 38 delete bullets_array[i];
el17sm 27:a1b41626f57c 39 }
el17sm 27:a1b41626f57c 40 }
el17sm 10:1a3499f6b583 41 }
el17sm 10:1a3499f6b583 42
el17sm 13:d04a6caba40d 43 // Accessors
el17sm 22:7abf4581bc9b 44 int Player::get_bullet_speed()
el17sm 22:7abf4581bc9b 45 {
el17sm 22:7abf4581bc9b 46 return _bullet_speed;
el17sm 22:7abf4581bc9b 47 };
el17sm 23:5a8f75e93508 48 int Player::get_hearts_width()
el17sm 23:5a8f75e93508 49 {
el17sm 27:a1b41626f57c 50 return 9;
el17sm 23:5a8f75e93508 51 }
el17sm 23:5a8f75e93508 52 int Player::get_hearts_height()
el17sm 23:5a8f75e93508 53 {
el17sm 27:a1b41626f57c 54 return 9;
el17sm 23:5a8f75e93508 55 }
el17sm 23:5a8f75e93508 56 int * Player::get_hearts_sprite()
el17sm 23:5a8f75e93508 57 {
el17sm 27:a1b41626f57c 58 return (int *) sprite_heart;
el17sm 23:5a8f75e93508 59 }
el17sm 13:d04a6caba40d 60
el17sm 13:d04a6caba40d 61 // Functions
el17sm 27:a1b41626f57c 62 void Player::move(float mapped_x, float mapped_y, int * map)
el17sm 22:7abf4581bc9b 63 {
el17sm 27:a1b41626f57c 64 if(!matrix_collision_test(position.x + velocity*mapped_x, position.y, map)) {
el17sm 16:ddb203a74dfc 65 position.x += velocity*mapped_x;
el17sm 10:1a3499f6b583 66 }
el17sm 27:a1b41626f57c 67 if(!matrix_collision_test(position.x, position.y - velocity*mapped_y, map)) {
el17sm 16:ddb203a74dfc 68 position.y -= velocity*mapped_y;
el17sm 10:1a3499f6b583 69 }
el17sm 10:1a3499f6b583 70 moving = false;
el17sm 22:7abf4581bc9b 71 if (abs(mapped_x) + abs(mapped_y) > 0.1f) {
el17sm 10:1a3499f6b583 72 moving = true;
el17sm 22:7abf4581bc9b 73 if (mapped_y < 0 && abs(mapped_y) > abs(mapped_x)) {
el17sm 10:1a3499f6b583 74 face = 2;
el17sm 22:7abf4581bc9b 75 } else if (mapped_y > 0 && abs(mapped_y) > abs(mapped_x)) {
el17sm 10:1a3499f6b583 76 face = 0;
el17sm 22:7abf4581bc9b 77 } else if (mapped_x > 0 && abs(mapped_x) > abs(mapped_y)) {
el17sm 10:1a3499f6b583 78 face = 1;
el17sm 22:7abf4581bc9b 79 } else if (mapped_x < 0 && abs(mapped_x) > abs(mapped_y)) {
el17sm 10:1a3499f6b583 80 face = 3;
el17sm 10:1a3499f6b583 81 }
el17sm 22:7abf4581bc9b 82 if (frame.number < frame.max) {
el17sm 12:a1c1991835ca 83 frame.count++;
el17sm 22:7abf4581bc9b 84 } else {
el17sm 12:a1c1991835ca 85 frame.count = 0;
el17sm 12:a1c1991835ca 86 }
el17sm 22:7abf4581bc9b 87 } else {
el17sm 12:a1c1991835ca 88 frame.count = 0;
el17sm 12:a1c1991835ca 89 }
el17sm 23:5a8f75e93508 90 frame.number = (frame.count/8) % frame.max;
el17sm 23:5a8f75e93508 91 invulnerability_counter++;
el17sm 23:5a8f75e93508 92 }
el17sm 23:5a8f75e93508 93
el17sm 23:5a8f75e93508 94 void Player::take_damage(int damage)
el17sm 23:5a8f75e93508 95 {
el17sm 23:5a8f75e93508 96 if (invulnerability_counter >= invulnerability_period) {
el17sm 23:5a8f75e93508 97 hp -= damage;
el17sm 23:5a8f75e93508 98 invulnerability_counter = 0;
el17sm 23:5a8f75e93508 99 }
el17sm 12:a1c1991835ca 100 }
el17sm 12:a1c1991835ca 101
el17sm 22:7abf4581bc9b 102 int * Player::get_frame()
el17sm 22:7abf4581bc9b 103 {
el17sm 23:5a8f75e93508 104 if ((invulnerability_counter < invulnerability_period) && (invulnerability_counter % 10 <= 4)) {
el17sm 23:5a8f75e93508 105 return (int*) sprite_transparent_player;
el17sm 23:5a8f75e93508 106 }
el17sm 12:a1c1991835ca 107 return (int *) sprite_player[face][frame.number];
el17sm 10:1a3499f6b583 108 }
el17sm 10:1a3499f6b583 109
el17sm 22:7abf4581bc9b 110 void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X)
el17sm 22:7abf4581bc9b 111 {
el17sm 15:44d5cc33d389 112 fire_rate_counter++;
el17sm 22:7abf4581bc9b 113 if (button_Y) {
el17sm 10:1a3499f6b583 114 face = 0;
el17sm 22:7abf4581bc9b 115 } else if (button_B) {
el17sm 10:1a3499f6b583 116 face = 1;
el17sm 22:7abf4581bc9b 117 } else if (button_A) {
el17sm 10:1a3499f6b583 118 face = 2;
el17sm 22:7abf4581bc9b 119 } else if (button_X) {
el17sm 10:1a3499f6b583 120 face = 3;
el17sm 10:1a3499f6b583 121 }
el17sm 22:7abf4581bc9b 122 if (button_Y || button_B || button_A || button_X) {
el17sm 22:7abf4581bc9b 123 for (int i = 0; i < bullets_max; i++) {
el17sm 22:7abf4581bc9b 124 if (!valid_bullets[i] && (fire_rate_counter >= fire_rate_delay)) {
el17sm 15:44d5cc33d389 125 bullets_array[i] = new Bullets(position.x+2, position.y-2, face);
el17sm 14:3361879490b2 126 valid_bullets[i] = true;
el17sm 15:44d5cc33d389 127 fire_rate_counter = 0;
el17sm 14:3361879490b2 128 break;
el17sm 14:3361879490b2 129 }
el17sm 13:d04a6caba40d 130 }
el17sm 13:d04a6caba40d 131 }
el17sm 10:1a3499f6b583 132 }