A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.
Dependencies: mbed MotionSensor
RoomEngine/RoomEngine.cpp@29:6b8411bb040a, 2019-05-04 (annotated)
- Committer:
- el17sm
- Date:
- Sat May 04 15:39:20 2019 +0000
- Revision:
- 29:6b8411bb040a
- Parent:
- 28:98848e6a77a2
- Child:
- 30:ec915d24d3e9
added Room complete;; started Floor engine;; ; To do:; Floor generation;; More enemies;; Bosses;; Title Screen;; Score;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17sm | 26:abbc19edc5c1 | 1 | #include "RoomEngine.h" |
el17sm | 27:a1b41626f57c | 2 | RoomEngine::RoomEngine() |
el17sm | 26:abbc19edc5c1 | 3 | { |
el17sm | 29:6b8411bb040a | 4 | _room_x = 5; |
el17sm | 29:6b8411bb040a | 5 | _room_y = 5; |
el17sm | 26:abbc19edc5c1 | 6 | } |
el17sm | 26:abbc19edc5c1 | 7 | |
el17sm | 27:a1b41626f57c | 8 | RoomEngine::~RoomEngine() |
el17sm | 27:a1b41626f57c | 9 | { |
el17sm | 27:a1b41626f57c | 10 | delete player; |
el17sm | 27:a1b41626f57c | 11 | } |
el17sm | 27:a1b41626f57c | 12 | |
el17sm | 29:6b8411bb040a | 13 | void RoomEngine::load(Player ¤t_player, Room *current_room) |
el17sm | 28:98848e6a77a2 | 14 | { |
el17sm | 28:98848e6a77a2 | 15 | player = ¤t_player; |
el17sm | 29:6b8411bb040a | 16 | room = current_room; |
el17sm | 28:98848e6a77a2 | 17 | set_input(0,0,0,0,0,0,0,0); |
el17sm | 28:98848e6a77a2 | 18 | update_player_position(check_player_room_position()); |
el17sm | 28:98848e6a77a2 | 19 | } |
el17sm | 28:98848e6a77a2 | 20 | |
el17sm | 28:98848e6a77a2 | 21 | void RoomEngine::entrance_scene(N5110 &lcd, Gamepad &gamepad) |
el17sm | 27:a1b41626f57c | 22 | { |
el17sm | 28:98848e6a77a2 | 23 | int side = check_player_room_position(); |
el17sm | 28:98848e6a77a2 | 24 | |
el17sm | 28:98848e6a77a2 | 25 | for(int i = 0; i<50; i++) { |
el17sm | 28:98848e6a77a2 | 26 | switch(side) { |
el17sm | 28:98848e6a77a2 | 27 | case 0 : |
el17sm | 28:98848e6a77a2 | 28 | set_mapped_coord(0, -((3 +player->get_sprite_height()) / (50 * player->get_velocity()))); |
el17sm | 28:98848e6a77a2 | 29 | break; |
el17sm | 28:98848e6a77a2 | 30 | case 1 : |
el17sm | 28:98848e6a77a2 | 31 | set_mapped_coord(-((3 +player->get_hitbox_width()) / (50 * player->get_velocity())), 0); |
el17sm | 28:98848e6a77a2 | 32 | break; |
el17sm | 28:98848e6a77a2 | 33 | case 2 : |
el17sm | 28:98848e6a77a2 | 34 | set_mapped_coord(0, ((3 +player->get_hitbox_height()) / (50 * player->get_velocity()))); |
el17sm | 28:98848e6a77a2 | 35 | break; |
el17sm | 28:98848e6a77a2 | 36 | case 3 : |
el17sm | 28:98848e6a77a2 | 37 | set_mapped_coord(((3 +player->get_hitbox_width()) / (50 * player->get_velocity())), 0); |
el17sm | 28:98848e6a77a2 | 38 | break; |
el17sm | 28:98848e6a77a2 | 39 | } |
el17sm | 28:98848e6a77a2 | 40 | move_player(); |
el17sm | 28:98848e6a77a2 | 41 | render(lcd, gamepad); |
el17sm | 28:98848e6a77a2 | 42 | lcd.setContrast(0.75 - (i * 0.005)); |
el17sm | 28:98848e6a77a2 | 43 | } |
el17sm | 28:98848e6a77a2 | 44 | } |
el17sm | 28:98848e6a77a2 | 45 | |
el17sm | 28:98848e6a77a2 | 46 | void RoomEngine::exit_scene(N5110 &lcd, Gamepad &gamepad) |
el17sm | 28:98848e6a77a2 | 47 | { |
el17sm | 28:98848e6a77a2 | 48 | int side = check_player_room_position(); |
el17sm | 28:98848e6a77a2 | 49 | |
el17sm | 28:98848e6a77a2 | 50 | for(int i = 0; i<50; i++) { |
el17sm | 28:98848e6a77a2 | 51 | switch(side) { |
el17sm | 28:98848e6a77a2 | 52 | case 0 : |
el17sm | 28:98848e6a77a2 | 53 | set_mapped_coord(0, (player->get_velocity() / 2)); |
el17sm | 28:98848e6a77a2 | 54 | break; |
el17sm | 28:98848e6a77a2 | 55 | case 1 : |
el17sm | 28:98848e6a77a2 | 56 | set_mapped_coord((player->get_velocity() / 2), 0); |
el17sm | 28:98848e6a77a2 | 57 | break; |
el17sm | 28:98848e6a77a2 | 58 | case 2 : |
el17sm | 28:98848e6a77a2 | 59 | set_mapped_coord(0, -(player->get_velocity() / 2)); |
el17sm | 28:98848e6a77a2 | 60 | break; |
el17sm | 28:98848e6a77a2 | 61 | case 3 : |
el17sm | 28:98848e6a77a2 | 62 | set_mapped_coord(-(player->get_velocity() / 2), 0); |
el17sm | 28:98848e6a77a2 | 63 | break; |
el17sm | 28:98848e6a77a2 | 64 | } |
el17sm | 28:98848e6a77a2 | 65 | move_player(); |
el17sm | 28:98848e6a77a2 | 66 | render(lcd, gamepad); |
el17sm | 28:98848e6a77a2 | 67 | lcd.setContrast(0.5 + (i * 0.005)); |
el17sm | 28:98848e6a77a2 | 68 | } |
el17sm | 27:a1b41626f57c | 69 | } |
el17sm | 27:a1b41626f57c | 70 | |
el17sm | 29:6b8411bb040a | 71 | void RoomEngine::update_current_room() |
el17sm | 29:6b8411bb040a | 72 | { |
el17sm | 29:6b8411bb040a | 73 | switch(check_player_room_position()) { |
el17sm | 29:6b8411bb040a | 74 | case 0 : |
el17sm | 29:6b8411bb040a | 75 | _room_y++; |
el17sm | 29:6b8411bb040a | 76 | break; |
el17sm | 29:6b8411bb040a | 77 | case 1 : |
el17sm | 29:6b8411bb040a | 78 | _room_x++; |
el17sm | 29:6b8411bb040a | 79 | break; |
el17sm | 29:6b8411bb040a | 80 | case 2 : |
el17sm | 29:6b8411bb040a | 81 | _room_y--; |
el17sm | 29:6b8411bb040a | 82 | break; |
el17sm | 29:6b8411bb040a | 83 | case 3 : |
el17sm | 29:6b8411bb040a | 84 | _room_x--; |
el17sm | 29:6b8411bb040a | 85 | break; |
el17sm | 29:6b8411bb040a | 86 | default : |
el17sm | 29:6b8411bb040a | 87 | break; |
el17sm | 29:6b8411bb040a | 88 | } |
el17sm | 29:6b8411bb040a | 89 | } |
el17sm | 29:6b8411bb040a | 90 | |
el17sm | 27:a1b41626f57c | 91 | void RoomEngine::read_input(Gamepad &gamepad) |
el17sm | 26:abbc19edc5c1 | 92 | { |
el17sm | 26:abbc19edc5c1 | 93 | _L = gamepad.check_event(Gamepad::L_PRESSED); |
el17sm | 26:abbc19edc5c1 | 94 | _R = gamepad.check_event(Gamepad::R_PRESSED); |
el17sm | 26:abbc19edc5c1 | 95 | _A = gamepad.check_event(Gamepad::A_PRESSED); |
el17sm | 26:abbc19edc5c1 | 96 | _B = gamepad.check_event(Gamepad::B_PRESSED); |
el17sm | 26:abbc19edc5c1 | 97 | _X = gamepad.check_event(Gamepad::X_PRESSED); |
el17sm | 26:abbc19edc5c1 | 98 | _Y = gamepad.check_event(Gamepad::Y_PRESSED); |
el17sm | 26:abbc19edc5c1 | 99 | mapped_coord = gamepad.get_mapped_coord(); |
el17sm | 26:abbc19edc5c1 | 100 | } |
el17sm | 26:abbc19edc5c1 | 101 | |
el17sm | 27:a1b41626f57c | 102 | void RoomEngine::update() |
el17sm | 26:abbc19edc5c1 | 103 | { |
el17sm | 26:abbc19edc5c1 | 104 | check_damage(); |
el17sm | 27:a1b41626f57c | 105 | check_enemies_death(); |
el17sm | 26:abbc19edc5c1 | 106 | move(); |
el17sm | 27:a1b41626f57c | 107 | player->buttons(_A, _B, _Y, _X); |
el17sm | 26:abbc19edc5c1 | 108 | } |
el17sm | 26:abbc19edc5c1 | 109 | |
el17sm | 27:a1b41626f57c | 110 | void RoomEngine::render(N5110 &lcd, Gamepad &gamepad) |
el17sm | 27:a1b41626f57c | 111 | { |
el17sm | 27:a1b41626f57c | 112 | minimap_detection(lcd, gamepad); |
el17sm | 27:a1b41626f57c | 113 | pause_detection(lcd, gamepad); |
el17sm | 27:a1b41626f57c | 114 | lcd.clear(); |
el17sm | 27:a1b41626f57c | 115 | draw(lcd); |
el17sm | 27:a1b41626f57c | 116 | lcd.refresh(); |
el17sm | 27:a1b41626f57c | 117 | wait_ms(1000/40); // setting FPS |
el17sm | 27:a1b41626f57c | 118 | } |
el17sm | 27:a1b41626f57c | 119 | |
el17sm | 27:a1b41626f57c | 120 | bool RoomEngine::check_player_death() |
el17sm | 27:a1b41626f57c | 121 | { |
el17sm | 27:a1b41626f57c | 122 | // Player Death |
el17sm | 27:a1b41626f57c | 123 | if (player->get_hp() <= 0) { |
el17sm | 27:a1b41626f57c | 124 | return true; |
el17sm | 27:a1b41626f57c | 125 | } |
el17sm | 27:a1b41626f57c | 126 | return false; |
el17sm | 26:abbc19edc5c1 | 127 | } |
el17sm | 26:abbc19edc5c1 | 128 | |
el17sm | 28:98848e6a77a2 | 129 | int RoomEngine::check_player_room_position() // returns 0,1,2,3 if the player exits the respective directions, returns 4 if the player is in the room |
el17sm | 28:98848e6a77a2 | 130 | { |
el17sm | 28:98848e6a77a2 | 131 | if (player->get_pos_y() < 0) { |
el17sm | 28:98848e6a77a2 | 132 | return 0; |
el17sm | 28:98848e6a77a2 | 133 | } |
el17sm | 28:98848e6a77a2 | 134 | else if (player->get_pos_x() > WIDTH - (player->get_hitbox_width())) { |
el17sm | 28:98848e6a77a2 | 135 | return 1; |
el17sm | 28:98848e6a77a2 | 136 | } |
el17sm | 28:98848e6a77a2 | 137 | else if (player->get_pos_y() > HEIGHT - (player->get_hitbox_height())) { |
el17sm | 28:98848e6a77a2 | 138 | return 2; |
el17sm | 28:98848e6a77a2 | 139 | } |
el17sm | 28:98848e6a77a2 | 140 | else if (player->get_pos_x() < 0) { |
el17sm | 28:98848e6a77a2 | 141 | return 3; |
el17sm | 28:98848e6a77a2 | 142 | } |
el17sm | 28:98848e6a77a2 | 143 | else { |
el17sm | 28:98848e6a77a2 | 144 | return 4; |
el17sm | 28:98848e6a77a2 | 145 | } |
el17sm | 28:98848e6a77a2 | 146 | } |
el17sm | 28:98848e6a77a2 | 147 | |
el17sm | 29:6b8411bb040a | 148 | // Public Accessors |
el17sm | 29:6b8411bb040a | 149 | |
el17sm | 29:6b8411bb040a | 150 | int RoomEngine::get_room_x() |
el17sm | 29:6b8411bb040a | 151 | { |
el17sm | 29:6b8411bb040a | 152 | return _room_x; |
el17sm | 29:6b8411bb040a | 153 | } |
el17sm | 29:6b8411bb040a | 154 | int RoomEngine::get_room_y() |
el17sm | 29:6b8411bb040a | 155 | { |
el17sm | 29:6b8411bb040a | 156 | return _room_y; |
el17sm | 29:6b8411bb040a | 157 | } |
el17sm | 29:6b8411bb040a | 158 | |
el17sm | 28:98848e6a77a2 | 159 | // Private Mutators |
el17sm | 28:98848e6a77a2 | 160 | |
el17sm | 28:98848e6a77a2 | 161 | void RoomEngine::set_input(bool L, bool R, bool A, bool B, bool X, bool Y, float mapped_x, float mapped_y) |
el17sm | 28:98848e6a77a2 | 162 | { |
el17sm | 28:98848e6a77a2 | 163 | _L = L; |
el17sm | 28:98848e6a77a2 | 164 | _R = R; |
el17sm | 28:98848e6a77a2 | 165 | _A = A; |
el17sm | 28:98848e6a77a2 | 166 | _B = B; |
el17sm | 28:98848e6a77a2 | 167 | _X = X; |
el17sm | 28:98848e6a77a2 | 168 | _Y = Y; |
el17sm | 28:98848e6a77a2 | 169 | set_mapped_coord(mapped_x, mapped_y); |
el17sm | 28:98848e6a77a2 | 170 | } |
el17sm | 28:98848e6a77a2 | 171 | |
el17sm | 28:98848e6a77a2 | 172 | void RoomEngine::set_mapped_coord(float x, float y) |
el17sm | 28:98848e6a77a2 | 173 | { |
el17sm | 28:98848e6a77a2 | 174 | mapped_coord.x = x; |
el17sm | 28:98848e6a77a2 | 175 | mapped_coord.y = y; |
el17sm | 28:98848e6a77a2 | 176 | } |
el17sm | 28:98848e6a77a2 | 177 | |
el17sm | 26:abbc19edc5c1 | 178 | // Private Functions |
el17sm | 26:abbc19edc5c1 | 179 | |
el17sm | 27:a1b41626f57c | 180 | bool RoomEngine::entity_collision(Entity &a, Entity &b) // returns true if the two entity hitboxes collide |
el17sm | 26:abbc19edc5c1 | 181 | { |
el17sm | 26:abbc19edc5c1 | 182 | if (((b.get_pos_x() <= a.get_pos_x()) && (a.get_pos_x() <= b.get_pos_x() + b.get_hitbox_width() - 1)) || |
el17sm | 26:abbc19edc5c1 | 183 | ((b.get_pos_x() <= a.get_pos_x() + a.get_hitbox_width() - 1) && (a.get_pos_x() + a.get_hitbox_width() - 1 <= b.get_pos_x() + b.get_hitbox_width() - 1))) { |
el17sm | 26:abbc19edc5c1 | 184 | if (((b.get_pos_y() <= a.get_pos_y()) && (a.get_pos_y() <= b.get_pos_y() + b.get_hitbox_height() - 1)) || |
el17sm | 26:abbc19edc5c1 | 185 | ((b.get_pos_y() <= a.get_pos_y() + a.get_hitbox_height() - 1) && (a.get_pos_y() + a.get_hitbox_height() - 1 <= b.get_pos_y() + b.get_hitbox_height() - 1))) { |
el17sm | 26:abbc19edc5c1 | 186 | return true; |
el17sm | 26:abbc19edc5c1 | 187 | } |
el17sm | 26:abbc19edc5c1 | 188 | } |
el17sm | 26:abbc19edc5c1 | 189 | return 0; |
el17sm | 26:abbc19edc5c1 | 190 | } |
el17sm | 26:abbc19edc5c1 | 191 | |
el17sm | 26:abbc19edc5c1 | 192 | // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the x direction |
el17sm | 27:a1b41626f57c | 193 | float RoomEngine::entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) |
el17sm | 26:abbc19edc5c1 | 194 | { |
el17sm | 26:abbc19edc5c1 | 195 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 26:abbc19edc5c1 | 196 | if (valid_enemies[i]) { |
el17sm | 26:abbc19edc5c1 | 197 | if(i != current_entity) { |
el17sm | 26:abbc19edc5c1 | 198 | if (((array[i]->get_prev_pos_x() <= a->get_pos_x()) && (a->get_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) || |
el17sm | 26:abbc19edc5c1 | 199 | ((array[i]->get_prev_pos_x() <= a->get_pos_x() + a->get_hitbox_width() - 1) && (a->get_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1))) { |
el17sm | 26:abbc19edc5c1 | 200 | if (((array[i]->get_prev_pos_y() <= a->get_prev_pos_y()) && (a->get_prev_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) || |
el17sm | 26:abbc19edc5c1 | 201 | ((array[i]->get_prev_pos_y() <= a->get_prev_pos_y() + a->get_hitbox_height() - 1) && (a->get_prev_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1))) { |
el17sm | 26:abbc19edc5c1 | 202 | return (2*((a->get_pos_x() > array[i]->get_prev_pos_x()) - 0.5)); |
el17sm | 26:abbc19edc5c1 | 203 | } |
el17sm | 26:abbc19edc5c1 | 204 | } |
el17sm | 26:abbc19edc5c1 | 205 | } |
el17sm | 26:abbc19edc5c1 | 206 | } |
el17sm | 26:abbc19edc5c1 | 207 | } |
el17sm | 26:abbc19edc5c1 | 208 | return 0; |
el17sm | 26:abbc19edc5c1 | 209 | } |
el17sm | 26:abbc19edc5c1 | 210 | |
el17sm | 26:abbc19edc5c1 | 211 | // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the y direction |
el17sm | 27:a1b41626f57c | 212 | float RoomEngine::entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) |
el17sm | 26:abbc19edc5c1 | 213 | { |
el17sm | 26:abbc19edc5c1 | 214 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 26:abbc19edc5c1 | 215 | if (valid_enemies[i]) { |
el17sm | 26:abbc19edc5c1 | 216 | if(i != current_entity) { |
el17sm | 26:abbc19edc5c1 | 217 | if (((array[i]->get_prev_pos_x() <= a->get_prev_pos_x()) && (a->get_prev_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) || |
el17sm | 26:abbc19edc5c1 | 218 | ((array[i]->get_prev_pos_x() <= a->get_prev_pos_x() + a->get_hitbox_width() - 1) && (a->get_prev_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1))) { |
el17sm | 26:abbc19edc5c1 | 219 | if (((array[i]->get_prev_pos_y() <= a->get_pos_y()) && (a->get_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) || |
el17sm | 26:abbc19edc5c1 | 220 | ((array[i]->get_prev_pos_y() <= a->get_pos_y() + a->get_hitbox_height() - 1) && (a->get_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1))) { |
el17sm | 26:abbc19edc5c1 | 221 | return (2*((a->get_pos_y() > array[i]->get_prev_pos_y()) - 0.5)); |
el17sm | 26:abbc19edc5c1 | 222 | } |
el17sm | 26:abbc19edc5c1 | 223 | } |
el17sm | 26:abbc19edc5c1 | 224 | } |
el17sm | 26:abbc19edc5c1 | 225 | } |
el17sm | 26:abbc19edc5c1 | 226 | } |
el17sm | 26:abbc19edc5c1 | 227 | return 0; |
el17sm | 26:abbc19edc5c1 | 228 | } |
el17sm | 26:abbc19edc5c1 | 229 | |
el17sm | 27:a1b41626f57c | 230 | void RoomEngine::check_damage() |
el17sm | 26:abbc19edc5c1 | 231 | { |
el17sm | 26:abbc19edc5c1 | 232 | check_damage_player(); |
el17sm | 26:abbc19edc5c1 | 233 | check_damage_enemies(); |
el17sm | 26:abbc19edc5c1 | 234 | } |
el17sm | 26:abbc19edc5c1 | 235 | |
el17sm | 27:a1b41626f57c | 236 | void RoomEngine::check_damage_player() |
el17sm | 26:abbc19edc5c1 | 237 | { |
el17sm | 26:abbc19edc5c1 | 238 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 27:a1b41626f57c | 239 | if (room->valid_enemies[i]) { |
el17sm | 27:a1b41626f57c | 240 | if(entity_collision(*player, *room->enemies[i])) { |
el17sm | 27:a1b41626f57c | 241 | player->take_damage(room->enemies[i]->get_attack()); |
el17sm | 28:98848e6a77a2 | 242 | break; // only let 1 enemy damage player at a time |
el17sm | 26:abbc19edc5c1 | 243 | } |
el17sm | 26:abbc19edc5c1 | 244 | } |
el17sm | 27:a1b41626f57c | 245 | } |
el17sm | 26:abbc19edc5c1 | 246 | } |
el17sm | 26:abbc19edc5c1 | 247 | |
el17sm | 27:a1b41626f57c | 248 | void RoomEngine::check_damage_enemies() |
el17sm | 26:abbc19edc5c1 | 249 | { |
el17sm | 28:98848e6a77a2 | 250 | check_damage_enemies_by_bullets(); |
el17sm | 28:98848e6a77a2 | 251 | check_damage_enemies_by_collisions(); |
el17sm | 28:98848e6a77a2 | 252 | } |
el17sm | 28:98848e6a77a2 | 253 | |
el17sm | 28:98848e6a77a2 | 254 | void RoomEngine::check_damage_enemies_by_bullets() |
el17sm | 28:98848e6a77a2 | 255 | { |
el17sm | 26:abbc19edc5c1 | 256 | for (int i = 0; i < bullets_max; i++) { |
el17sm | 27:a1b41626f57c | 257 | if (player->valid_bullets[i]) { |
el17sm | 29:6b8411bb040a | 258 | if (player->bullets_array[i]->out_of_bounds_check(room->get_current_map_2d(), room->get_doorways())) { |
el17sm | 27:a1b41626f57c | 259 | player->valid_bullets[i] = false; |
el17sm | 27:a1b41626f57c | 260 | delete player->bullets_array[i]; |
el17sm | 26:abbc19edc5c1 | 261 | } else { |
el17sm | 26:abbc19edc5c1 | 262 | for (int j = 0; j < MAX_ENEMIES; j++) { |
el17sm | 27:a1b41626f57c | 263 | if (room->valid_enemies[j] && (entity_collision(*player->bullets_array[i], *room->enemies[j]))) { |
el17sm | 27:a1b41626f57c | 264 | room->enemies[j]->take_damage(player->get_attack()); |
el17sm | 27:a1b41626f57c | 265 | player->valid_bullets[i] = false; |
el17sm | 27:a1b41626f57c | 266 | delete player->bullets_array[i]; |
el17sm | 26:abbc19edc5c1 | 267 | break; |
el17sm | 26:abbc19edc5c1 | 268 | } |
el17sm | 26:abbc19edc5c1 | 269 | } |
el17sm | 26:abbc19edc5c1 | 270 | } |
el17sm | 26:abbc19edc5c1 | 271 | } |
el17sm | 26:abbc19edc5c1 | 272 | } |
el17sm | 26:abbc19edc5c1 | 273 | } |
el17sm | 26:abbc19edc5c1 | 274 | |
el17sm | 28:98848e6a77a2 | 275 | void RoomEngine::check_damage_enemies_by_collisions() |
el17sm | 28:98848e6a77a2 | 276 | { |
el17sm | 28:98848e6a77a2 | 277 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 28:98848e6a77a2 | 278 | if (room->valid_enemies[i]) { |
el17sm | 28:98848e6a77a2 | 279 | if((room->enemies[i]->is_damaged_by_collision()) && (entity_collision(*player, *room->enemies[i]))) { |
el17sm | 28:98848e6a77a2 | 280 | room->enemies[i]->take_damage(player->get_attack()); |
el17sm | 28:98848e6a77a2 | 281 | } |
el17sm | 28:98848e6a77a2 | 282 | } |
el17sm | 28:98848e6a77a2 | 283 | } |
el17sm | 28:98848e6a77a2 | 284 | } |
el17sm | 28:98848e6a77a2 | 285 | |
el17sm | 27:a1b41626f57c | 286 | void RoomEngine::check_enemies_death() |
el17sm | 26:abbc19edc5c1 | 287 | { |
el17sm | 26:abbc19edc5c1 | 288 | // Enemy Death |
el17sm | 26:abbc19edc5c1 | 289 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 27:a1b41626f57c | 290 | if (room->valid_enemies[i]) { |
el17sm | 27:a1b41626f57c | 291 | if(room->enemies[i]->get_hp() <= 0) { |
el17sm | 28:98848e6a77a2 | 292 | float temp_pos_x = room->enemies[i]->get_pos_x(); |
el17sm | 28:98848e6a77a2 | 293 | float temp_pos_y = room->enemies[i]->get_pos_y(); |
el17sm | 27:a1b41626f57c | 294 | delete room->enemies[i]; |
el17sm | 28:98848e6a77a2 | 295 | if ((rand() % 100) < room->enemies[i]->get_hp_drop_chance()){ |
el17sm | 28:98848e6a77a2 | 296 | room->enemies[i] = new Health(temp_pos_x, temp_pos_y); |
el17sm | 28:98848e6a77a2 | 297 | } else { |
el17sm | 28:98848e6a77a2 | 298 | room->valid_enemies[i] = false; |
el17sm | 28:98848e6a77a2 | 299 | } |
el17sm | 26:abbc19edc5c1 | 300 | } |
el17sm | 28:98848e6a77a2 | 301 | |
el17sm | 26:abbc19edc5c1 | 302 | } |
el17sm | 26:abbc19edc5c1 | 303 | } |
el17sm | 26:abbc19edc5c1 | 304 | } |
el17sm | 26:abbc19edc5c1 | 305 | |
el17sm | 27:a1b41626f57c | 306 | void RoomEngine::move() |
el17sm | 26:abbc19edc5c1 | 307 | { |
el17sm | 26:abbc19edc5c1 | 308 | move_player(); |
el17sm | 26:abbc19edc5c1 | 309 | move_enemies(); |
el17sm | 26:abbc19edc5c1 | 310 | move_bullets(); |
el17sm | 26:abbc19edc5c1 | 311 | } |
el17sm | 26:abbc19edc5c1 | 312 | |
el17sm | 27:a1b41626f57c | 313 | void RoomEngine::move_player() |
el17sm | 26:abbc19edc5c1 | 314 | { |
el17sm | 29:6b8411bb040a | 315 | player->move(mapped_coord.x, mapped_coord.y, room->get_current_map_2d(), room->get_doorways()); |
el17sm | 26:abbc19edc5c1 | 316 | } |
el17sm | 26:abbc19edc5c1 | 317 | |
el17sm | 27:a1b41626f57c | 318 | void RoomEngine::move_enemies() |
el17sm | 26:abbc19edc5c1 | 319 | { |
el17sm | 26:abbc19edc5c1 | 320 | // Actual Movement of Enemies |
el17sm | 26:abbc19edc5c1 | 321 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 27:a1b41626f57c | 322 | if (room->valid_enemies[i]) { |
el17sm | 27:a1b41626f57c | 323 | room->enemies[i]->update_prev_pos(); |
el17sm | 29:6b8411bb040a | 324 | room->enemies[i]->move(player->get_pos_x(), player->get_pos_y(), room->get_current_map_2d(), room->get_doorways()); |
el17sm | 26:abbc19edc5c1 | 325 | } |
el17sm | 27:a1b41626f57c | 326 | } |
el17sm | 26:abbc19edc5c1 | 327 | // Entity Collision Repulsion |
el17sm | 26:abbc19edc5c1 | 328 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 27:a1b41626f57c | 329 | if (room->valid_enemies[i]) { |
el17sm | 27:a1b41626f57c | 330 | room->enemies[i]->position_add_x(entity_move_check_x(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies)); |
el17sm | 27:a1b41626f57c | 331 | room->enemies[i]->position_add_y(entity_move_check_y(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies)); |
el17sm | 26:abbc19edc5c1 | 332 | } |
el17sm | 27:a1b41626f57c | 333 | } |
el17sm | 27:a1b41626f57c | 334 | } |
el17sm | 27:a1b41626f57c | 335 | |
el17sm | 27:a1b41626f57c | 336 | void RoomEngine::move_bullets() |
el17sm | 27:a1b41626f57c | 337 | { |
el17sm | 27:a1b41626f57c | 338 | for (int i = 0; i < bullets_max; i++) { |
el17sm | 27:a1b41626f57c | 339 | if (player->valid_bullets[i]) { |
el17sm | 29:6b8411bb040a | 340 | player->bullets_array[i]->move(player->get_bullet_speed(), 0, 0, room->get_doorways()); |
el17sm | 27:a1b41626f57c | 341 | } |
el17sm | 27:a1b41626f57c | 342 | } |
el17sm | 27:a1b41626f57c | 343 | } |
el17sm | 27:a1b41626f57c | 344 | |
el17sm | 28:98848e6a77a2 | 345 | void RoomEngine::update_player_position(int side) |
el17sm | 28:98848e6a77a2 | 346 | { |
el17sm | 28:98848e6a77a2 | 347 | switch(side) { |
el17sm | 28:98848e6a77a2 | 348 | case 0 : |
el17sm | 28:98848e6a77a2 | 349 | player->set_position(39, 49); |
el17sm | 28:98848e6a77a2 | 350 | break; |
el17sm | 28:98848e6a77a2 | 351 | case 1 : |
el17sm | 28:98848e6a77a2 | 352 | player->set_position(0 - player->get_hitbox_width(), 25); |
el17sm | 28:98848e6a77a2 | 353 | break; |
el17sm | 28:98848e6a77a2 | 354 | case 2 : |
el17sm | 28:98848e6a77a2 | 355 | player->set_position(39, 0 - player->get_hitbox_height()); |
el17sm | 28:98848e6a77a2 | 356 | break; |
el17sm | 28:98848e6a77a2 | 357 | case 3 : |
el17sm | 28:98848e6a77a2 | 358 | player->set_position(85, 25); |
el17sm | 28:98848e6a77a2 | 359 | break; |
el17sm | 28:98848e6a77a2 | 360 | case 4 : |
el17sm | 28:98848e6a77a2 | 361 | player->set_position(39, 27); |
el17sm | 28:98848e6a77a2 | 362 | break; |
el17sm | 28:98848e6a77a2 | 363 | } |
el17sm | 28:98848e6a77a2 | 364 | } |
el17sm | 28:98848e6a77a2 | 365 | |
el17sm | 27:a1b41626f57c | 366 | void RoomEngine::minimap_detection(N5110 &lcd, Gamepad &gamepad) |
el17sm | 27:a1b41626f57c | 367 | { |
el17sm | 28:98848e6a77a2 | 368 | while(gamepad.check_event(Gamepad::BACK_PRESSED)) { |
el17sm | 27:a1b41626f57c | 369 | lcd.clear(); |
el17sm | 27:a1b41626f57c | 370 | lcd.refresh(); |
el17sm | 27:a1b41626f57c | 371 | wait_ms(1000/40); |
el17sm | 26:abbc19edc5c1 | 372 | }; |
el17sm | 26:abbc19edc5c1 | 373 | } |
el17sm | 26:abbc19edc5c1 | 374 | |
el17sm | 27:a1b41626f57c | 375 | void RoomEngine::pause_detection(N5110 &lcd, Gamepad &gamepad) |
el17sm | 26:abbc19edc5c1 | 376 | { |
el17sm | 28:98848e6a77a2 | 377 | if(gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 27:a1b41626f57c | 378 | draw_health(lcd); |
el17sm | 26:abbc19edc5c1 | 379 | int * paused_screen = lcd.readScreen(); |
el17sm | 26:abbc19edc5c1 | 380 | int pause_timer = 2; |
el17sm | 26:abbc19edc5c1 | 381 | lcd.drawSpriteTransparent(20, 20, 9, 45, (int *)pause_sprite); |
el17sm | 26:abbc19edc5c1 | 382 | wait(0.05); |
el17sm | 28:98848e6a77a2 | 383 | while(gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 26:abbc19edc5c1 | 384 | lcd.clear(); |
el17sm | 26:abbc19edc5c1 | 385 | lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); |
el17sm | 26:abbc19edc5c1 | 386 | if (pause_timer % 10 <= 4) { |
el17sm | 26:abbc19edc5c1 | 387 | lcd.drawSpriteTransparent(20, 20, 9, 45, (int *)pause_sprite); |
el17sm | 26:abbc19edc5c1 | 388 | } |
el17sm | 26:abbc19edc5c1 | 389 | lcd.refresh(); |
el17sm | 26:abbc19edc5c1 | 390 | pause_timer++; |
el17sm | 26:abbc19edc5c1 | 391 | wait_ms(1000/40); |
el17sm | 27:a1b41626f57c | 392 | } |
el17sm | 26:abbc19edc5c1 | 393 | wait(0.05); |
el17sm | 26:abbc19edc5c1 | 394 | pause_timer += 2; |
el17sm | 28:98848e6a77a2 | 395 | while(!gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 26:abbc19edc5c1 | 396 | lcd.clear(); |
el17sm | 26:abbc19edc5c1 | 397 | lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); |
el17sm | 26:abbc19edc5c1 | 398 | if (pause_timer % 10 <= 4) { |
el17sm | 26:abbc19edc5c1 | 399 | lcd.drawSpriteTransparent(20, 20, 9, 45, (int *)pause_sprite); |
el17sm | 26:abbc19edc5c1 | 400 | } |
el17sm | 26:abbc19edc5c1 | 401 | lcd.refresh(); |
el17sm | 26:abbc19edc5c1 | 402 | pause_timer++; |
el17sm | 26:abbc19edc5c1 | 403 | wait_ms(1000/40); |
el17sm | 27:a1b41626f57c | 404 | } |
el17sm | 26:abbc19edc5c1 | 405 | wait(0.05); |
el17sm | 26:abbc19edc5c1 | 406 | pause_timer += 2; |
el17sm | 28:98848e6a77a2 | 407 | while(gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 26:abbc19edc5c1 | 408 | lcd.clear(); |
el17sm | 26:abbc19edc5c1 | 409 | lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); |
el17sm | 26:abbc19edc5c1 | 410 | if (pause_timer % 10 <= 4) { |
el17sm | 26:abbc19edc5c1 | 411 | lcd.drawSpriteTransparent(20, 20, 9, 45, (int *)pause_sprite); |
el17sm | 26:abbc19edc5c1 | 412 | } |
el17sm | 26:abbc19edc5c1 | 413 | lcd.refresh(); |
el17sm | 26:abbc19edc5c1 | 414 | pause_timer++; |
el17sm | 26:abbc19edc5c1 | 415 | wait_ms(1000/40); |
el17sm | 27:a1b41626f57c | 416 | } |
el17sm | 27:a1b41626f57c | 417 | } |
el17sm | 27:a1b41626f57c | 418 | } |
el17sm | 27:a1b41626f57c | 419 | |
el17sm | 27:a1b41626f57c | 420 | void RoomEngine::draw(N5110 &lcd) |
el17sm | 27:a1b41626f57c | 421 | { |
el17sm | 27:a1b41626f57c | 422 | room->draw_room(lcd); |
el17sm | 27:a1b41626f57c | 423 | //lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]); |
el17sm | 27:a1b41626f57c | 424 | draw_player(lcd); |
el17sm | 27:a1b41626f57c | 425 | draw_enemies(lcd); |
el17sm | 27:a1b41626f57c | 426 | draw_bullets(lcd); |
el17sm | 29:6b8411bb040a | 427 | room->draw_room_overlay(lcd); |
el17sm | 27:a1b41626f57c | 428 | if (_L) { |
el17sm | 27:a1b41626f57c | 429 | draw_health(lcd); |
el17sm | 26:abbc19edc5c1 | 430 | } |
el17sm | 26:abbc19edc5c1 | 431 | } |
el17sm | 26:abbc19edc5c1 | 432 | |
el17sm | 27:a1b41626f57c | 433 | void RoomEngine::draw_player(N5110 &lcd) |
el17sm | 26:abbc19edc5c1 | 434 | { |
el17sm | 27:a1b41626f57c | 435 | lcd.drawSpriteTransparent(player->get_pos_x()-player->get_offset_x(), |
el17sm | 27:a1b41626f57c | 436 | player->get_pos_y()-player->get_offset_y(), |
el17sm | 27:a1b41626f57c | 437 | player->get_sprite_height(), |
el17sm | 27:a1b41626f57c | 438 | player->get_sprite_width(), |
el17sm | 27:a1b41626f57c | 439 | player->get_frame()); |
el17sm | 26:abbc19edc5c1 | 440 | } |
el17sm | 26:abbc19edc5c1 | 441 | |
el17sm | 27:a1b41626f57c | 442 | void RoomEngine::draw_enemies(N5110 &lcd) |
el17sm | 26:abbc19edc5c1 | 443 | { |
el17sm | 26:abbc19edc5c1 | 444 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 27:a1b41626f57c | 445 | if (room->valid_enemies[i]) { |
el17sm | 27:a1b41626f57c | 446 | lcd.drawSpriteTransparent(room->enemies[i]->get_pos_x()-room->enemies[i]->get_offset_x(), |
el17sm | 27:a1b41626f57c | 447 | room->enemies[i]->get_pos_y()-room->enemies[i]->get_offset_y(), |
el17sm | 27:a1b41626f57c | 448 | room->enemies[i]->get_sprite_height(), |
el17sm | 27:a1b41626f57c | 449 | room->enemies[i]->get_sprite_width(), |
el17sm | 27:a1b41626f57c | 450 | room->enemies[i]->get_frame()); |
el17sm | 26:abbc19edc5c1 | 451 | } |
el17sm | 27:a1b41626f57c | 452 | } |
el17sm | 26:abbc19edc5c1 | 453 | } |
el17sm | 26:abbc19edc5c1 | 454 | |
el17sm | 27:a1b41626f57c | 455 | void RoomEngine::draw_bullets(N5110 &lcd) |
el17sm | 26:abbc19edc5c1 | 456 | { |
el17sm | 26:abbc19edc5c1 | 457 | for (int i = 0; i < bullets_max; i++) { |
el17sm | 27:a1b41626f57c | 458 | if (player->valid_bullets[i]) { |
el17sm | 27:a1b41626f57c | 459 | lcd.drawSpriteTransparent(player->bullets_array[i]->get_pos_x()-player->bullets_array[i]->get_offset_x(), |
el17sm | 27:a1b41626f57c | 460 | player->bullets_array[i]->get_pos_y()-player->bullets_array[i]->get_offset_y(), |
el17sm | 27:a1b41626f57c | 461 | player->bullets_array[i]->get_sprite_height(), |
el17sm | 27:a1b41626f57c | 462 | player->bullets_array[i]->get_sprite_width(), |
el17sm | 27:a1b41626f57c | 463 | player->bullets_array[i]->get_frame()); |
el17sm | 26:abbc19edc5c1 | 464 | } |
el17sm | 27:a1b41626f57c | 465 | } |
el17sm | 26:abbc19edc5c1 | 466 | } |
el17sm | 26:abbc19edc5c1 | 467 | |
el17sm | 27:a1b41626f57c | 468 | void RoomEngine::draw_health(N5110 &lcd) |
el17sm | 26:abbc19edc5c1 | 469 | { |
el17sm | 27:a1b41626f57c | 470 | for (int i = 0; i < player->get_hp(); i++){ |
el17sm | 27:a1b41626f57c | 471 | lcd.drawSpriteTransparent(i*10, |
el17sm | 27:a1b41626f57c | 472 | 0, |
el17sm | 27:a1b41626f57c | 473 | player->get_hearts_height(), |
el17sm | 27:a1b41626f57c | 474 | player->get_hearts_width(), |
el17sm | 27:a1b41626f57c | 475 | player->get_hearts_sprite()); |
el17sm | 27:a1b41626f57c | 476 | } |
el17sm | 26:abbc19edc5c1 | 477 | } |