ICT290 / src / scene / mainMenu / menuState.cpp
menuState.cpp
Raw
#include "menuState.h"
#include "../ShaysWorldState.h"
#include "../theArchanist/theArchanist.h"
#include "exitScreen.h"
#include "gui/mainMenu.h"
// Static variables
std::shared_ptr<State> menuState::m_thisState{nullptr};
gameMap menuState::m_map{};
float menuState::m_timer{5};
MenuSounds menuState::m_sounds{};
ArchSettings menuState::m_archSettings{};

std::shared_ptr<State> menuState::init(Resources& engineResources) {
    m_thisState = std::make_shared<State>(draw,
                                          handleEvents,
                                          destroy,
                                          pause,
                                          update,
                                          engineResources);

    // init lights here

    setCamera();

    std::string modelsFile{"../res/preload/roomModels.txt"};
    int rooms{10}, mapSeed{0};
    m_map = gameMap(rooms, m_thisState->resources, modelsFile, mapSeed);

    m_thisState->skybox.setModel(
        m_thisState->resources.modelManager.getModel("as2/spaceSkybox.obj"));

    return m_thisState;
}

void menuState::destroy() {
    pause(true);
}

void menuState::pause(bool pause) {
    if (pause) {
        Light::disableLighting();
        m_thisState->audioManager.stopAllSounds();
        MENU_GUI::resetPauseState();
    } else {
        glClearColor(0.1f, 0.1f, 0.1f, 1.f);

        Light::enableLighting();

        // enable specific lights here
        m_thisState->lights->setPosition(1, 1, 0);
        m_thisState->lights[0].enable();

        m_thisState->camera.sensitivity = m_thisState->resources.settings
                                              .mouseSens;
        m_thisState->audioManager.setMasterVolume(
            m_thisState->resources.settings.volume);
        m_thisState->audioManager.setMute(m_thisState->resources.settings.mute);

        setCamera();

        m_thisState->audioManager.playSound(m_sounds.music);
    }
}

void menuState::handleEvents(SDL_Event& event) {
    while (SDL_PollEvent(&event)) {
        ImGui_ImplSDL2_ProcessEvent(&event);
    }
}

void menuState::update(float deltaTime) {
    m_timer -= deltaTime;
    if (m_timer <= 0) {
        static int mapSeed{-99999};
        std::string modelsFile{"../res/preload/roomModels.txt"};
        int rooms{10};
        m_map = gameMap(rooms,
                        m_thisState->resources,
                        modelsFile,
                        mapSeed += 113);
        m_timer = 5;
    }
    if (MENU_GUI::click.occurred) {
        m_thisState->audioManager.playSound(m_sounds.click);
        MENU_GUI::click.occurred = false;
    }
    if (MENU_GUI::pauseState.exit) {
        m_thisState->newState = exitScreen::init(m_thisState->resources);
        m_thisState->stop = true;
    }

    if (MENU_GUI::game.archanist) {
        m_thisState->newState = theArchanist::init(m_thisState->resources,
                                                   m_archSettings.seed,
                                                   m_archSettings.levels,
                                                   m_archSettings.difficulty);
    } else if (MENU_GUI::game.shaysWorld) {
        m_thisState->newState = ShaysWorldState::init(m_thisState->resources);
    }
}

void menuState::draw() {
    Light::disableLighting();
    m_thisState->skybox.draw(m_thisState->camera.direction,
                             m_thisState->camera.up);
    Light::enableLighting();
    m_thisState->lights[0].enable();
    m_map.draw();
    MENU_GUI::mainMenu(m_thisState->resources,
                       m_archSettings.seed,
                       m_archSettings.levels,
                       m_archSettings.difficulty);
}

void menuState::setCamera() {
    // Set camera perspective
    m_thisState->camera.setPerspective(45.f, (float)1920 / 1080, 0.1f, 10000.f);

    m_thisState->camera.position = glm::vec3{0, 10, 0};

    m_thisState->camera.yaw = 135;
    m_thisState->camera.pitch = 10;

    m_thisState->camera.lockMouse(false);

    m_thisState->camera.update(0.01f);
}