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@56:ef9521b7ed78, 2019-05-09 (annotated)
- Committer:
- el17sm
- Date:
- Thu May 09 08:42:52 2019 +0000
- Revision:
- 56:ef9521b7ed78
- Parent:
- 51:4d0cd75e7ed3
- Child:
- 57:1c12361b6e3d
Full code in-line commented
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 | 46:f09711580d4a | 5 | hp = 20; |
el17sm | 33:4f3948dcd2f7 | 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 | 36:92d131695e7c | 11 | hitbox.width = 19; |
el17sm | 36:92d131695e7c | 12 | hitbox.height = 9; |
el17sm | 33:4f3948dcd2f7 | 13 | |
el17sm | 36:92d131695e7c | 14 | sprite_size.width = 21; |
el17sm | 36:92d131695e7c | 15 | sprite_size.height = 23; |
el17sm | 36:92d131695e7c | 16 | sprite_size.offset_x = -1; |
el17sm | 36:92d131695e7c | 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 | 36:92d131695e7c | 24 | position.x = pos_x; |
el17sm | 36:92d131695e7c | 25 | position.y = pos_y; |
el17sm | 33:4f3948dcd2f7 | 26 | update_prev_pos(); |
el17sm | 33:4f3948dcd2f7 | 27 | |
el17sm | 33:4f3948dcd2f7 | 28 | frame.count = 0; |
el17sm | 33:4f3948dcd2f7 | 29 | frame.number = 0; |
el17sm | 36:92d131695e7c | 30 | frame.max = 2; |
el17sm | 37:a404860171a9 | 31 | face = 2; |
el17sm | 33:4f3948dcd2f7 | 32 | |
el17sm | 36:92d131695e7c | 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 | 36:92d131695e7c | 45 | velocity = 0.2; |
el17sm | 36:92d131695e7c | 46 | } |
el17sm | 36:92d131695e7c | 47 | undo_move_x(entity_to_map_collision_test(position.x, prev_pos.y, map, doorways)); |
el17sm | 36:92d131695e7c | 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 | 56:ef9521b7ed78 | 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 | 56:ef9521b7ed78 | 58 | position.x += velocity * pos_diff.real() / std::abs(pos_diff); |
el17sm | 56:ef9521b7ed78 | 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 | 56:ef9521b7ed78 | 63 | face = 1; |
el17sm | 56:ef9521b7ed78 | 64 | } else { |
el17sm | 56:ef9521b7ed78 | 65 | face = 3; |
el17sm | 56:ef9521b7ed78 | 66 | } |
el17sm | 56:ef9521b7ed78 | 67 | } else { |
el17sm | 56:ef9521b7ed78 | 68 | if (pos_diff.imag() > 0) { |
el17sm | 56:ef9521b7ed78 | 69 | face = 2; |
el17sm | 56:ef9521b7ed78 | 70 | } else { |
el17sm | 56:ef9521b7ed78 | 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 | 56:ef9521b7ed78 | 79 | velocity = skull_velocity_pattern[(int)((_dash_counter - DASH_DELAY)/4)]; |
el17sm | 56:ef9521b7ed78 | 80 | if (face == 0){ |
el17sm | 56:ef9521b7ed78 | 81 | position.y -= velocity; |
el17sm | 56:ef9521b7ed78 | 82 | } else if (face == 1){ |
el17sm | 56:ef9521b7ed78 | 83 | position.x += velocity; |
el17sm | 56:ef9521b7ed78 | 84 | } else if (face == 2){ |
el17sm | 56:ef9521b7ed78 | 85 | position.y += velocity; |
el17sm | 56:ef9521b7ed78 | 86 | } else if (face == 3){ |
el17sm | 56:ef9521b7ed78 | 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 | 36:92d131695e7c | 94 | lcd.drawSpriteTransparent(position.x+_shadow.offset_x, |
el17sm | 36:92d131695e7c | 95 | position.y+_shadow.offset_y, |
el17sm | 36:92d131695e7c | 96 | _shadow.height, |
el17sm | 36:92d131695e7c | 97 | _shadow.width, |
el17sm | 36:92d131695e7c | 98 | (char *)skull_shadow_sprite[frame.number]); |
el17sm | 36:92d131695e7c | 99 | lcd.drawSpriteTransparent(position.x+sprite_size.offset_x, |
el17sm | 36:92d131695e7c | 100 | position.y+sprite_size.offset_y, |
el17sm | 36:92d131695e7c | 101 | sprite_size.height, |
el17sm | 36:92d131695e7c | 102 | sprite_size.width, |
el17sm | 36:92d131695e7c | 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 | 36:92d131695e7c | 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 | 36:92d131695e7c | 115 | if (frame.number < frame.max) { |
el17sm | 36:92d131695e7c | 116 | frame.count++; |
el17sm | 36:92d131695e7c | 117 | } else { |
el17sm | 36:92d131695e7c | 118 | frame.count = 0; |
el17sm | 36:92d131695e7c | 119 | } |
el17sm | 36:92d131695e7c | 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 | 36:92d131695e7c | 125 | if (frame.number == 0) { |
el17sm | 36:92d131695e7c | 126 | sprite_size.offset_y = -14; |
el17sm | 36:92d131695e7c | 127 | } else if (frame.number == 1) { |
el17sm | 36:92d131695e7c | 128 | sprite_size.offset_y = -15; |
el17sm | 36:92d131695e7c | 129 | } |
el17sm | 33:4f3948dcd2f7 | 130 | } |