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
- Committer:
- el17sm
- Date:
- 2019-04-29
- Revision:
- 27:a1b41626f57c
- Parent:
- 26:abbc19edc5c1
- Child:
- 28:98848e6a77a2
File content as of revision 27:a1b41626f57c:
#include "RoomEngine.h" RoomEngine::RoomEngine() { player = new Player(39, 27); } RoomEngine::~RoomEngine() { delete player; } void RoomEngine::load_room(Room ¤t_room) { room = ¤t_room; } void RoomEngine::read_input(Gamepad &gamepad) { _L = gamepad.check_event(Gamepad::L_PRESSED); _R = gamepad.check_event(Gamepad::R_PRESSED); _START = gamepad.check_event(Gamepad::START_PRESSED); _BACK = gamepad.check_event(Gamepad::BACK_PRESSED); _A = gamepad.check_event(Gamepad::A_PRESSED); _B = gamepad.check_event(Gamepad::B_PRESSED); _X = gamepad.check_event(Gamepad::X_PRESSED); _Y = gamepad.check_event(Gamepad::Y_PRESSED); mapped_coord = gamepad.get_mapped_coord(); } void RoomEngine::update() { check_damage(); check_enemies_death(); move(); player->buttons(_A, _B, _Y, _X); } void RoomEngine::render(N5110 &lcd, Gamepad &gamepad) { minimap_detection(lcd, gamepad); pause_detection(lcd, gamepad); lcd.clear(); draw(lcd); lcd.refresh(); wait_ms(1000/40); // setting FPS } bool RoomEngine::check_player_death() { // Player Death if (player->get_hp() <= 0) { return true; } return false; } // Private Functions bool RoomEngine::entity_collision(Entity &a, Entity &b) // returns true if the two entity hitboxes collide { if (((b.get_pos_x() <= a.get_pos_x()) && (a.get_pos_x() <= b.get_pos_x() + b.get_hitbox_width() - 1)) || ((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))) { if (((b.get_pos_y() <= a.get_pos_y()) && (a.get_pos_y() <= b.get_pos_y() + b.get_hitbox_height() - 1)) || ((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))) { return true; } } return 0; } // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the x direction float RoomEngine::entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) { for (int i = 0; i < no_of_enemies; i++) { if (valid_enemies[i]) { if(i != current_entity) { 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)) || ((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))) { 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)) || ((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))) { return (2*((a->get_pos_x() > array[i]->get_prev_pos_x()) - 0.5)); } } } } } return 0; } // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the y direction float RoomEngine::entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]) { for (int i = 0; i < no_of_enemies; i++) { if (valid_enemies[i]) { if(i != current_entity) { 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)) || ((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))) { 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)) || ((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))) { return (2*((a->get_pos_y() > array[i]->get_prev_pos_y()) - 0.5)); } } } } } return 0; } void RoomEngine::check_damage() { check_damage_player(); check_damage_enemies(); } void RoomEngine::check_damage_player() { for (int i = 0; i < MAX_ENEMIES; i++) { if (room->valid_enemies[i]) { if(entity_collision(*player, *room->enemies[i])) { player->take_damage(room->enemies[i]->get_attack()); } } } } void RoomEngine::check_damage_enemies() { for (int i = 0; i < bullets_max; i++) { if (player->valid_bullets[i]) { if (player->bullets_array[i]->out_of_bounds_check(room->get_current_map_2d())) { player->valid_bullets[i] = false; delete player->bullets_array[i]; } else { for (int j = 0; j < MAX_ENEMIES; j++) { if (room->valid_enemies[j] && (entity_collision(*player->bullets_array[i], *room->enemies[j]))) { room->enemies[j]->take_damage(player->get_attack()); player->valid_bullets[i] = false; delete player->bullets_array[i]; break; } } } } } } void RoomEngine::check_enemies_death() { // Enemy Death for (int i = 0; i < MAX_ENEMIES; i++) { if (room->valid_enemies[i]) { if(room->enemies[i]->get_hp() <= 0) { room->valid_enemies[i] = false; delete room->enemies[i]; } } } } void RoomEngine::move() { move_player(); move_enemies(); move_bullets(); } void RoomEngine::move_player() { player->move(mapped_coord.x, mapped_coord.y, room->get_current_map_2d()); } void RoomEngine::move_enemies() { // Actual Movement of Enemies for (int i = 0; i < MAX_ENEMIES; i++) { if (room->valid_enemies[i]) { room->enemies[i]->update_prev_pos(); room->enemies[i]->move(player->get_pos_x(), player->get_pos_y(), room->get_current_map_2d()); } } // Entity Collision Repulsion for (int i = 0; i < MAX_ENEMIES; i++) { if (room->valid_enemies[i]) { room->enemies[i]->position_add_x(entity_move_check_x(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies)); room->enemies[i]->position_add_y(entity_move_check_y(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies)); } } } void RoomEngine::move_bullets() { for (int i = 0; i < bullets_max; i++) { if (player->valid_bullets[i]) { player->bullets_array[i]->move(player->get_bullet_speed(), 0, 0); } } } void RoomEngine::minimap_detection(N5110 &lcd, Gamepad &gamepad) { while(_BACK) { _BACK = gamepad.check_event(Gamepad::BACK_PRESSED); lcd.clear(); lcd.refresh(); wait_ms(1000/40); }; } void RoomEngine::pause_detection(N5110 &lcd, Gamepad &gamepad) { if(_START) { draw_health(lcd); int * paused_screen = lcd.readScreen(); int pause_timer = 2; lcd.drawSpriteTransparent(20, 20, 9, 45, (int *)pause_sprite); wait(0.05); while(_START) { _START = gamepad.check_event(Gamepad::START_PRESSED); lcd.clear(); lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); if (pause_timer % 10 <= 4) { lcd.drawSpriteTransparent(20, 20, 9, 45, (int *)pause_sprite); } lcd.refresh(); pause_timer++; wait_ms(1000/40); } wait(0.05); pause_timer += 2; while(!_START) { _START = gamepad.check_event(Gamepad::START_PRESSED); lcd.clear(); lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); if (pause_timer % 10 <= 4) { lcd.drawSpriteTransparent(20, 20, 9, 45, (int *)pause_sprite); } lcd.refresh(); pause_timer++; wait_ms(1000/40); } wait(0.05); pause_timer += 2; while(_START) { _START = gamepad.check_event(Gamepad::START_PRESSED); lcd.clear(); lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); if (pause_timer % 10 <= 4) { lcd.drawSpriteTransparent(20, 20, 9, 45, (int *)pause_sprite); } lcd.refresh(); pause_timer++; wait_ms(1000/40); } } } void RoomEngine::draw(N5110 &lcd) { room->draw_room(lcd); //lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]); draw_player(lcd); draw_enemies(lcd); draw_bullets(lcd); if (_L) { draw_health(lcd); } } void RoomEngine::draw_player(N5110 &lcd) { lcd.drawSpriteTransparent(player->get_pos_x()-player->get_offset_x(), player->get_pos_y()-player->get_offset_y(), player->get_sprite_height(), player->get_sprite_width(), player->get_frame()); } void RoomEngine::draw_enemies(N5110 &lcd) { for (int i = 0; i < MAX_ENEMIES; i++) { if (room->valid_enemies[i]) { lcd.drawSpriteTransparent(room->enemies[i]->get_pos_x()-room->enemies[i]->get_offset_x(), room->enemies[i]->get_pos_y()-room->enemies[i]->get_offset_y(), room->enemies[i]->get_sprite_height(), room->enemies[i]->get_sprite_width(), room->enemies[i]->get_frame()); } } } void RoomEngine::draw_bullets(N5110 &lcd) { for (int i = 0; i < bullets_max; i++) { if (player->valid_bullets[i]) { lcd.drawSpriteTransparent(player->bullets_array[i]->get_pos_x()-player->bullets_array[i]->get_offset_x(), player->bullets_array[i]->get_pos_y()-player->bullets_array[i]->get_offset_y(), player->bullets_array[i]->get_sprite_height(), player->bullets_array[i]->get_sprite_width(), player->bullets_array[i]->get_frame()); } } } void RoomEngine::draw_health(N5110 &lcd) { for (int i = 0; i < player->get_hp(); i++){ lcd.drawSpriteTransparent(i*10, 0, player->get_hearts_height(), player->get_hearts_width(), player->get_hearts_sprite()); } }