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