Go to the documentation of this file.00001 #include "FSM.h"
00002 #include "State.h"
00003 #include "RakAssert.h"
00004
00005
00006
00007 FSM::FSM()
00008 {
00009
00010 }
00011 FSM::~FSM()
00012 {
00013 Clear();
00014 }
00015 void FSM::Clear(void)
00016 {
00017 unsigned i;
00018 if (stateHistory.Size())
00019 stateHistory[stateHistory.Size()-1]->OnLeave(this, true);
00020 for (i=0; i < stateHistory.Size(); i++)
00021 stateHistory[i]->FSMRemoveRef(this);
00022 stateHistory.Clear(false, __FILE__, __LINE__);
00023 }
00024 State *FSM::CurrentState(void) const
00025 {
00026 if (stateHistory.Size()==0)
00027 return 0;
00028 return stateHistory[stateHistory.Size()-1];
00029 }
00030 State *FSM::GetState(int index) const
00031 {
00032 RakAssert(index>=0 && index < (int) stateHistory.Size());
00033 return stateHistory[(unsigned) index];
00034 }
00035 int FSM::GetStateIndex(State *state) const
00036 {
00037 return (int) stateHistory.GetIndexOf(state);
00038 }
00039 int FSM::GetStateHistorySize(void) const
00040 {
00041 return stateHistory.Size();
00042 }
00043 void FSM::RemoveState(const int index)
00044 {
00045 RakAssert(index>=0 && index < (int) stateHistory.Size());
00046 if (index==stateHistory.Size()-1)
00047 stateHistory[index]->OnLeave(this, true);
00048 stateHistory[index]->FSMRemoveRef(this);
00049 stateHistory.RemoveAtIndex((const unsigned int)index);
00050 if (index==stateHistory.Size())
00051 stateHistory[stateHistory.Size()-1]->OnEnter(this, false);
00052 }
00053 void FSM::AddState(State *state)
00054 {
00055 if (stateHistory.Size())
00056 stateHistory[stateHistory.Size()-1]->OnLeave(this, false);
00057 state->FSMAddRef(this);
00058 state->OnEnter(this, true);
00059 stateHistory.Insert(state, __FILE__, __LINE__ );
00060 }
00061 void FSM::ReplaceState(const int index, State *state)
00062 {
00063 RakAssert(index>=0 && index < (int) stateHistory.Size());
00064 if (state!=stateHistory[index])
00065 {
00066 if (index==stateHistory.Size()-1)
00067 stateHistory[index]->OnLeave(this, true);
00068 stateHistory[index]->FSMRemoveRef(this);
00069 state->FSMAddRef(this);
00070 if (index==stateHistory.Size()-1)
00071 state->OnEnter(this, true);
00072 stateHistory[index]=state;
00073 }
00074 }
00075 void FSM::SetPriorState(const int index)
00076 {
00077 RakAssert(index>=0 && index < (int) stateHistory.Size());
00078 stateHistory[stateHistory.Size()-1]->OnLeave(this, true);
00079 for (unsigned i=stateHistory.Size()-1; i > (unsigned) index; --i)
00080 {
00081 stateHistory[i]->FSMRemoveRef(this);
00082 stateHistory.RemoveFromEnd();
00083 }
00084 stateHistory[index]->OnEnter(this, false);
00085 }