statemachine.cpp

00001 #include "statemachine.h"
00002 #include "state.h"
00003 #include "debug.h"
00004 
00005 using namespace irr;
00006 using namespace core;
00007 
00008 extern Debug* dbg;
00009 
00010 StateMachine::StateMachine(IrrlichtDevice* d)
00011 {
00012     //ctor
00013     dbg->log("create StateMachine");
00014     device = d;
00015     currentState = NULL;
00016 }
00017 
00018 StateMachine::~StateMachine()
00019 {
00020     //dtor
00021 }
00022 
00023 void StateMachine::addState(State* s)
00024 {
00025     states.push_back(s);
00026 }
00027 
00028 void StateMachine::pushEvent(c8* event)
00029 {
00030     events.push_back(stringc(event));
00031 }
00032 
00033 stringc StateMachine::popEvent()
00034 {
00035     u32 s = events.size();
00036     if (s <= 0)
00037     {
00038         return stringc("");
00039     }
00040 
00041     stringc event = events[s-1];
00042     events.erase(s-1);
00043     return event;
00044 }
00045 
00046 void StateMachine::update(u32 deltaTime)
00047 {
00048     if (! events.empty())
00049     {
00050         stringc tmp = popEvent();
00051 
00052         // transition to other state?
00053         for (u32 i=0; i<states.size(); i++)
00054         {
00055             State* s = states[i];
00056             if (tmp.equals_ignore_case(s->getName()))
00057             {
00058                 // transition found
00059                 // exit this state
00060                 currentState->exit();
00061                 // change state
00062                 currentState = s;
00063                 // entry next state
00064                 device->setEventReceiver(currentState);
00065                 currentState->entry();
00066                 currentState->render(deltaTime);
00067                 return;
00068             }
00069         }
00070     }
00071 
00072         currentState->render(deltaTime);
00073 }
00074 
00075 State* StateMachine::getCurrentState()
00076 {
00077     return currentState;
00078 }
00079 
00080 void StateMachine::startWith(State* s)
00081 {
00082     dbg->log("start StateMachine with State", s->getName());
00083     currentState = s;
00084     device->setEventReceiver(currentState);
00085     currentState->entry();
00086 }
00087 

Generated on Sun Dec 2 17:09:58 2007 for Swarm by  doxygen 1.4.6-NO