A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.
Dependencies: mbed MotionSensor
Diff: RoomEngine/RoomEngine.cpp
- Revision:
- 28:98848e6a77a2
- Parent:
- 27:a1b41626f57c
- Child:
- 29:6b8411bb040a
--- a/RoomEngine/RoomEngine.cpp Mon Apr 29 10:39:09 2019 +0000 +++ b/RoomEngine/RoomEngine.cpp Thu May 02 21:30:49 2019 +0000 @@ -1,7 +1,7 @@ #include "RoomEngine.h" RoomEngine::RoomEngine() { - player = new Player(39, 27); + } RoomEngine::~RoomEngine() @@ -9,17 +9,73 @@ delete player; } -void RoomEngine::load_room(Room ¤t_room) +void RoomEngine::init(Player ¤t_player, Room ¤t_room) +{ + player = ¤t_player; + room = ¤t_room; + set_input(0,0,0,0,0,0,0,0); + update_player_position(check_player_room_position()); +} + +void RoomEngine::save_room(Room ¤t_room) +{ + +} + +void RoomEngine::entrance_scene(N5110 &lcd, Gamepad &gamepad) { - room = ¤t_room; + int side = check_player_room_position(); + + for(int i = 0; i<50; i++) { + switch(side) { + case 0 : + set_mapped_coord(0, -((3 +player->get_sprite_height()) / (50 * player->get_velocity()))); + break; + case 1 : + set_mapped_coord(-((3 +player->get_hitbox_width()) / (50 * player->get_velocity())), 0); + break; + case 2 : + set_mapped_coord(0, ((3 +player->get_hitbox_height()) / (50 * player->get_velocity()))); + break; + case 3 : + set_mapped_coord(((3 +player->get_hitbox_width()) / (50 * player->get_velocity())), 0); + break; + } + move_player(); + render(lcd, gamepad); + lcd.setContrast(0.75 - (i * 0.005)); + } +} + +void RoomEngine::exit_scene(N5110 &lcd, Gamepad &gamepad) +{ + int side = check_player_room_position(); + + for(int i = 0; i<50; i++) { + switch(side) { + case 0 : + set_mapped_coord(0, (player->get_velocity() / 2)); + break; + case 1 : + set_mapped_coord((player->get_velocity() / 2), 0); + break; + case 2 : + set_mapped_coord(0, -(player->get_velocity() / 2)); + break; + case 3 : + set_mapped_coord(-(player->get_velocity() / 2), 0); + break; + } + move_player(); + render(lcd, gamepad); + lcd.setContrast(0.5 + (i * 0.005)); + } } 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); @@ -54,6 +110,44 @@ return false; } +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 +{ + if (player->get_pos_y() < 0) { + return 0; + } + else if (player->get_pos_x() > WIDTH - (player->get_hitbox_width())) { + return 1; + } + else if (player->get_pos_y() > HEIGHT - (player->get_hitbox_height())) { + return 2; + } + else if (player->get_pos_x() < 0) { + return 3; + } + else { + return 4; + } +} + +// Private Mutators + +void RoomEngine::set_input(bool L, bool R, bool A, bool B, bool X, bool Y, float mapped_x, float mapped_y) +{ + _L = L; + _R = R; + _A = A; + _B = B; + _X = X; + _Y = Y; + set_mapped_coord(mapped_x, mapped_y); +} + +void RoomEngine::set_mapped_coord(float x, float y) +{ + mapped_coord.x = x; + mapped_coord.y = y; +} + // Private Functions bool RoomEngine::entity_collision(Entity &a, Entity &b) // returns true if the two entity hitboxes collide @@ -118,6 +212,7 @@ if (room->valid_enemies[i]) { if(entity_collision(*player, *room->enemies[i])) { player->take_damage(room->enemies[i]->get_attack()); + break; // only let 1 enemy damage player at a time } } } @@ -125,6 +220,12 @@ void RoomEngine::check_damage_enemies() { + check_damage_enemies_by_bullets(); + check_damage_enemies_by_collisions(); +} + +void RoomEngine::check_damage_enemies_by_bullets() +{ 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())) { @@ -144,15 +245,33 @@ } } +void RoomEngine::check_damage_enemies_by_collisions() +{ + for (int i = 0; i < MAX_ENEMIES; i++) { + if (room->valid_enemies[i]) { + if((room->enemies[i]->is_damaged_by_collision()) && (entity_collision(*player, *room->enemies[i]))) { + room->enemies[i]->take_damage(player->get_attack()); + } + } + } +} + 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; + float temp_pos_x = room->enemies[i]->get_pos_x(); + float temp_pos_y = room->enemies[i]->get_pos_y(); delete room->enemies[i]; + if ((rand() % 100) < room->enemies[i]->get_hp_drop_chance()){ + room->enemies[i] = new Health(temp_pos_x, temp_pos_y); + } else { + room->valid_enemies[i] = false; + } } + } } } @@ -196,10 +315,30 @@ } } +void RoomEngine::update_player_position(int side) +{ + switch(side) { + case 0 : + player->set_position(39, 49); + break; + case 1 : + player->set_position(0 - player->get_hitbox_width(), 25); + break; + case 2 : + player->set_position(39, 0 - player->get_hitbox_height()); + break; + case 3 : + player->set_position(85, 25); + break; + case 4 : + player->set_position(39, 27); + break; + } +} + void RoomEngine::minimap_detection(N5110 &lcd, Gamepad &gamepad) { - while(_BACK) { - _BACK = gamepad.check_event(Gamepad::BACK_PRESSED); + while(gamepad.check_event(Gamepad::BACK_PRESSED)) { lcd.clear(); lcd.refresh(); wait_ms(1000/40); @@ -208,14 +347,13 @@ void RoomEngine::pause_detection(N5110 &lcd, Gamepad &gamepad) { - if(_START) { + if(gamepad.check_event(Gamepad::START_PRESSED)) { 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); + while(gamepad.check_event(Gamepad::START_PRESSED)) { lcd.clear(); lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); if (pause_timer % 10 <= 4) { @@ -227,8 +365,7 @@ } wait(0.05); pause_timer += 2; - while(!_START) { - _START = gamepad.check_event(Gamepad::START_PRESSED); + while(!gamepad.check_event(Gamepad::START_PRESSED)) { lcd.clear(); lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); if (pause_timer % 10 <= 4) { @@ -240,8 +377,7 @@ } wait(0.05); pause_timer += 2; - while(_START) { - _START = gamepad.check_event(Gamepad::START_PRESSED); + while(gamepad.check_event(Gamepad::START_PRESSED)) { lcd.clear(); lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen); if (pause_timer % 10 <= 4) { @@ -264,6 +400,7 @@ if (_L) { draw_health(lcd); } + room->draw_room_overlay(lcd); } void RoomEngine::draw_player(N5110 &lcd)