ICT290 / src / scene / mainMenu / developerScreen.cpp
developerScreen.cpp
Raw
#include "developerScreen.h"
#include <random>
#include "menuState.h"
// Static variables
std::shared_ptr<State> devScreen::m_thisState{nullptr};
float devScreen::m_timer{10};
devSounds devScreen::m_sounds{};
std::vector<glm::vec3> devScreen::m_snowDirections;

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

    pause(false);

    setCamera();

    std::string name{"banner"};
    glm::vec3 location{0, 0, -1};
    glm::vec3 rotation{0, 90, 0};
    glm::vec3 scale{1};
    auto model = m_thisState->resources.modelManager.getModel(
        "as2/devScreen.obj");

    Object devScreen{name, model, location, rotation, scale};

    m_thisState->objects.emplace(name, devScreen);

    createSnow();

    return m_thisState;
}

void devScreen::destroy() {
    pause(true);
    m_snowDirections.clear();
}

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

        setCamera();

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

void devScreen::handleEvents(SDL_Event& event) {
    // Skip intro
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_KEYDOWN || event.type == SDL_MOUSEBUTTONDOWN) {
            m_timer = 0;
            m_thisState->audioManager.playSound(m_sounds.click);
        }
    }
}

void devScreen::update(float deltaTime) {
    updateSnow(deltaTime);

    m_timer -= deltaTime;
    if (m_timer <= 5) {
        m_thisState->audioManager.setMasterVolume(
            m_thisState->audioManager.getMasterVolume() - 0.25f);
    }
    if (m_timer <= 0) {
        m_thisState->newState = menuState::init(m_thisState->resources);
        m_thisState->stop = true;
    }
}

void devScreen::draw() {
    for (auto& obj : m_thisState->objects) {
        obj.second.draw();
    }
}

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

    m_thisState->camera.position = glm::vec3{-0.24f, 1, 0.77};

    m_thisState->camera.lockMouse(false);

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

void devScreen::createSnow() {
    const std::size_t snowCount{200};

    m_snowDirections.emplace_back(-1, -1, 0);
    m_snowDirections.emplace_back(-1, 0, 0);
    m_snowDirections.emplace_back(0, -1, 0);
    m_snowDirections.emplace_back(-0.85f, -0.25f, 0);
    m_snowDirections.emplace_back(-0.35f, -0.75f, 0);
    m_snowDirections.emplace_back(-0.65f, -0.45f, 0);
    m_snowDirections.emplace_back(-0.95f, -0.5f, 0);
    m_snowDirections.emplace_back(-0.25f, -0.85f, 0);
    m_snowDirections.emplace_back(-0.05f, -0.95f, 0);

    std::string name{"Snow"};
    glm::vec3 location{0, 2, -0.8f};
    glm::vec3 rotation{0};
    glm::vec3 scale{0.1f};

    std::uniform_real_distribution<> spawnDist{-2, 3};

    auto model = m_thisState->resources.modelManager.getModel("as2/snow.obj");
    for (std::size_t i = 0; i < snowCount; ++i) {
        std::random_device rd{};
        std::mt19937 gen{rd()};
        location.x = (float)spawnDist(gen);
        location.y = (float)spawnDist(gen);
        m_thisState->objects
            .emplace(name, Object{name, model, location, rotation, scale});
    }
}

void devScreen::updateSnow(float deltaTime) {
    std::uniform_real_distribution<> spawnDist{-2, 3};
    std::uniform_int_distribution<> dirDist{0,
                                            (int)m_snowDirections.size() - 1};
    std::uniform_real_distribution<> speed{10, 20};
    for (auto& snow : m_thisState->objects) {
        if (snow.first == "Snow") {
            std::random_device rd{};
            std::mt19937 gen{rd()};
            if (snow.second.location.y > -2) {
                snow.second.location += m_snowDirections[dirDist(gen)]
                                        * (float)speed(gen) * deltaTime;
                snow.second.location.z = -0.8f;
            } else {
                snow.second.location.x = (float)spawnDist(gen);
                snow.second.location.y = 2;
            }
        }
    }
}