28 m_CurrentState->OnExit(m_Owner,
typeid(
nullptr));
43 template<std::derived_from<EntityState> StateType,
typename... Args>
45 const std::type_index stateID(
typeid(StateType));
47 if (m_States.contains(stateID)) {
52 m_States[stateID] = std::make_unique<StateType>(std::forward<Args>(args)...);
60 template<std::derived_from<EntityState> StateType>
62 const std::type_index nextStateID(
typeid(StateType));
64 if (
CORI_CORE_CHECK(m_States.contains(nextStateID),
"StateMachine for Entity - {}: Attempted to change to unregistered state type '{}', register the State first!", m_Owner.GetDebugData(),
CORI_CLEAN_TYPE_NAME(StateType))) {
return; }
66 EntityState* nextStateRawPtr = m_States.at(nextStateID).get();
68 if (m_CurrentState == nextStateRawPtr) {
74 m_CurrentState->OnExit(m_Owner,
typeid(*nextStateRawPtr));
78 m_LastState = m_CurrentState;
80 m_LastState = nextStateRawPtr;
83 m_CurrentState = nextStateRawPtr;
85 m_CurrentState->OnEnter(m_Owner,
typeid(*m_LastState));
90 m_CurrentState->OnTickUpdate(m_Owner, timeStep);
99 template<std::derived_from<EntityState> StateType>
101 if (!m_CurrentState) {
105 return typeid(StateType) ==
typeid(*m_CurrentState);
112 template<std::derived_from<EntityState> StateType>
124 CORI_CORE_ASSERT(m_CurrentState,
"Trying to retrieve current state, but current state is null. Always make sure you set some initial state before using fsm.")
125 return m_CurrentState;
133 CORI_CORE_ASSERT(m_LastState,
"Trying to retrieve last state, but last state is null. Always make sure you set some initial state before using fsm.")
143 std::unordered_map<std::type_index, std::unique_ptr<EntityState>> m_States;
#define CORI_CLEAN_TYPE_NAME(tn)
#define CORI_CORE_CHECK(x,...)
#define CORI_CORE_ASSERT(x,...)
#define CORI_CORE_TRACE_TAGGED(...)
#define CORI_CORE_WARN_TAGGED(...)
void SetStateIfNotInState()
Sets an Entity to the state only if it is not currently in that state.
StateMachine & operator=(StateMachine &&)=delete
bool IsInState() const
Cheks if an Entity is in a specific state.
void SetState()
Changes the current state to the specified one.
EntityState * GetLastState() const
Gets the pointer to the last state. Can be used to get the type_info of the last state.
EntityState * GetCurrentState() const
Gets the pointer to the current state. Can be used to get the type_info of the current state.
StateMachine(const StateMachine &)=delete
void OnTickUpdate(const float timeStep)
StateMachine & operator=(const StateMachine &)=delete
void Register(Args &&... args)
Registers an EntityState with a StateMachine, you need to register a state before using it.
StateMachine(StateMachine &&)=delete
An abstract class designed to be used with StateMachine component.
Entities are the essential part of WorldSystem.
Components designed to be used with entities.
Components that are used with the WorldSystem (ECS).
Anything connected to WorldSystem (ECS) is in this namespace.