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@40:cbcbf6fc1421, 2019-05-07 (annotated)
- Committer:
- el17sm
- Date:
- Tue May 07 12:42:12 2019 +0000
- Revision:
- 40:cbcbf6fc1421
- Parent:
- 37:a404860171a9
- Child:
- 44:cc4cecfc639f
Restart Works
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 | 40:cbcbf6fc1421 | 5 | hp = 25; |
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 | moving = true; |
el17sm | 36:92d131695e7c | 12 | hitbox.width = 19; |
el17sm | 36:92d131695e7c | 13 | hitbox.height = 9; |
el17sm | 33:4f3948dcd2f7 | 14 | |
el17sm | 36:92d131695e7c | 15 | sprite_size.width = 21; |
el17sm | 36:92d131695e7c | 16 | sprite_size.height = 23; |
el17sm | 36:92d131695e7c | 17 | sprite_size.offset_x = -1; |
el17sm | 36:92d131695e7c | 18 | sprite_size.offset_y = -14; |
el17sm | 33:4f3948dcd2f7 | 19 | |
el17sm | 36:92d131695e7c | 20 | _shadow.width = 19; |
el17sm | 36:92d131695e7c | 21 | _shadow.height = 5; |
el17sm | 36:92d131695e7c | 22 | _shadow.offset_x = 0; |
el17sm | 36:92d131695e7c | 23 | _shadow.offset_y = 5; |
el17sm | 36:92d131695e7c | 24 | |
el17sm | 36:92d131695e7c | 25 | position.x = pos_x; |
el17sm | 36:92d131695e7c | 26 | position.y = pos_y; |
el17sm | 33:4f3948dcd2f7 | 27 | update_prev_pos(); |
el17sm | 33:4f3948dcd2f7 | 28 | |
el17sm | 33:4f3948dcd2f7 | 29 | frame.count = 0; |
el17sm | 33:4f3948dcd2f7 | 30 | frame.number = 0; |
el17sm | 36:92d131695e7c | 31 | frame.max = 2; |
el17sm | 37:a404860171a9 | 32 | face = 2; |
el17sm | 33:4f3948dcd2f7 | 33 | |
el17sm | 36:92d131695e7c | 34 | velocity = 0.2; |
el17sm | 37:a404860171a9 | 35 | _hp_drop_chance = 100; |
el17sm | 33:4f3948dcd2f7 | 36 | } |
el17sm | 33:4f3948dcd2f7 | 37 | |
el17sm | 34:1d5b4da3935e | 38 | void Skull::move(float player_x, float player_y, char * map, bool * doorways) |
el17sm | 33:4f3948dcd2f7 | 39 | { |
el17sm | 36:92d131695e7c | 40 | // Approaching Movement |
el17sm | 36:92d131695e7c | 41 | if (_dash_counter < DASH_DELAY) { |
el17sm | 36:92d131695e7c | 42 | _dash = false; |
el17sm | 36:92d131695e7c | 43 | std::complex<double> pos_diff(player_x - position.x, player_y - position.y); // defining difference in position as a vector |
el17sm | 36:92d131695e7c | 44 | position.x += velocity * pos_diff.real() / std::abs(pos_diff); |
el17sm | 36:92d131695e7c | 45 | position.y += velocity * pos_diff.imag() / std::abs(pos_diff); |
el17sm | 36:92d131695e7c | 46 | |
el17sm | 36:92d131695e7c | 47 | // Setting Face |
el17sm | 36:92d131695e7c | 48 | if (abs(pos_diff.real()) > abs(pos_diff.imag())) { |
el17sm | 36:92d131695e7c | 49 | if (pos_diff.real() > 0) { |
el17sm | 36:92d131695e7c | 50 | face = 1; |
el17sm | 36:92d131695e7c | 51 | } else { |
el17sm | 36:92d131695e7c | 52 | face = 3; |
el17sm | 36:92d131695e7c | 53 | } |
el17sm | 36:92d131695e7c | 54 | } else { |
el17sm | 36:92d131695e7c | 55 | if (pos_diff.imag() > 0) { |
el17sm | 36:92d131695e7c | 56 | face = 2; |
el17sm | 36:92d131695e7c | 57 | } else { |
el17sm | 36:92d131695e7c | 58 | face = 0; |
el17sm | 36:92d131695e7c | 59 | } |
el17sm | 36:92d131695e7c | 60 | } |
el17sm | 36:92d131695e7c | 61 | } else if (_dash_counter < DASH_DELAY + 28){ |
el17sm | 36:92d131695e7c | 62 | _dash = true; |
el17sm | 36:92d131695e7c | 63 | velocity = skull_velocity_pattern[(int)((_dash_counter - DASH_DELAY)/4)]; |
el17sm | 36:92d131695e7c | 64 | if (face == 0){ |
el17sm | 36:92d131695e7c | 65 | position.y -= velocity; |
el17sm | 36:92d131695e7c | 66 | } else if (face == 1){ |
el17sm | 36:92d131695e7c | 67 | position.x += velocity; |
el17sm | 36:92d131695e7c | 68 | } else if (face == 2){ |
el17sm | 36:92d131695e7c | 69 | position.y += velocity; |
el17sm | 36:92d131695e7c | 70 | } else if (face == 3){ |
el17sm | 36:92d131695e7c | 71 | position.x -= velocity; |
el17sm | 36:92d131695e7c | 72 | } |
el17sm | 36:92d131695e7c | 73 | } else { |
el17sm | 36:92d131695e7c | 74 | _dash_counter = 0; |
el17sm | 36:92d131695e7c | 75 | velocity = 0.2; |
el17sm | 36:92d131695e7c | 76 | } |
el17sm | 36:92d131695e7c | 77 | undo_move_x(entity_to_map_collision_test(position.x, prev_pos.y, map, doorways)); |
el17sm | 36:92d131695e7c | 78 | undo_move_y(entity_to_map_collision_test(prev_pos.x, position.y, map, doorways)); |
el17sm | 33:4f3948dcd2f7 | 79 | |
el17sm | 36:92d131695e7c | 80 | _dash_counter++; |
el17sm | 36:92d131695e7c | 81 | increment_frames(); |
el17sm | 33:4f3948dcd2f7 | 82 | } |
el17sm | 33:4f3948dcd2f7 | 83 | |
el17sm | 33:4f3948dcd2f7 | 84 | void Skull::draw(N5110 &lcd) |
el17sm | 33:4f3948dcd2f7 | 85 | { |
el17sm | 36:92d131695e7c | 86 | update_offsets(); |
el17sm | 36:92d131695e7c | 87 | lcd.drawSpriteTransparent(position.x+_shadow.offset_x, |
el17sm | 36:92d131695e7c | 88 | position.y+_shadow.offset_y, |
el17sm | 36:92d131695e7c | 89 | _shadow.height, |
el17sm | 36:92d131695e7c | 90 | _shadow.width, |
el17sm | 36:92d131695e7c | 91 | (char *)skull_shadow_sprite[frame.number]); |
el17sm | 36:92d131695e7c | 92 | lcd.drawSpriteTransparent(position.x+sprite_size.offset_x, |
el17sm | 36:92d131695e7c | 93 | position.y+sprite_size.offset_y, |
el17sm | 36:92d131695e7c | 94 | sprite_size.height, |
el17sm | 36:92d131695e7c | 95 | sprite_size.width, |
el17sm | 36:92d131695e7c | 96 | (char *)skull_sprite[face][_dash]); |
el17sm | 33:4f3948dcd2f7 | 97 | } |
el17sm | 33:4f3948dcd2f7 | 98 | |
el17sm | 33:4f3948dcd2f7 | 99 | void Skull::take_damage(int damage) |
el17sm | 33:4f3948dcd2f7 | 100 | { |
el17sm | 36:92d131695e7c | 101 | hp -= 1; |
el17sm | 36:92d131695e7c | 102 | } |
el17sm | 36:92d131695e7c | 103 | |
el17sm | 36:92d131695e7c | 104 | // Private Functions |
el17sm | 36:92d131695e7c | 105 | |
el17sm | 36:92d131695e7c | 106 | void Skull::increment_frames() |
el17sm | 36:92d131695e7c | 107 | { |
el17sm | 36:92d131695e7c | 108 | if (frame.number < frame.max) { |
el17sm | 36:92d131695e7c | 109 | frame.count++; |
el17sm | 36:92d131695e7c | 110 | } else { |
el17sm | 36:92d131695e7c | 111 | frame.count = 0; |
el17sm | 36:92d131695e7c | 112 | } |
el17sm | 36:92d131695e7c | 113 | frame.number = (frame.count/20) % frame.max; |
el17sm | 36:92d131695e7c | 114 | } |
el17sm | 36:92d131695e7c | 115 | |
el17sm | 36:92d131695e7c | 116 | void Skull::update_offsets() |
el17sm | 36:92d131695e7c | 117 | { |
el17sm | 36:92d131695e7c | 118 | if (frame.number == 0) { |
el17sm | 36:92d131695e7c | 119 | sprite_size.offset_y = -14; |
el17sm | 36:92d131695e7c | 120 | } else if (frame.number == 1) { |
el17sm | 36:92d131695e7c | 121 | sprite_size.offset_y = -15; |
el17sm | 36:92d131695e7c | 122 | } |
el17sm | 33:4f3948dcd2f7 | 123 | } |