Cocos2d-x Game Development Limited state Machine (FSM) (ii) 1 state mode
State is the base class for States. It is actually an interface, it has 3 implementation classes, each corresponding to each state. The monkey class only needs to maintain the State class, and the specific behavior is done through 3 implementation classes. This is polymorphism. The following is the complete code:
2 State base class
state.h//#ifndef state_h_#define state_h_class monkey;struct state{ virtual void execute (Monkey *mk) = 0;}; #endif//State_h_
3 Implementing Class 3.1 Stopstate
stopstate.h//#ifndef stop_state_h_#define stop_state_h_#include "State.h" #include "Monkey.h" Class Stopstate : Public state{public: virtual void execute (Monkey *mk);}; #endif//Stop_state_h_
stopstate.cpp//#include "StopState.h" #include "WalkState.h" void Stopstate::execute (Monkey *mk) { if (mk-> Isstoptimeout ()) { mk->changestate (new Walkstate ()); Mk->walk (); }}
3.2 walkstate
walkstate.h//#ifndef walk_state_h_#define walk_state_h_#include "State.h" #include "Monkey.h" Class Walkstate : Public state{public: virtual void execute (Monkey *mk);}; #endif//Walk_state_h_
walkstate.cpp//#include "WalkState.h" #include "TurnState.h" #include "StopState.h" void Walkstate::execute (Monkey * MK) { mk->walk (); if (Mk->iswalkoutborder ()) { mk->changestate (new Turnstate ()); Mk->turn (); } else if (Mk->iswalktimeout ()) { mk->changestate (new Stopstate ()); Mk->stop (); }}
3.3 Turnstate
turnstate.h//#ifndef turn_state_h_#define turn_state_h_#include "State.h" #include "Monkey.h" Class Turnstate : Public state{public: virtual void execute (Monkey *mk);}; #endif//Turn_state_h_
turnstate.cpp//#include "TurnState.h" #include "WalkState.h" void Turnstate::execute (Monkey *mk) { mk-> Changestate ((state*) New Walkstate ()); Mk->walk ();}
4 Monkey
#ifndef monkey_h_#define monkey_h_#include <time.h> #include "cocos2d.h" USING_NS_CC; #include "State.h" #define Max_stop_time 10#define max_walk_time 20#define max_walk_dist 100class monkey:public Node{public:monkey () { Log ("Monkey ()"); } create_func (Monkey); virtual BOOL init () {_curpos = 0; _step = 1; _curstate = 0; This->scheduleupdate (); return true; } void Changestate (state * newstate) {state * oldstate = _curstate; _curstate = newstate; if (oldstate) {delete oldstate; } _curtime = time (0); } void Stop () {Cocos2d::log ("Stop ()"); } void Walk () {_curpos + = _step; Cocos2d::log ("Walk (): pos=%d", _curpos); } void Turn () {_step *=-1; Cocos2d::log ("Turn (): step=%d", _step); } void Update (float dt) {if (_curstate) {_curstate->execute (this); }}private:state * _curstate; time_t _curtime; int _curpos; int _step;public:bool isstoptimeout () {return (time (0)-_curtime > Max_stop_time); } bool Iswalktimeout () {return (time (0)-_curtime > Max_walk_time); } bool Iswalkoutborder () {return (_curpos > Max_walk_dist | | _curpos <-max_walk_dist); }}; #endif//Monkey_h_
5 Summary
When we need to add a new state, do not need to modify the long conditional judgment statement, only need to construct a new state class, modify its preamble and post-order state class can be. The unique behavior of any state is independent and does not mix in the code of other States. The long conditional statement that was originally made to determine the state transfer logic is missing, but is distributed between the sub-classes of States. On the other hand, from the point of view of design, the original identification of the current state is an internal variable within the FSM, and the transition between state and state is only represented by the assignment of its own variable, and if the own variable is derived as an array of variables, it is prone to inconsistent internal state of the FSM, The introduction of state can make such a situation quite simple. For the new version of FSM, the transition between state and state is also an atomic operation and does not need to take into account the assignment of multiple variables.
Since all state machines have the same rules and paradigms, the state machine should be independent of the specific object, and it is perfectly possible to distill a purely state machine class to manage the state of any object. This task is done in the next section.
Cocos2d-x Game Development Limited state Machine (FSM) (ii)