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@49:3f83ed62d123, 2019-05-09 (annotated)
- Committer:
- el17sm
- Date:
- Thu May 09 04:24:30 2019 +0000
- Revision:
- 49:3f83ed62d123
- Parent:
- 47:6e31b195ce3c
- Child:
- 50:2c5cb92a5361
test doxygen
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17sm | 26:abbc19edc5c1 | 1 | #include "RoomEngine.h" |
el17sm | 32:fe6359ef9916 | 2 | // Constructor |
el17sm | 41:0697508a28ba | 3 | RoomEngine::RoomEngine(float global_contrast) |
el17sm | 26:abbc19edc5c1 | 4 | { |
el17sm | 46:f09711580d4a | 5 | _room_x = MAX_ROOMS_MAP_X / 2; |
el17sm | 46:f09711580d4a | 6 | _room_y = MAX_ROOMS_MAP_Y / 2; |
el17sm | 30:ec915d24d3e9 | 7 | _global_contrast = global_contrast; |
el17sm | 26:abbc19edc5c1 | 8 | } |
el17sm | 26:abbc19edc5c1 | 9 | |
el17sm | 32:fe6359ef9916 | 10 | // Destructor |
el17sm | 27:a1b41626f57c | 11 | RoomEngine::~RoomEngine() |
el17sm | 27:a1b41626f57c | 12 | { |
el17sm | 40:cbcbf6fc1421 | 13 | room->unload(); |
el17sm | 27:a1b41626f57c | 14 | } |
el17sm | 27:a1b41626f57c | 15 | |
el17sm | 32:fe6359ef9916 | 16 | // Public Functions |
el17sm | 31:ab24d028ddfd | 17 | void RoomEngine::load(Player *current_player, Room *current_room) |
el17sm | 28:98848e6a77a2 | 18 | { |
el17sm | 31:ab24d028ddfd | 19 | player = current_player; |
el17sm | 29:6b8411bb040a | 20 | room = current_room; |
el17sm | 28:98848e6a77a2 | 21 | set_input(0,0,0,0,0,0,0,0); |
el17sm | 28:98848e6a77a2 | 22 | update_player_position(check_player_room_position()); |
el17sm | 46:f09711580d4a | 23 | room->load(); |
el17sm | 28:98848e6a77a2 | 24 | } |
el17sm | 28:98848e6a77a2 | 25 | |
el17sm | 28:98848e6a77a2 | 26 | void RoomEngine::entrance_scene(N5110 &lcd, Gamepad &gamepad) |
el17sm | 27:a1b41626f57c | 27 | { |
el17sm | 28:98848e6a77a2 | 28 | int side = check_player_room_position(); |
el17sm | 28:98848e6a77a2 | 29 | for(int i = 0; i<50; i++) { |
el17sm | 49:3f83ed62d123 | 30 | switch(side) { // Setting movement of player as it enters the screen (velocity) |
el17sm | 28:98848e6a77a2 | 31 | case 0 : |
el17sm | 49:3f83ed62d123 | 32 | set_mapped_coord(0, -((4 + player->get_sprite_height()) / (50 * player->get_velocity()))); break; |
el17sm | 28:98848e6a77a2 | 33 | case 1 : |
el17sm | 49:3f83ed62d123 | 34 | set_mapped_coord(-((4 + player->get_hitbox_width()) / (50 * player->get_velocity())), 0); break; |
el17sm | 28:98848e6a77a2 | 35 | case 2 : |
el17sm | 49:3f83ed62d123 | 36 | set_mapped_coord(0, ((4 + player->get_hitbox_height()) / (50 * player->get_velocity()))); break; |
el17sm | 28:98848e6a77a2 | 37 | case 3 : |
el17sm | 49:3f83ed62d123 | 38 | set_mapped_coord(((4 + player->get_hitbox_width()) / (50 * player->get_velocity())), 0); break; |
el17sm | 28:98848e6a77a2 | 39 | } |
el17sm | 28:98848e6a77a2 | 40 | move_player(); |
el17sm | 28:98848e6a77a2 | 41 | render(lcd, gamepad); |
el17sm | 49:3f83ed62d123 | 42 | if (0.75 - (i * 0.005) > _global_contrast) { // Fade in |
el17sm | 30:ec915d24d3e9 | 43 | lcd.setContrast(0.75 - (i * 0.005)); |
el17sm | 49:3f83ed62d123 | 44 | } else { lcd.setContrast(_global_contrast);} |
el17sm | 28:98848e6a77a2 | 45 | } |
el17sm | 28:98848e6a77a2 | 46 | } |
el17sm | 28:98848e6a77a2 | 47 | |
el17sm | 28:98848e6a77a2 | 48 | void RoomEngine::exit_scene(N5110 &lcd, Gamepad &gamepad) |
el17sm | 28:98848e6a77a2 | 49 | { |
el17sm | 28:98848e6a77a2 | 50 | int side = check_player_room_position(); |
el17sm | 28:98848e6a77a2 | 51 | |
el17sm | 28:98848e6a77a2 | 52 | for(int i = 0; i<50; i++) { |
el17sm | 49:3f83ed62d123 | 53 | switch(side) { // Setting movement of player as it exits the screen (velocity) |
el17sm | 28:98848e6a77a2 | 54 | case 0 : |
el17sm | 49:3f83ed62d123 | 55 | set_mapped_coord(0, (player->get_velocity() / 2)); break; |
el17sm | 28:98848e6a77a2 | 56 | case 1 : |
el17sm | 49:3f83ed62d123 | 57 | set_mapped_coord((player->get_velocity() / 2), 0); break; |
el17sm | 28:98848e6a77a2 | 58 | case 2 : |
el17sm | 49:3f83ed62d123 | 59 | set_mapped_coord(0, -(player->get_velocity() / 2)); break; |
el17sm | 28:98848e6a77a2 | 60 | case 3 : |
el17sm | 49:3f83ed62d123 | 61 | set_mapped_coord(-(player->get_velocity() / 2), 0); break; |
el17sm | 28:98848e6a77a2 | 62 | } |
el17sm | 28:98848e6a77a2 | 63 | move_player(); |
el17sm | 28:98848e6a77a2 | 64 | render(lcd, gamepad); |
el17sm | 49:3f83ed62d123 | 65 | lcd.setContrast(_global_contrast + (i * 0.005)); // Fade out |
el17sm | 28:98848e6a77a2 | 66 | } |
el17sm | 30:ec915d24d3e9 | 67 | lcd.setContrast(0.75); |
el17sm | 27:a1b41626f57c | 68 | } |
el17sm | 27:a1b41626f57c | 69 | |
el17sm | 29:6b8411bb040a | 70 | void RoomEngine::update_current_room() |
el17sm | 29:6b8411bb040a | 71 | { |
el17sm | 29:6b8411bb040a | 72 | switch(check_player_room_position()) { |
el17sm | 29:6b8411bb040a | 73 | case 0 : |
el17sm | 46:f09711580d4a | 74 | _room_y--; |
el17sm | 29:6b8411bb040a | 75 | break; |
el17sm | 29:6b8411bb040a | 76 | case 1 : |
el17sm | 29:6b8411bb040a | 77 | _room_x++; |
el17sm | 29:6b8411bb040a | 78 | break; |
el17sm | 29:6b8411bb040a | 79 | case 2 : |
el17sm | 46:f09711580d4a | 80 | _room_y++; |
el17sm | 29:6b8411bb040a | 81 | break; |
el17sm | 29:6b8411bb040a | 82 | case 3 : |
el17sm | 29:6b8411bb040a | 83 | _room_x--; |
el17sm | 29:6b8411bb040a | 84 | break; |
el17sm | 29:6b8411bb040a | 85 | default : |
el17sm | 29:6b8411bb040a | 86 | break; |
el17sm | 29:6b8411bb040a | 87 | } |
el17sm | 29:6b8411bb040a | 88 | } |
el17sm | 29:6b8411bb040a | 89 | |
el17sm | 27:a1b41626f57c | 90 | void RoomEngine::read_input(Gamepad &gamepad) |
el17sm | 26:abbc19edc5c1 | 91 | { |
el17sm | 26:abbc19edc5c1 | 92 | _L = gamepad.check_event(Gamepad::L_PRESSED); |
el17sm | 26:abbc19edc5c1 | 93 | _R = gamepad.check_event(Gamepad::R_PRESSED); |
el17sm | 26:abbc19edc5c1 | 94 | _A = gamepad.check_event(Gamepad::A_PRESSED); |
el17sm | 26:abbc19edc5c1 | 95 | _B = gamepad.check_event(Gamepad::B_PRESSED); |
el17sm | 26:abbc19edc5c1 | 96 | _X = gamepad.check_event(Gamepad::X_PRESSED); |
el17sm | 26:abbc19edc5c1 | 97 | _Y = gamepad.check_event(Gamepad::Y_PRESSED); |
el17sm | 26:abbc19edc5c1 | 98 | mapped_coord = gamepad.get_mapped_coord(); |
el17sm | 26:abbc19edc5c1 | 99 | } |
el17sm | 26:abbc19edc5c1 | 100 | |
el17sm | 47:6e31b195ce3c | 101 | void RoomEngine::update(int &number_of_enemies_killed) |
el17sm | 26:abbc19edc5c1 | 102 | { |
el17sm | 45:8725b4171646 | 103 | room->update_doorways(); |
el17sm | 26:abbc19edc5c1 | 104 | check_damage(); |
el17sm | 47:6e31b195ce3c | 105 | check_enemies_death(number_of_enemies_killed); |
el17sm | 41:0697508a28ba | 106 | check_walls_collision(); |
el17sm | 26:abbc19edc5c1 | 107 | move(); |
el17sm | 27:a1b41626f57c | 108 | player->buttons(_A, _B, _Y, _X); |
el17sm | 26:abbc19edc5c1 | 109 | } |
el17sm | 26:abbc19edc5c1 | 110 | |
el17sm | 27:a1b41626f57c | 111 | void RoomEngine::render(N5110 &lcd, Gamepad &gamepad) |
el17sm | 27:a1b41626f57c | 112 | { |
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 | 28:98848e6a77a2 | 120 | 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 | 121 | { |
el17sm | 28:98848e6a77a2 | 122 | if (player->get_pos_y() < 0) { |
el17sm | 28:98848e6a77a2 | 123 | return 0; |
el17sm | 28:98848e6a77a2 | 124 | } |
el17sm | 28:98848e6a77a2 | 125 | else if (player->get_pos_x() > WIDTH - (player->get_hitbox_width())) { |
el17sm | 28:98848e6a77a2 | 126 | return 1; |
el17sm | 28:98848e6a77a2 | 127 | } |
el17sm | 28:98848e6a77a2 | 128 | else if (player->get_pos_y() > HEIGHT - (player->get_hitbox_height())) { |
el17sm | 28:98848e6a77a2 | 129 | return 2; |
el17sm | 28:98848e6a77a2 | 130 | } |
el17sm | 28:98848e6a77a2 | 131 | else if (player->get_pos_x() < 0) { |
el17sm | 28:98848e6a77a2 | 132 | return 3; |
el17sm | 28:98848e6a77a2 | 133 | } |
el17sm | 28:98848e6a77a2 | 134 | else { |
el17sm | 28:98848e6a77a2 | 135 | return 4; |
el17sm | 28:98848e6a77a2 | 136 | } |
el17sm | 28:98848e6a77a2 | 137 | } |
el17sm | 28:98848e6a77a2 | 138 | |
el17sm | 29:6b8411bb040a | 139 | // Public Accessors |
el17sm | 29:6b8411bb040a | 140 | |
el17sm | 29:6b8411bb040a | 141 | int RoomEngine::get_room_x() |
el17sm | 29:6b8411bb040a | 142 | { |
el17sm | 29:6b8411bb040a | 143 | return _room_x; |
el17sm | 29:6b8411bb040a | 144 | } |
el17sm | 29:6b8411bb040a | 145 | int RoomEngine::get_room_y() |
el17sm | 29:6b8411bb040a | 146 | { |
el17sm | 29:6b8411bb040a | 147 | return _room_y; |
el17sm | 29:6b8411bb040a | 148 | } |
el17sm | 29:6b8411bb040a | 149 | |
el17sm | 28:98848e6a77a2 | 150 | // Private Mutators |
el17sm | 28:98848e6a77a2 | 151 | |
el17sm | 28:98848e6a77a2 | 152 | 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 | 153 | { |
el17sm | 28:98848e6a77a2 | 154 | _L = L; |
el17sm | 28:98848e6a77a2 | 155 | _R = R; |
el17sm | 28:98848e6a77a2 | 156 | _A = A; |
el17sm | 28:98848e6a77a2 | 157 | _B = B; |
el17sm | 28:98848e6a77a2 | 158 | _X = X; |
el17sm | 28:98848e6a77a2 | 159 | _Y = Y; |
el17sm | 28:98848e6a77a2 | 160 | set_mapped_coord(mapped_x, mapped_y); |
el17sm | 28:98848e6a77a2 | 161 | } |
el17sm | 28:98848e6a77a2 | 162 | |
el17sm | 28:98848e6a77a2 | 163 | void RoomEngine::set_mapped_coord(float x, float y) |
el17sm | 28:98848e6a77a2 | 164 | { |
el17sm | 28:98848e6a77a2 | 165 | mapped_coord.x = x; |
el17sm | 28:98848e6a77a2 | 166 | mapped_coord.y = y; |
el17sm | 28:98848e6a77a2 | 167 | } |
el17sm | 28:98848e6a77a2 | 168 | |
el17sm | 26:abbc19edc5c1 | 169 | // Private Functions |
el17sm | 26:abbc19edc5c1 | 170 | |
el17sm | 27:a1b41626f57c | 171 | bool RoomEngine::entity_collision(Entity &a, Entity &b) // returns true if the two entity hitboxes collide |
el17sm | 26:abbc19edc5c1 | 172 | { |
el17sm | 26:abbc19edc5c1 | 173 | 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 | 174 | ((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 | 175 | 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 | 176 | ((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 | 177 | return true; |
el17sm | 26:abbc19edc5c1 | 178 | } |
el17sm | 26:abbc19edc5c1 | 179 | } |
el17sm | 26:abbc19edc5c1 | 180 | return 0; |
el17sm | 26:abbc19edc5c1 | 181 | } |
el17sm | 26:abbc19edc5c1 | 182 | |
el17sm | 26:abbc19edc5c1 | 183 | // 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 | 184 | float RoomEngine::entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) |
el17sm | 26:abbc19edc5c1 | 185 | { |
el17sm | 26:abbc19edc5c1 | 186 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 49:3f83ed62d123 | 187 | if ((valid_enemies[i]) && (i != current_entity)) { // only check if entity b exists and entity a is not the same as entity b |
el17sm | 49:3f83ed62d123 | 188 | 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 | 49:3f83ed62d123 | 189 | ((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 | 49:3f83ed62d123 | 190 | 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 | 49:3f83ed62d123 | 191 | ((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 | 49:3f83ed62d123 | 192 | return (2*((a->get_pos_x() > array[i]->get_prev_pos_x()) - 0.5)); |
el17sm | 26:abbc19edc5c1 | 193 | } |
el17sm | 26:abbc19edc5c1 | 194 | } |
el17sm | 26:abbc19edc5c1 | 195 | } |
el17sm | 26:abbc19edc5c1 | 196 | } |
el17sm | 26:abbc19edc5c1 | 197 | return 0; |
el17sm | 26:abbc19edc5c1 | 198 | } |
el17sm | 26:abbc19edc5c1 | 199 | |
el17sm | 26:abbc19edc5c1 | 200 | // 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 | 201 | float RoomEngine::entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) |
el17sm | 26:abbc19edc5c1 | 202 | { |
el17sm | 26:abbc19edc5c1 | 203 | for (int i = 0; i < no_of_enemies; i++) { |
el17sm | 49:3f83ed62d123 | 204 | if ((valid_enemies[i]) && (i != current_entity)) { // only check if entity b exists and entity a is not the same as entity b |
el17sm | 49:3f83ed62d123 | 205 | 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 | 49:3f83ed62d123 | 206 | ((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 | 49:3f83ed62d123 | 207 | 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 | 49:3f83ed62d123 | 208 | ((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 | 49:3f83ed62d123 | 209 | return (2*((a->get_pos_y() > array[i]->get_prev_pos_y()) - 0.5)); |
el17sm | 26:abbc19edc5c1 | 210 | } |
el17sm | 26:abbc19edc5c1 | 211 | } |
el17sm | 26:abbc19edc5c1 | 212 | } |
el17sm | 26:abbc19edc5c1 | 213 | } |
el17sm | 26:abbc19edc5c1 | 214 | return 0; |
el17sm | 26:abbc19edc5c1 | 215 | } |
el17sm | 26:abbc19edc5c1 | 216 | |
el17sm | 27:a1b41626f57c | 217 | void RoomEngine::check_damage() |
el17sm | 26:abbc19edc5c1 | 218 | { |
el17sm | 26:abbc19edc5c1 | 219 | check_damage_player(); |
el17sm | 26:abbc19edc5c1 | 220 | check_damage_enemies(); |
el17sm | 26:abbc19edc5c1 | 221 | } |
el17sm | 26:abbc19edc5c1 | 222 | |
el17sm | 27:a1b41626f57c | 223 | void RoomEngine::check_damage_player() |
el17sm | 26:abbc19edc5c1 | 224 | { |
el17sm | 26:abbc19edc5c1 | 225 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 27:a1b41626f57c | 226 | if (room->valid_enemies[i]) { |
el17sm | 27:a1b41626f57c | 227 | if(entity_collision(*player, *room->enemies[i])) { |
el17sm | 27:a1b41626f57c | 228 | player->take_damage(room->enemies[i]->get_attack()); |
el17sm | 28:98848e6a77a2 | 229 | break; // only let 1 enemy damage player at a time |
el17sm | 26:abbc19edc5c1 | 230 | } |
el17sm | 26:abbc19edc5c1 | 231 | } |
el17sm | 37:a404860171a9 | 232 | if (room->valid_collectibles[i]) { |
el17sm | 37:a404860171a9 | 233 | if(entity_collision(*player, *room->collectibles[i])) { |
el17sm | 37:a404860171a9 | 234 | player->take_damage(room->collectibles[i]->get_attack()); |
el17sm | 37:a404860171a9 | 235 | delete room->collectibles[i]; |
el17sm | 37:a404860171a9 | 236 | room->valid_collectibles[i] = false; |
el17sm | 37:a404860171a9 | 237 | break; // only let 1 heart heal player at a time |
el17sm | 37:a404860171a9 | 238 | } |
el17sm | 37:a404860171a9 | 239 | } |
el17sm | 27:a1b41626f57c | 240 | } |
el17sm | 26:abbc19edc5c1 | 241 | } |
el17sm | 26:abbc19edc5c1 | 242 | |
el17sm | 27:a1b41626f57c | 243 | void RoomEngine::check_damage_enemies() |
el17sm | 26:abbc19edc5c1 | 244 | { |
el17sm | 26:abbc19edc5c1 | 245 | for (int i = 0; i < bullets_max; i++) { |
el17sm | 27:a1b41626f57c | 246 | if (player->valid_bullets[i]) { |
el17sm | 30:ec915d24d3e9 | 247 | if (player->update_bullets(room->get_current_map_2d(), room->get_doorways())) { |
el17sm | 26:abbc19edc5c1 | 248 | } else { |
el17sm | 26:abbc19edc5c1 | 249 | for (int j = 0; j < MAX_ENEMIES; j++) { |
el17sm | 27:a1b41626f57c | 250 | if (room->valid_enemies[j] && (entity_collision(*player->bullets_array[i], *room->enemies[j]))) { |
el17sm | 27:a1b41626f57c | 251 | room->enemies[j]->take_damage(player->get_attack()); |
el17sm | 27:a1b41626f57c | 252 | player->valid_bullets[i] = false; |
el17sm | 27:a1b41626f57c | 253 | delete player->bullets_array[i]; |
el17sm | 26:abbc19edc5c1 | 254 | break; |
el17sm | 26:abbc19edc5c1 | 255 | } |
el17sm | 26:abbc19edc5c1 | 256 | } |
el17sm | 26:abbc19edc5c1 | 257 | } |
el17sm | 26:abbc19edc5c1 | 258 | } |
el17sm | 26:abbc19edc5c1 | 259 | } |
el17sm | 26:abbc19edc5c1 | 260 | } |
el17sm | 26:abbc19edc5c1 | 261 | |
el17sm | 47:6e31b195ce3c | 262 | void RoomEngine::check_enemies_death(int &number_of_enemies_killed) |
el17sm | 26:abbc19edc5c1 | 263 | { |
el17sm | 26:abbc19edc5c1 | 264 | // Enemy Death |
el17sm | 26:abbc19edc5c1 | 265 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 37:a404860171a9 | 266 | if((room->valid_enemies[i]) && (room->enemies[i]->get_hp() <= 0)) { |
el17sm | 37:a404860171a9 | 267 | if ((rand() % 100) < room->enemies[i]->get_hp_drop_chance()){ |
el17sm | 37:a404860171a9 | 268 | for (int j = 0; j < MAX_ENEMIES; j++) { |
el17sm | 37:a404860171a9 | 269 | if (!room->valid_collectibles[j]) { |
el17sm | 37:a404860171a9 | 270 | room->collectibles[j] = new Health(room->enemies[i]->get_pos_x(), room->enemies[i]->get_pos_y()); // Spawn a health drop |
el17sm | 37:a404860171a9 | 271 | room->valid_collectibles[j] = true; |
el17sm | 37:a404860171a9 | 272 | break; |
el17sm | 37:a404860171a9 | 273 | } |
el17sm | 28:98848e6a77a2 | 274 | } |
el17sm | 26:abbc19edc5c1 | 275 | } |
el17sm | 37:a404860171a9 | 276 | delete room->enemies[i]; |
el17sm | 37:a404860171a9 | 277 | room->valid_enemies[i] = false; |
el17sm | 47:6e31b195ce3c | 278 | number_of_enemies_killed++; |
el17sm | 26:abbc19edc5c1 | 279 | } |
el17sm | 26:abbc19edc5c1 | 280 | } |
el17sm | 26:abbc19edc5c1 | 281 | } |
el17sm | 26:abbc19edc5c1 | 282 | |
el17sm | 41:0697508a28ba | 283 | void RoomEngine::check_walls_collision() |
el17sm | 41:0697508a28ba | 284 | { |
el17sm | 44:cc4cecfc639f | 285 | // Enemy |
el17sm | 44:cc4cecfc639f | 286 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 44:cc4cecfc639f | 287 | if(room->valid_enemies[i]) { |
el17sm | 44:cc4cecfc639f | 288 | room->enemies[i]->undo_move_x(entity_move_check_x(room->enemies[i], room->walls, 2, 10, room->valid_walls)); |
el17sm | 44:cc4cecfc639f | 289 | room->enemies[i]->undo_move_y(entity_move_check_y(room->enemies[i], room->walls, 2, 10, room->valid_walls)); |
el17sm | 44:cc4cecfc639f | 290 | } |
el17sm | 44:cc4cecfc639f | 291 | } |
el17sm | 44:cc4cecfc639f | 292 | // Player |
el17sm | 44:cc4cecfc639f | 293 | player->undo_move_x(entity_move_check_x(player, room->walls, 2, 10, room->valid_walls)); |
el17sm | 44:cc4cecfc639f | 294 | player->undo_move_y(entity_move_check_y(player, room->walls, 2, 10, room->valid_walls)); |
el17sm | 44:cc4cecfc639f | 295 | // Bullets |
el17sm | 44:cc4cecfc639f | 296 | for (int i = 0; i < bullets_max; i++) { |
el17sm | 44:cc4cecfc639f | 297 | for (int j = 0; j < 2; j++) { |
el17sm | 44:cc4cecfc639f | 298 | if ((player->valid_bullets[i]) && (room->valid_walls[j]) && (entity_collision(*player->bullets_array[i], *room->walls[j]))) { |
el17sm | 44:cc4cecfc639f | 299 | delete player->bullets_array[i]; player->valid_bullets[i] = false; |
el17sm | 44:cc4cecfc639f | 300 | } |
el17sm | 44:cc4cecfc639f | 301 | } |
el17sm | 44:cc4cecfc639f | 302 | } |
el17sm | 41:0697508a28ba | 303 | } |
el17sm | 41:0697508a28ba | 304 | |
el17sm | 27:a1b41626f57c | 305 | void RoomEngine::move() |
el17sm | 26:abbc19edc5c1 | 306 | { |
el17sm | 26:abbc19edc5c1 | 307 | move_player(); |
el17sm | 26:abbc19edc5c1 | 308 | move_enemies(); |
el17sm | 26:abbc19edc5c1 | 309 | } |
el17sm | 26:abbc19edc5c1 | 310 | |
el17sm | 27:a1b41626f57c | 311 | void RoomEngine::move_player() |
el17sm | 26:abbc19edc5c1 | 312 | { |
el17sm | 29:6b8411bb040a | 313 | player->move(mapped_coord.x, mapped_coord.y, room->get_current_map_2d(), room->get_doorways()); |
el17sm | 26:abbc19edc5c1 | 314 | } |
el17sm | 26:abbc19edc5c1 | 315 | |
el17sm | 27:a1b41626f57c | 316 | void RoomEngine::move_enemies() |
el17sm | 26:abbc19edc5c1 | 317 | { |
el17sm | 26:abbc19edc5c1 | 318 | // Actual Movement of Enemies |
el17sm | 26:abbc19edc5c1 | 319 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 27:a1b41626f57c | 320 | if (room->valid_enemies[i]) { |
el17sm | 27:a1b41626f57c | 321 | room->enemies[i]->update_prev_pos(); |
el17sm | 29:6b8411bb040a | 322 | room->enemies[i]->move(player->get_pos_x(), player->get_pos_y(), room->get_current_map_2d(), room->get_doorways()); |
el17sm | 26:abbc19edc5c1 | 323 | } |
el17sm | 27:a1b41626f57c | 324 | } |
el17sm | 26:abbc19edc5c1 | 325 | // Entity Collision Repulsion |
el17sm | 26:abbc19edc5c1 | 326 | for (int i = 0; i < MAX_ENEMIES; i++) { |
el17sm | 37:a404860171a9 | 327 | if (room->valid_enemies[i]) { |
el17sm | 27:a1b41626f57c | 328 | room->enemies[i]->position_add_x(entity_move_check_x(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies)); |
el17sm | 27:a1b41626f57c | 329 | room->enemies[i]->position_add_y(entity_move_check_y(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies)); |
el17sm | 26:abbc19edc5c1 | 330 | } |
el17sm | 27:a1b41626f57c | 331 | } |
el17sm | 27:a1b41626f57c | 332 | } |
el17sm | 27:a1b41626f57c | 333 | |
el17sm | 28:98848e6a77a2 | 334 | void RoomEngine::update_player_position(int side) |
el17sm | 28:98848e6a77a2 | 335 | { |
el17sm | 28:98848e6a77a2 | 336 | switch(side) { |
el17sm | 28:98848e6a77a2 | 337 | case 0 : |
el17sm | 28:98848e6a77a2 | 338 | player->set_position(39, 49); |
el17sm | 28:98848e6a77a2 | 339 | break; |
el17sm | 28:98848e6a77a2 | 340 | case 1 : |
el17sm | 28:98848e6a77a2 | 341 | player->set_position(0 - player->get_hitbox_width(), 25); |
el17sm | 28:98848e6a77a2 | 342 | break; |
el17sm | 28:98848e6a77a2 | 343 | case 2 : |
el17sm | 28:98848e6a77a2 | 344 | player->set_position(39, 0 - player->get_hitbox_height()); |
el17sm | 28:98848e6a77a2 | 345 | break; |
el17sm | 28:98848e6a77a2 | 346 | case 3 : |
el17sm | 28:98848e6a77a2 | 347 | player->set_position(85, 25); |
el17sm | 28:98848e6a77a2 | 348 | break; |
el17sm | 28:98848e6a77a2 | 349 | case 4 : |
el17sm | 41:0697508a28ba | 350 | player->set_position(39, 37); |
el17sm | 28:98848e6a77a2 | 351 | break; |
el17sm | 28:98848e6a77a2 | 352 | } |
el17sm | 28:98848e6a77a2 | 353 | } |
el17sm | 28:98848e6a77a2 | 354 | |
el17sm | 27:a1b41626f57c | 355 | void RoomEngine::pause_detection(N5110 &lcd, Gamepad &gamepad) |
el17sm | 26:abbc19edc5c1 | 356 | { |
el17sm | 28:98848e6a77a2 | 357 | if(gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 27:a1b41626f57c | 358 | draw_health(lcd); |
el17sm | 33:4f3948dcd2f7 | 359 | char * paused_screen = lcd.readScreen(); |
el17sm | 26:abbc19edc5c1 | 360 | int pause_timer = 2; |
el17sm | 33:4f3948dcd2f7 | 361 | lcd.drawSpriteTransparent(20, 20, 9, 45, (char *)pause_sprite); |
el17sm | 28:98848e6a77a2 | 362 | while(gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 49:3f83ed62d123 | 363 | draw_pause_screen(lcd, paused_screen, pause_timer); |
el17sm | 27:a1b41626f57c | 364 | } |
el17sm | 26:abbc19edc5c1 | 365 | pause_timer += 2; |
el17sm | 28:98848e6a77a2 | 366 | while(!gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 49:3f83ed62d123 | 367 | draw_pause_screen(lcd, paused_screen, pause_timer); |
el17sm | 27:a1b41626f57c | 368 | } |
el17sm | 26:abbc19edc5c1 | 369 | pause_timer += 2; |
el17sm | 28:98848e6a77a2 | 370 | while(gamepad.check_event(Gamepad::START_PRESSED)) { |
el17sm | 49:3f83ed62d123 | 371 | draw_pause_screen(lcd, paused_screen, pause_timer); |
el17sm | 27:a1b41626f57c | 372 | } |
el17sm | 27:a1b41626f57c | 373 | } |
el17sm | 27:a1b41626f57c | 374 | } |
el17sm | 27:a1b41626f57c | 375 | |
el17sm | 49:3f83ed62d123 | 376 | void RoomEngine::draw_pause_screen(N5110 &lcd, char * paused_screen, int &pause_timer) |
el17sm | 49:3f83ed62d123 | 377 | { |
el17sm | 49:3f83ed62d123 | 378 | lcd.clear(); |
el17sm | 49:3f83ed62d123 | 379 | lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); |
el17sm | 49:3f83ed62d123 | 380 | if (pause_timer % 10 <= 4) { |
el17sm | 49:3f83ed62d123 | 381 | lcd.drawSpriteTransparent(20, 20, 9, 45, (char *)pause_sprite); |
el17sm | 49:3f83ed62d123 | 382 | } |
el17sm | 49:3f83ed62d123 | 383 | lcd.refresh(); |
el17sm | 49:3f83ed62d123 | 384 | pause_timer++; |
el17sm | 49:3f83ed62d123 | 385 | wait_ms(1000/40); |
el17sm | 49:3f83ed62d123 | 386 | } |
el17sm | 49:3f83ed62d123 | 387 | |
el17sm | 27:a1b41626f57c | 388 | void RoomEngine::draw(N5110 &lcd) |
el17sm | 27:a1b41626f57c | 389 | { |
el17sm | 27:a1b41626f57c | 390 | room->draw_room(lcd); |
el17sm | 34:1d5b4da3935e | 391 | //lcd.drawSprite(0,0,screen_height,screen_width,(char *)level_map[1]); |
el17sm | 39:0c2521949429 | 392 | for(int j = 0; j < HEIGHT; j++) { |
el17sm | 39:0c2521949429 | 393 | if (j == player->get_pos_y()) { |
el17sm | 39:0c2521949429 | 394 | player->draw(lcd); |
el17sm | 39:0c2521949429 | 395 | } |
el17sm | 40:cbcbf6fc1421 | 396 | player->draw_bullets(lcd, j); |
el17sm | 41:0697508a28ba | 397 | room->draw(lcd, j); |
el17sm | 39:0c2521949429 | 398 | } |
el17sm | 39:0c2521949429 | 399 | |
el17sm | 29:6b8411bb040a | 400 | room->draw_room_overlay(lcd); |
el17sm | 27:a1b41626f57c | 401 | if (_L) { |
el17sm | 27:a1b41626f57c | 402 | draw_health(lcd); |
el17sm | 26:abbc19edc5c1 | 403 | } |
el17sm | 26:abbc19edc5c1 | 404 | } |
el17sm | 26:abbc19edc5c1 | 405 | |
el17sm | 27:a1b41626f57c | 406 | void RoomEngine::draw_health(N5110 &lcd) |
el17sm | 26:abbc19edc5c1 | 407 | { |
el17sm | 27:a1b41626f57c | 408 | for (int i = 0; i < player->get_hp(); i++){ |
el17sm | 27:a1b41626f57c | 409 | lcd.drawSpriteTransparent(i*10, |
el17sm | 27:a1b41626f57c | 410 | 0, |
el17sm | 27:a1b41626f57c | 411 | player->get_hearts_height(), |
el17sm | 27:a1b41626f57c | 412 | player->get_hearts_width(), |
el17sm | 27:a1b41626f57c | 413 | player->get_hearts_sprite()); |
el17sm | 27:a1b41626f57c | 414 | } |
el17sm | 26:abbc19edc5c1 | 415 | } |