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/Mobs/Headless/Headless.cpp@59:fd4669864b67, 2019-05-09 (annotated)
- Committer:
- el17sm
- Date:
- Thu May 09 14:49:27 2019 +0000
- Revision:
- 59:fd4669864b67
- Parent:
- 57:1c12361b6e3d
wtf y no work
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17sm | 32:fe6359ef9916 | 1 | #include "Headless.h" |
| el17sm | 32:fe6359ef9916 | 2 | #include "math.h" |
| el17sm | 32:fe6359ef9916 | 3 | #include <complex> |
| el17sm | 32:fe6359ef9916 | 4 | |
| el17sm | 32:fe6359ef9916 | 5 | Headless::Headless(float pos_x, float pos_y) |
| el17sm | 32:fe6359ef9916 | 6 | { |
| el17sm | 57:1c12361b6e3d | 7 | _hp = 4; |
| el17sm | 57:1c12361b6e3d | 8 | _attack = 1; |
| el17sm | 57:1c12361b6e3d | 9 | _face = 0; |
| el17sm | 56:ef9521b7ed78 | 10 | _hp_drop_chance = 10; // out of 100 |
| el17sm | 56:ef9521b7ed78 | 11 | |
| el17sm | 57:1c12361b6e3d | 12 | _hitbox.width = 6; |
| el17sm | 57:1c12361b6e3d | 13 | _hitbox.height = 5; |
| el17sm | 56:ef9521b7ed78 | 14 | |
| el17sm | 57:1c12361b6e3d | 15 | _position.x = pos_x; |
| el17sm | 57:1c12361b6e3d | 16 | _position.y = pos_y; |
| el17sm | 56:ef9521b7ed78 | 17 | |
| el17sm | 57:1c12361b6e3d | 18 | _sprite_size.width = 6; |
| el17sm | 57:1c12361b6e3d | 19 | _sprite_size.height = 9; |
| el17sm | 57:1c12361b6e3d | 20 | _sprite_size.offset_x = 0; |
| el17sm | 57:1c12361b6e3d | 21 | _sprite_size.offset_y = -4; |
| el17sm | 56:ef9521b7ed78 | 22 | |
| el17sm | 57:1c12361b6e3d | 23 | _frame.count = 0; |
| el17sm | 57:1c12361b6e3d | 24 | _frame.number = 0; |
| el17sm | 57:1c12361b6e3d | 25 | _frame.max = 4; |
| el17sm | 56:ef9521b7ed78 | 26 | |
| el17sm | 57:1c12361b6e3d | 27 | _velocity = 0.25; |
| el17sm | 32:fe6359ef9916 | 28 | } |
| el17sm | 32:fe6359ef9916 | 29 | |
| el17sm | 34:1d5b4da3935e | 30 | void Headless::move(float player_x, float player_y, char * map, bool * doorways) |
| el17sm | 32:fe6359ef9916 | 31 | { |
| el17sm | 57:1c12361b6e3d | 32 | std::complex<double> pos_diff(player_x - _position.x, player_y - _position.y); // Defining difference in position as a vector for simplicity (magnitude is hard to calculate) |
| el17sm | 57:1c12361b6e3d | 33 | _position.x += _velocity * pos_diff.real() / std::abs(pos_diff); |
| el17sm | 57:1c12361b6e3d | 34 | _position.y += _velocity * pos_diff.imag() / std::abs(pos_diff); |
| el17sm | 56:ef9521b7ed78 | 35 | // Setting face |
| el17sm | 36:92d131695e7c | 36 | if (abs(pos_diff.real()) > abs(pos_diff.imag())) { |
| el17sm | 57:1c12361b6e3d | 37 | if (pos_diff.real() > 0) { _face = 1; |
| el17sm | 57:1c12361b6e3d | 38 | } else { _face = 3; |
| el17sm | 36:92d131695e7c | 39 | } |
| el17sm | 36:92d131695e7c | 40 | } else { |
| el17sm | 57:1c12361b6e3d | 41 | if (pos_diff.imag() > 0) { _face = 2; |
| el17sm | 57:1c12361b6e3d | 42 | } else { _face = 0; |
| el17sm | 36:92d131695e7c | 43 | } |
| el17sm | 32:fe6359ef9916 | 44 | } |
| el17sm | 56:ef9521b7ed78 | 45 | // Wall Collision |
| el17sm | 57:1c12361b6e3d | 46 | undo_move_x(entity_to_map_collision_test(_position.x, _prev_pos.y, map, doorways)); |
| el17sm | 57:1c12361b6e3d | 47 | undo_move_y(entity_to_map_collision_test(_prev_pos.x, _position.y, map, doorways)); |
| el17sm | 56:ef9521b7ed78 | 48 | // Animation |
| el17sm | 56:ef9521b7ed78 | 49 | increment_frame(); |
| el17sm | 56:ef9521b7ed78 | 50 | } |
| el17sm | 32:fe6359ef9916 | 51 | |
| el17sm | 56:ef9521b7ed78 | 52 | void Headless::increment_frame() |
| el17sm | 56:ef9521b7ed78 | 53 | { |
| el17sm | 57:1c12361b6e3d | 54 | if (_frame.number < _frame.max) { |
| el17sm | 57:1c12361b6e3d | 55 | _frame.count++; |
| el17sm | 32:fe6359ef9916 | 56 | } else { |
| el17sm | 57:1c12361b6e3d | 57 | _frame.count = 0; |
| el17sm | 32:fe6359ef9916 | 58 | } |
| el17sm | 57:1c12361b6e3d | 59 | _frame.number = (_frame.count/16) % _frame.max; // Frame number is used in chosing sprite-frame for animation; the constant 16 is the number of frames per sprite-frame |
| el17sm | 32:fe6359ef9916 | 60 | } |
| el17sm | 32:fe6359ef9916 | 61 | |
| el17sm | 32:fe6359ef9916 | 62 | void Headless::draw(N5110 &lcd) |
| el17sm | 32:fe6359ef9916 | 63 | { |
| el17sm | 57:1c12361b6e3d | 64 | lcd.drawSpriteTransparent(_position.x+_sprite_size.offset_x, |
| el17sm | 57:1c12361b6e3d | 65 | _position.y+_sprite_size.offset_y, |
| el17sm | 57:1c12361b6e3d | 66 | _sprite_size.height, |
| el17sm | 57:1c12361b6e3d | 67 | _sprite_size.width, |
| el17sm | 32:fe6359ef9916 | 68 | get_frame()); |
| el17sm | 32:fe6359ef9916 | 69 | } |
| el17sm | 32:fe6359ef9916 | 70 | |
| el17sm | 32:fe6359ef9916 | 71 | void Headless::take_damage(int damage) |
| el17sm | 32:fe6359ef9916 | 72 | { |
| el17sm | 57:1c12361b6e3d | 73 | _hp -= damage; |
| el17sm | 32:fe6359ef9916 | 74 | } |
| el17sm | 32:fe6359ef9916 | 75 | |
| el17sm | 56:ef9521b7ed78 | 76 | char * Headless::get_frame() // Returns the corresponding frame |
| el17sm | 32:fe6359ef9916 | 77 | { |
| el17sm | 57:1c12361b6e3d | 78 | return (char *) sprite_headless[_face][_frame.number]; |
| el17sm | 32:fe6359ef9916 | 79 | } |