1
0
forked from 0ad/0ad

Let CFsm actions override the default state transition

This was SVN commit r10432.
This commit is contained in:
Ykkrosh 2011-10-27 14:10:55 +00:00
parent 28d7f52578
commit a2bba82b9d
2 changed files with 19 additions and 7 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2011 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -169,8 +169,9 @@ bool CFsmTransition::RunActions( void ) const
CFsm::CFsm( void )
{
m_Done = false;
m_FirstState = ( unsigned int )FSM_INVALID_STATE;
m_CurrState = ( unsigned int )FSM_INVALID_STATE;
m_FirstState = FSM_INVALID_STATE;
m_CurrState = FSM_INVALID_STATE;
m_NextState = FSM_INVALID_STATE;
}
//-----------------------------------------------------------------------------
@ -222,8 +223,9 @@ void CFsm::Shutdown( void )
m_Transitions.clear();
m_Done = false;
m_FirstState = ( unsigned int )FSM_INVALID_STATE;
m_CurrState = ( unsigned int )FSM_INVALID_STATE;
m_FirstState = FSM_INVALID_STATE;
m_CurrState = FSM_INVALID_STATE;
m_NextState = FSM_INVALID_STATE;
}
//-----------------------------------------------------------------------------
@ -409,11 +411,18 @@ bool CFsm::Update( unsigned int eventType, void* pEventParam )
// Valid transition?
if ( !pTransition->ApplyConditions() ) return false;
// Save the default state transition (actions might call SetNextState
// to override this)
SetNextState( pTransition->GetNextState() );
// Run transition actions
if ( !pTransition->RunActions() ) return false;
// Switch state
SetCurrState( pTransition->GetNextState() );
SetCurrState( GetNextState() );
// Reset the next state since it's no longer valid
SetNextState( FSM_INVALID_STATE );
return true;
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2011 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -152,6 +152,8 @@ public:
void SetFirstState ( unsigned int firstState );
void SetCurrState ( unsigned int state );
unsigned int GetCurrState ( void ) const { return m_CurrState; }
void SetNextState ( unsigned int nextState ) { m_NextState = nextState; }
unsigned int GetNextState ( void ) const { return m_NextState; }
const StateSet& GetStates ( void ) const { return m_States; }
const EventMap& GetEvents ( void ) const { return m_Events; }
const TransitionList& GetTransitions ( void ) const { return m_Transitions; }
@ -166,6 +168,7 @@ private:
bool m_Done; // FSM work is done
unsigned int m_FirstState; // Initial state
unsigned int m_CurrState; // Current state
unsigned int m_NextState; // Next state
StateSet m_States; // List of states
EventMap m_Events; // List of events
TransitionList m_Transitions; // State transitions