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-05-05
Revision:
30:ec915d24d3e9
Parent:
29:6b8411bb040a
Child:
31:ab24d028ddfd

File content as of revision 30:ec915d24d3e9:

#include "RoomEngine.h"

RoomEngine::RoomEngine(float global_contrast)
{
    _room_x = 5;
    _room_y = 5;
    _global_contrast = global_contrast;
}

RoomEngine::~RoomEngine()
{
    delete player;
}

void RoomEngine::load(Player &current_player, Room *current_room)
{
    player = &current_player;
    room = current_room;
    set_input(0,0,0,0,0,0,0,0);
    update_player_position(check_player_room_position());
}

void RoomEngine::entrance_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, -((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);
        if (0.75 - (i * 0.005) > _global_contrast) {
            lcd.setContrast(0.75 - (i * 0.005));
        } else {
            lcd.setContrast(_global_contrast);
        }
    }
}

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(_global_contrast + (i * 0.005));
    }
    lcd.setContrast(0.75);
}

void RoomEngine::update_current_room()
{
    switch(check_player_room_position()) {
        case 0 :
            _room_y++;
            break;
        case 1 :
            _room_x++;
            break;
        case 2 :
            _room_y--;
            break;
        case 3 :
            _room_x--;
            break;
        default :
        break;
    }
}

void RoomEngine::read_input(Gamepad &gamepad)
{
    _L = gamepad.check_event(Gamepad::L_PRESSED);
    _R = gamepad.check_event(Gamepad::R_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;
}

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;
    }
}

// Public Accessors

int RoomEngine::get_room_x()
{
    return _room_x;
}
int RoomEngine::get_room_y()
{
    return _room_y;
}

// 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
{
    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());
                break; // only let 1 enemy damage player at a time
            }
        }
    }
}

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->update_bullets(room->get_current_map_2d(), room->get_doorways())) {
            } 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_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) {
                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;
                }
            }
            
        }
    }
}

void RoomEngine::move()
{
    move_player();
    move_enemies();
}

void RoomEngine::move_player()
{
    player->move(mapped_coord.x, mapped_coord.y, room->get_current_map_2d(), room->get_doorways());
}

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(), room->get_doorways());
        }
    }
    // Entity Collision Repulsion
    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (room->valid_enemies[i] && !room->enemies[i]->is_damaged_by_collision()) {
            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::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(gamepad.check_event(Gamepad::BACK_PRESSED)) {
        lcd.clear();
        lcd.refresh();
        wait_ms(1000/40);
    };
}

void RoomEngine::pause_detection(N5110 &lcd, Gamepad &gamepad)
{
    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(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(!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(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]);
    player->draw(lcd);
    draw_enemies(lcd);
    room->draw_room_overlay(lcd);
    if (_L) {
        draw_health(lcd);
    }
}

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_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());
    }
}