Steven Mahasin / Mbed 2 deprecated DreamDungeon

Dependencies:   mbed MotionSensor

Committer:
el17sm
Date:
Thu Apr 25 05:53:30 2019 +0000
Revision:
22:7abf4581bc9b
Parent:
16:ddb203a74dfc
Child:
23:5a8f75e93508
Snakes 90% done, need to change entity to group of entity collision into repulsion

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 10:1a3499f6b583 10 hitbox.width = 6;
el17sm 10:1a3499f6b583 11 hitbox.height = 5;
el17sm 10:1a3499f6b583 12 position.x = pos_x;
el17sm 10:1a3499f6b583 13 position.y = pos_y;
el17sm 10:1a3499f6b583 14 sprite_size.width = 6;
el17sm 10:1a3499f6b583 15 sprite_size.height = 12;
el17sm 10:1a3499f6b583 16 sprite_size.offset_x = 0;
el17sm 10:1a3499f6b583 17 sprite_size.offset_y = 7;
el17sm 12:a1c1991835ca 18 frame.count = 0;
el17sm 12:a1c1991835ca 19 frame.number = 0;
el17sm 12:a1c1991835ca 20 frame.max = 4;
el17sm 22:7abf4581bc9b 21 for (int i = 0; i < bullets_max; i++) {
el17sm 22:7abf4581bc9b 22 valid_bullets[i] = false;
el17sm 22:7abf4581bc9b 23 }
el17sm 15:44d5cc33d389 24 fire_rate_counter = 0;
el17sm 22:7abf4581bc9b 25
el17sm 16:ddb203a74dfc 26 // Upgradable status
el17sm 22:7abf4581bc9b 27 fire_rate_delay = 3;
el17sm 16:ddb203a74dfc 28 velocity = 1.4;
el17sm 22:7abf4581bc9b 29 _bullet_speed = 2;
el17sm 10:1a3499f6b583 30 }
el17sm 10:1a3499f6b583 31
el17sm 13:d04a6caba40d 32 // Accessors
el17sm 22:7abf4581bc9b 33 int Player::get_attack()
el17sm 22:7abf4581bc9b 34 {
el17sm 22:7abf4581bc9b 35 return 1;
el17sm 22:7abf4581bc9b 36 };
el17sm 22:7abf4581bc9b 37
el17sm 22:7abf4581bc9b 38 int Player::get_bullet_speed()
el17sm 22:7abf4581bc9b 39 {
el17sm 22:7abf4581bc9b 40 return _bullet_speed;
el17sm 22:7abf4581bc9b 41 };
el17sm 13:d04a6caba40d 42
el17sm 13:d04a6caba40d 43 // Functions
el17sm 22:7abf4581bc9b 44 void Player::move(float mapped_x, float mapped_y)
el17sm 22:7abf4581bc9b 45 {
el17sm 22:7abf4581bc9b 46 if(!matrix_collision_test(position.x + velocity*mapped_x, position.y, 0)) {
el17sm 16:ddb203a74dfc 47 position.x += velocity*mapped_x;
el17sm 10:1a3499f6b583 48 }
el17sm 22:7abf4581bc9b 49 if(!matrix_collision_test(position.x, position.y - velocity*mapped_y, 0)) {
el17sm 16:ddb203a74dfc 50 position.y -= velocity*mapped_y;
el17sm 10:1a3499f6b583 51 }
el17sm 10:1a3499f6b583 52 moving = false;
el17sm 22:7abf4581bc9b 53 if (abs(mapped_x) + abs(mapped_y) > 0.1f) {
el17sm 10:1a3499f6b583 54 moving = true;
el17sm 22:7abf4581bc9b 55 if (mapped_y < 0 && abs(mapped_y) > abs(mapped_x)) {
el17sm 10:1a3499f6b583 56 face = 2;
el17sm 22:7abf4581bc9b 57 } else if (mapped_y > 0 && abs(mapped_y) > abs(mapped_x)) {
el17sm 10:1a3499f6b583 58 face = 0;
el17sm 22:7abf4581bc9b 59 } else if (mapped_x > 0 && abs(mapped_x) > abs(mapped_y)) {
el17sm 10:1a3499f6b583 60 face = 1;
el17sm 22:7abf4581bc9b 61 } else if (mapped_x < 0 && abs(mapped_x) > abs(mapped_y)) {
el17sm 10:1a3499f6b583 62 face = 3;
el17sm 10:1a3499f6b583 63 }
el17sm 22:7abf4581bc9b 64
el17sm 22:7abf4581bc9b 65 if (frame.number < frame.max) {
el17sm 12:a1c1991835ca 66 frame.count++;
el17sm 22:7abf4581bc9b 67 } else {
el17sm 12:a1c1991835ca 68 frame.count = 0;
el17sm 12:a1c1991835ca 69 }
el17sm 22:7abf4581bc9b 70 } else {
el17sm 12:a1c1991835ca 71 frame.count = 0;
el17sm 12:a1c1991835ca 72 }
el17sm 12:a1c1991835ca 73 frame.number = (frame.count/4) % frame.max;
el17sm 12:a1c1991835ca 74 }
el17sm 12:a1c1991835ca 75
el17sm 22:7abf4581bc9b 76 int * Player::get_frame()
el17sm 22:7abf4581bc9b 77 {
el17sm 12:a1c1991835ca 78 return (int *) sprite_player[face][frame.number];
el17sm 10:1a3499f6b583 79 }
el17sm 10:1a3499f6b583 80
el17sm 22:7abf4581bc9b 81 void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X)
el17sm 22:7abf4581bc9b 82 {
el17sm 15:44d5cc33d389 83 fire_rate_counter++;
el17sm 22:7abf4581bc9b 84 if (button_Y) {
el17sm 10:1a3499f6b583 85 face = 0;
el17sm 22:7abf4581bc9b 86 } else if (button_B) {
el17sm 10:1a3499f6b583 87 face = 1;
el17sm 22:7abf4581bc9b 88 } else if (button_A) {
el17sm 10:1a3499f6b583 89 face = 2;
el17sm 22:7abf4581bc9b 90 } else if (button_X) {
el17sm 10:1a3499f6b583 91 face = 3;
el17sm 10:1a3499f6b583 92 }
el17sm 22:7abf4581bc9b 93 if (button_Y || button_B || button_A || button_X) {
el17sm 22:7abf4581bc9b 94 for (int i = 0; i < bullets_max; i++) {
el17sm 22:7abf4581bc9b 95 if (!valid_bullets[i] && (fire_rate_counter >= fire_rate_delay)) {
el17sm 15:44d5cc33d389 96 bullets_array[i] = new Bullets(position.x+2, position.y-2, face);
el17sm 14:3361879490b2 97 valid_bullets[i] = true;
el17sm 15:44d5cc33d389 98 fire_rate_counter = 0;
el17sm 14:3361879490b2 99 break;
el17sm 14:3361879490b2 100 }
el17sm 13:d04a6caba40d 101 }
el17sm 13:d04a6caba40d 102 }
el17sm 10:1a3499f6b583 103 }