BubbleBobbleRemake / DuvelEngine / IStateController.h
IStateController.h
Raw
#pragma once
#include "IState.h"
#include <memory>
#include <map>
#include <iostream>

template <typename T>
class IStateController
{
public:
    virtual ~IStateController() = default;

    void ChangeState(unsigned stateId)
    {
        auto it = m_pStates.find(stateId);
        if (it == m_pStates.end())
        {
            // Handle error: State with the provided ID doesn't exist.
            std::cout << "Error: State with the provided ID doesn't exist." << std::endl;
            return;
        }

        if (m_pCurrentState)
        {
            m_pCurrentState->Exit(static_cast<T*>(this));
        }

        m_pCurrentState = it->second;
        m_pCurrentState->Enter(static_cast<T*>(this));
    }

    void UpdateState(float deltaTime)
    {
        if (m_pCurrentState)
        {
            m_pCurrentState->Update(static_cast<T*>(this), deltaTime);
        }
    }

protected:
    std::map<unsigned, std::shared_ptr<IState<T>>> m_pStates;
    std::shared_ptr<IState<T>> m_pCurrentState;
};