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