Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/network/FSM.cpp
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
void CFsm::Setup() | void CFsm::Setup() | ||||
{ | { | ||||
// Does nothing by default | // Does nothing by default | ||||
} | } | ||||
void CFsm::Shutdown() | void CFsm::Shutdown() | ||||
{ | { | ||||
m_States.clear(); | |||||
m_Transitions.clear(); | m_Transitions.clear(); | ||||
m_Done = false; | m_Done = false; | ||||
m_FirstState = FSM_INVALID_STATE; | m_FirstState = FSM_INVALID_STATE; | ||||
m_CurrState = FSM_INVALID_STATE; | m_CurrState = FSM_INVALID_STATE; | ||||
m_NextState = FSM_INVALID_STATE; | m_NextState = FSM_INVALID_STATE; | ||||
} | } | ||||
void CFsm::AddState(unsigned int state) | |||||
{ | |||||
m_States.insert(state); | |||||
} | |||||
void CFsm::AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState, | void CFsm::AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState, | ||||
Action* pAction /* = nullptr */, void* pContext /* = nullptr*/) | Action* pAction /* = nullptr */, void* pContext /* = nullptr*/) | ||||
{ | { | ||||
// Make sure we store the current state | |||||
AddState(state); | |||||
// Make sure we store the next state | |||||
AddState(nextState); | |||||
m_Transitions.insert({TransitionKey{state, eventType}, Transition{{pAction, pContext}, nextState}}); | m_Transitions.insert({TransitionKey{state, eventType}, Transition{{pAction, pContext}, nextState}}); | ||||
} | } | ||||
void CFsm::SetFirstState(unsigned int firstState) | void CFsm::SetFirstState(unsigned int firstState) | ||||
{ | { | ||||
m_FirstState = firstState; | m_FirstState = firstState; | ||||
} | } | ||||
void CFsm::SetCurrState(unsigned int state) | void CFsm::SetCurrState(unsigned int state) | ||||
{ | { | ||||
m_CurrState = state; | m_CurrState = state; | ||||
} | } | ||||
bool CFsm::IsFirstTime() const | bool CFsm::IsFirstTime() const | ||||
{ | { | ||||
return (m_CurrState == FSM_INVALID_STATE); | return (m_CurrState == FSM_INVALID_STATE); | ||||
} | } | ||||
bool CFsm::Update(unsigned int eventType, void* pEventParam) | bool CFsm::Update(unsigned int eventType, void* pEventParam) | ||||
{ | { | ||||
if (IsFirstTime()) | if (IsFirstTime()) | ||||
m_CurrState = m_FirstState; | m_CurrState = m_FirstState; | ||||
if (!IsValidState(m_CurrState)) | |||||
return false; | |||||
// Lookup transition | // Lookup transition | ||||
auto transitionIterator = m_Transitions.find({m_CurrState, eventType}); | auto transitionIterator = m_Transitions.find({m_CurrState, eventType}); | ||||
if (transitionIterator == m_Transitions.end()) | if (transitionIterator == m_Transitions.end()) | ||||
return false; | return false; | ||||
CFsmEvent event{eventType, pEventParam}; | CFsmEvent event{eventType, pEventParam}; | ||||
// Save the default state transition (actions might call SetNextState | // Save the default state transition (actions might call SetNextState | ||||
Show All 11 Lines | bool CFsm::Update(unsigned int eventType, void* pEventParam) | ||||
return true; | return true; | ||||
} | } | ||||
bool CFsm::IsDone() const | bool CFsm::IsDone() const | ||||
{ | { | ||||
// By default the internal flag m_Done is tested | // By default the internal flag m_Done is tested | ||||
return m_Done; | return m_Done; | ||||
} | } | ||||
bool CFsm::IsValidState(unsigned int state) const | |||||
{ | |||||
StateSet::const_iterator it = m_States.find(state); | |||||
if (it == m_States.end()) | |||||
return false; | |||||
return true; | |||||
} |
Wildfire Games · Phabricator