A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.
Dependencies: mbed MotionSensor
Entity/Bosses/Skull/Skull.cpp@61:901871a7c6ff, 2019-05-09 (annotated)
- Committer:
- el17sm
- Date:
- Thu May 09 14:56:46 2019 +0000
- Revision:
- 61:901871a7c6ff
- Parent:
- 57:1c12361b6e3d
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17sm | 33:4f3948dcd2f7 | 1 | #include "Skull.h" |
el17sm | 36:92d131695e7c | 2 | #include <complex> |
el17sm | 33:4f3948dcd2f7 | 3 | Skull::Skull(float pos_x, float pos_y) |
el17sm | 33:4f3948dcd2f7 | 4 | { |
el17sm | 57:1c12361b6e3d | 5 | _hp = 20; |
el17sm | 57:1c12361b6e3d | 6 | _attack = 1; |
el17sm | 33:4f3948dcd2f7 | 7 | |
el17sm | 36:92d131695e7c | 8 | _dash = false; |
el17sm | 36:92d131695e7c | 9 | _dash_counter = 0; |
el17sm | 36:92d131695e7c | 10 | |
el17sm | 57:1c12361b6e3d | 11 | _hitbox.width = 19; |
el17sm | 57:1c12361b6e3d | 12 | _hitbox.height = 9; |
el17sm | 33:4f3948dcd2f7 | 13 | |
el17sm | 57:1c12361b6e3d | 14 | _sprite_size.width = 21; |
el17sm | 57:1c12361b6e3d | 15 | _sprite_size.height = 23; |
el17sm | 57:1c12361b6e3d | 16 | _sprite_size.offset_x = -1; |
el17sm | 57:1c12361b6e3d | 17 | _sprite_size.offset_y = -14; |
el17sm | 33:4f3948dcd2f7 | 18 | |
el17sm | 36:92d131695e7c | 19 | _shadow.width = 19; |
el17sm | 36:92d131695e7c | 20 | _shadow.height = 5; |
el17sm | 36:92d131695e7c | 21 | _shadow.offset_x = 0; |
el17sm | 36:92d131695e7c | 22 | _shadow.offset_y = 5; |
el17sm | 36:92d131695e7c | 23 | |
el17sm | 57:1c12361b6e3d | 24 | _position.x = pos_x; |
el17sm | 57:1c12361b6e3d | 25 | _position.y = pos_y; |
el17sm | 33:4f3948dcd2f7 | 26 | update_prev_pos(); |
el17sm | 33:4f3948dcd2f7 | 27 | |
el17sm | 57:1c12361b6e3d | 28 | _frame.count = 0; |
el17sm | 57:1c12361b6e3d | 29 | _frame.number = 0; |
el17sm | 57:1c12361b6e3d | 30 | _frame.max = 2; |
el17sm | 57:1c12361b6e3d | 31 | _face = 2; |
el17sm | 33:4f3948dcd2f7 | 32 | |
el17sm | 57:1c12361b6e3d | 33 | _velocity = 0.2; |
el17sm | 46:f09711580d4a | 34 | _hp_drop_chance = 0; |
el17sm | 33:4f3948dcd2f7 | 35 | } |
el17sm | 33:4f3948dcd2f7 | 36 | |
el17sm | 34:1d5b4da3935e | 37 | void Skull::move(float player_x, float player_y, char * map, bool * doorways) |
el17sm | 33:4f3948dcd2f7 | 38 | { |
el17sm | 56:ef9521b7ed78 | 39 | if (_dash_counter < DASH_DELAY) { // Approaching Movement |
el17sm | 56:ef9521b7ed78 | 40 | approaching_movement(player_x, player_y); |
el17sm | 56:ef9521b7ed78 | 41 | } else if (_dash_counter < DASH_DELAY + 28){ // Dashing movement; const 28 = 4(velocity_index_increment_delay) * 7 (length of the velocity_pattern) |
el17sm | 56:ef9521b7ed78 | 42 | dash_movement(); |
el17sm | 36:92d131695e7c | 43 | } else { |
el17sm | 36:92d131695e7c | 44 | _dash_counter = 0; |
el17sm | 57:1c12361b6e3d | 45 | _velocity = 0.2; |
el17sm | 36:92d131695e7c | 46 | } |
el17sm | 57:1c12361b6e3d | 47 | undo_move_x(entity_to_map_collision_test(_position.x, _prev_pos.y, map, doorways)); |
el17sm | 57:1c12361b6e3d | 48 | undo_move_y(entity_to_map_collision_test(_prev_pos.x, _position.y, map, doorways)); |
el17sm | 33:4f3948dcd2f7 | 49 | |
el17sm | 36:92d131695e7c | 50 | _dash_counter++; |
el17sm | 36:92d131695e7c | 51 | increment_frames(); |
el17sm | 33:4f3948dcd2f7 | 52 | } |
el17sm | 33:4f3948dcd2f7 | 53 | |
el17sm | 56:ef9521b7ed78 | 54 | void Skull::approaching_movement(float player_x, float player_y) |
el17sm | 56:ef9521b7ed78 | 55 | { |
el17sm | 56:ef9521b7ed78 | 56 | _dash = false; |
el17sm | 57:1c12361b6e3d | 57 | std::complex<double> pos_diff(player_x - _position.x, player_y - _position.y); // defining difference in position as a vector for simplicity, similar to Headless |
el17sm | 57:1c12361b6e3d | 58 | _position.x += _velocity * pos_diff.real() / std::abs(pos_diff); |
el17sm | 57:1c12361b6e3d | 59 | _position.y += _velocity * pos_diff.imag() / std::abs(pos_diff); |
el17sm | 56:ef9521b7ed78 | 60 | // Setting Face |
el17sm | 56:ef9521b7ed78 | 61 | if (abs(pos_diff.real()) > abs(pos_diff.imag())) { |
el17sm | 56:ef9521b7ed78 | 62 | if (pos_diff.real() > 0) { |
el17sm | 57:1c12361b6e3d | 63 | _face = 1; |
el17sm | 56:ef9521b7ed78 | 64 | } else { |
el17sm | 57:1c12361b6e3d | 65 | _face = 3; |
el17sm | 56:ef9521b7ed78 | 66 | } |
el17sm | 56:ef9521b7ed78 | 67 | } else { |
el17sm | 56:ef9521b7ed78 | 68 | if (pos_diff.imag() > 0) { |
el17sm | 57:1c12361b6e3d | 69 | _face = 2; |
el17sm | 56:ef9521b7ed78 | 70 | } else { |
el17sm | 57:1c12361b6e3d | 71 | _face = 0; |
el17sm | 56:ef9521b7ed78 | 72 | } |
el17sm | 56:ef9521b7ed78 | 73 | } |
el17sm | 56:ef9521b7ed78 | 74 | } |
el17sm | 56:ef9521b7ed78 | 75 | |
el17sm | 56:ef9521b7ed78 | 76 | void Skull::dash_movement() // Changes velocity over time using the velocity pattern |
el17sm | 56:ef9521b7ed78 | 77 | { |
el17sm | 56:ef9521b7ed78 | 78 | _dash = true; |
el17sm | 57:1c12361b6e3d | 79 | _velocity = skull_velocity_pattern[(int)((_dash_counter - DASH_DELAY)/4)]; |
el17sm | 57:1c12361b6e3d | 80 | if (_face == 0){ |
el17sm | 57:1c12361b6e3d | 81 | _position.y -= _velocity; |
el17sm | 57:1c12361b6e3d | 82 | } else if (_face == 1){ |
el17sm | 57:1c12361b6e3d | 83 | _position.x += _velocity; |
el17sm | 57:1c12361b6e3d | 84 | } else if (_face == 2){ |
el17sm | 57:1c12361b6e3d | 85 | _position.y += _velocity; |
el17sm | 57:1c12361b6e3d | 86 | } else if (_face == 3){ |
el17sm | 57:1c12361b6e3d | 87 | _position.x -= _velocity; |
el17sm | 56:ef9521b7ed78 | 88 | } |
el17sm | 56:ef9521b7ed78 | 89 | } |
el17sm | 56:ef9521b7ed78 | 90 | |
el17sm | 33:4f3948dcd2f7 | 91 | void Skull::draw(N5110 &lcd) |
el17sm | 33:4f3948dcd2f7 | 92 | { |
el17sm | 36:92d131695e7c | 93 | update_offsets(); |
el17sm | 57:1c12361b6e3d | 94 | lcd.drawSpriteTransparent(_position.x+_shadow.offset_x, |
el17sm | 57:1c12361b6e3d | 95 | _position.y+_shadow.offset_y, |
el17sm | 36:92d131695e7c | 96 | _shadow.height, |
el17sm | 36:92d131695e7c | 97 | _shadow.width, |
el17sm | 57:1c12361b6e3d | 98 | (char *)skull_shadow_sprite[_frame.number]); |
el17sm | 57:1c12361b6e3d | 99 | lcd.drawSpriteTransparent(_position.x+_sprite_size.offset_x, |
el17sm | 57:1c12361b6e3d | 100 | _position.y+_sprite_size.offset_y, |
el17sm | 57:1c12361b6e3d | 101 | _sprite_size.height, |
el17sm | 57:1c12361b6e3d | 102 | _sprite_size.width, |
el17sm | 57:1c12361b6e3d | 103 | (char *)skull_sprite[_face][_dash]); |
el17sm | 33:4f3948dcd2f7 | 104 | } |
el17sm | 33:4f3948dcd2f7 | 105 | |
el17sm | 33:4f3948dcd2f7 | 106 | void Skull::take_damage(int damage) |
el17sm | 33:4f3948dcd2f7 | 107 | { |
el17sm | 57:1c12361b6e3d | 108 | _hp -= 1; |
el17sm | 36:92d131695e7c | 109 | } |
el17sm | 36:92d131695e7c | 110 | |
el17sm | 56:ef9521b7ed78 | 111 | // Methods |
el17sm | 36:92d131695e7c | 112 | |
el17sm | 36:92d131695e7c | 113 | void Skull::increment_frames() |
el17sm | 36:92d131695e7c | 114 | { |
el17sm | 57:1c12361b6e3d | 115 | if (_frame.number < _frame.max) { |
el17sm | 57:1c12361b6e3d | 116 | _frame.count++; |
el17sm | 36:92d131695e7c | 117 | } else { |
el17sm | 57:1c12361b6e3d | 118 | _frame.count = 0; |
el17sm | 36:92d131695e7c | 119 | } |
el17sm | 57:1c12361b6e3d | 120 | _frame.number = (_frame.count/20) % _frame.max; |
el17sm | 36:92d131695e7c | 121 | } |
el17sm | 36:92d131695e7c | 122 | |
el17sm | 56:ef9521b7ed78 | 123 | void Skull::update_offsets() // Animates the shadows by offsetting the skull from the shadow periodically |
el17sm | 36:92d131695e7c | 124 | { |
el17sm | 57:1c12361b6e3d | 125 | if (_frame.number == 0) { |
el17sm | 57:1c12361b6e3d | 126 | _sprite_size.offset_y = -14; |
el17sm | 57:1c12361b6e3d | 127 | } else if (_frame.number == 1) { |
el17sm | 57:1c12361b6e3d | 128 | _sprite_size.offset_y = -15; |
el17sm | 36:92d131695e7c | 129 | } |
el17sm | 33:4f3948dcd2f7 | 130 | } |