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@15:44d5cc33d389, 2019-04-24 (annotated)
- Committer:
- el17sm
- Date:
- Wed Apr 24 06:28:14 2019 +0000
- Revision:
- 15:44d5cc33d389
- Parent:
- 14:3361879490b2
- Child:
- 16:ddb203a74dfc
Fully functioning without map generation and map movement. Only Headless enemy
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 | 10:1a3499f6b583 | 5 | Player::Player(float pos_x, float pos_y){ |
| el17sm | 10:1a3499f6b583 | 6 | moving = false; |
| el17sm | 10:1a3499f6b583 | 7 | face = 0; |
| el17sm | 13:d04a6caba40d | 8 | hp = 1; |
| el17sm | 10:1a3499f6b583 | 9 | hitbox.width = 6; |
| el17sm | 10:1a3499f6b583 | 10 | hitbox.height = 5; |
| el17sm | 10:1a3499f6b583 | 11 | position.x = pos_x; |
| el17sm | 10:1a3499f6b583 | 12 | position.y = pos_y; |
| el17sm | 10:1a3499f6b583 | 13 | sprite_size.width = 6; |
| el17sm | 10:1a3499f6b583 | 14 | sprite_size.height = 12; |
| el17sm | 10:1a3499f6b583 | 15 | sprite_size.offset_x = 0; |
| el17sm | 10:1a3499f6b583 | 16 | sprite_size.offset_y = 7; |
| el17sm | 12:a1c1991835ca | 17 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 18 | frame.number = 0; |
| el17sm | 12:a1c1991835ca | 19 | frame.max = 4; |
| el17sm | 13:d04a6caba40d | 20 | for (int i = 0; i < bullets_max; i++){valid_bullets[i] = false;} |
| el17sm | 15:44d5cc33d389 | 21 | fire_rate_counter = 0; |
| el17sm | 15:44d5cc33d389 | 22 | fire_rate_delay = 5; |
| el17sm | 10:1a3499f6b583 | 23 | } |
| el17sm | 10:1a3499f6b583 | 24 | |
| el17sm | 13:d04a6caba40d | 25 | // Accessors |
| el17sm | 14:3361879490b2 | 26 | int Player::get_attack(){return 1;}; |
| el17sm | 13:d04a6caba40d | 27 | |
| el17sm | 13:d04a6caba40d | 28 | // Functions |
| el17sm | 10:1a3499f6b583 | 29 | void Player::move(float mapped_x, float mapped_y){ |
| el17sm | 11:63e54f6e7939 | 30 | if(!matrix_collision_test(position.x + player_speed*mapped_x, position.y, 0)){ |
| el17sm | 11:63e54f6e7939 | 31 | position.x += player_speed*mapped_x; |
| el17sm | 10:1a3499f6b583 | 32 | } |
| el17sm | 11:63e54f6e7939 | 33 | if(!matrix_collision_test(position.x, position.y - player_speed*mapped_y, 0)){ |
| el17sm | 11:63e54f6e7939 | 34 | position.y -= player_speed*mapped_y; |
| el17sm | 10:1a3499f6b583 | 35 | } |
| el17sm | 10:1a3499f6b583 | 36 | moving = false; |
| el17sm | 10:1a3499f6b583 | 37 | if (abs(mapped_x) + abs(mapped_y) > 0.1f){ |
| el17sm | 10:1a3499f6b583 | 38 | moving = true; |
| el17sm | 10:1a3499f6b583 | 39 | if (mapped_y < 0 && abs(mapped_y) > abs(mapped_x)){ |
| el17sm | 10:1a3499f6b583 | 40 | face = 2; |
| el17sm | 10:1a3499f6b583 | 41 | } |
| el17sm | 10:1a3499f6b583 | 42 | else if (mapped_y > 0 && abs(mapped_y) > abs(mapped_x)){ |
| el17sm | 10:1a3499f6b583 | 43 | face = 0; |
| el17sm | 10:1a3499f6b583 | 44 | } |
| el17sm | 10:1a3499f6b583 | 45 | else if (mapped_x > 0 && abs(mapped_x) > abs(mapped_y)){ |
| el17sm | 10:1a3499f6b583 | 46 | face = 1; |
| el17sm | 10:1a3499f6b583 | 47 | } |
| el17sm | 10:1a3499f6b583 | 48 | else if (mapped_x < 0 && abs(mapped_x) > abs(mapped_y)){ |
| el17sm | 10:1a3499f6b583 | 49 | face = 3; |
| el17sm | 10:1a3499f6b583 | 50 | } |
| el17sm | 12:a1c1991835ca | 51 | |
| el17sm | 12:a1c1991835ca | 52 | if (frame.number < frame.max){ |
| el17sm | 12:a1c1991835ca | 53 | frame.count++; |
| el17sm | 12:a1c1991835ca | 54 | } |
| el17sm | 12:a1c1991835ca | 55 | else { |
| el17sm | 12:a1c1991835ca | 56 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 57 | } |
| el17sm | 10:1a3499f6b583 | 58 | } |
| el17sm | 12:a1c1991835ca | 59 | else{ |
| el17sm | 12:a1c1991835ca | 60 | frame.count = 0; |
| el17sm | 12:a1c1991835ca | 61 | } |
| el17sm | 12:a1c1991835ca | 62 | frame.number = (frame.count/4) % frame.max; |
| el17sm | 12:a1c1991835ca | 63 | } |
| el17sm | 12:a1c1991835ca | 64 | |
| el17sm | 12:a1c1991835ca | 65 | int * Player::get_frame(){ |
| el17sm | 12:a1c1991835ca | 66 | return (int *) sprite_player[face][frame.number]; |
| el17sm | 10:1a3499f6b583 | 67 | } |
| el17sm | 10:1a3499f6b583 | 68 | |
| el17sm | 10:1a3499f6b583 | 69 | void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X){ |
| el17sm | 15:44d5cc33d389 | 70 | fire_rate_counter++; |
| el17sm | 10:1a3499f6b583 | 71 | if (button_Y){ |
| el17sm | 10:1a3499f6b583 | 72 | face = 0; |
| el17sm | 10:1a3499f6b583 | 73 | } |
| el17sm | 10:1a3499f6b583 | 74 | else if (button_B){ |
| el17sm | 10:1a3499f6b583 | 75 | face = 1; |
| el17sm | 10:1a3499f6b583 | 76 | } |
| el17sm | 10:1a3499f6b583 | 77 | else if (button_A){ |
| el17sm | 10:1a3499f6b583 | 78 | face = 2; |
| el17sm | 10:1a3499f6b583 | 79 | } |
| el17sm | 10:1a3499f6b583 | 80 | else if (button_X){ |
| el17sm | 10:1a3499f6b583 | 81 | face = 3; |
| el17sm | 10:1a3499f6b583 | 82 | } |
| el17sm | 14:3361879490b2 | 83 | if (button_Y || button_B || button_A || button_X){ |
| el17sm | 14:3361879490b2 | 84 | for (int i = 0; i < bullets_max; i++){ |
| el17sm | 15:44d5cc33d389 | 85 | if (!valid_bullets[i] && fire_rate_counter > fire_rate_delay){ |
| el17sm | 15:44d5cc33d389 | 86 | bullets_array[i] = new Bullets(position.x+2, position.y-2, face); |
| el17sm | 14:3361879490b2 | 87 | valid_bullets[i] = true; |
| el17sm | 15:44d5cc33d389 | 88 | fire_rate_counter = 0; |
| el17sm | 14:3361879490b2 | 89 | break; |
| el17sm | 14:3361879490b2 | 90 | } |
| el17sm | 13:d04a6caba40d | 91 | } |
| el17sm | 13:d04a6caba40d | 92 | } |
| el17sm | 10:1a3499f6b583 | 93 | } |