Cocos2d-x Game Development Limited state Machine (FSM) (i)

Source: Internet
Author: User
Tags fsm

Cocos2d-x Game Development Limited state Machine (FSM) (i)

Reference:
http://blog.csdn.net/mgphuang/article/details/5845252
"Cocos2d-x Game Development Tour" (Jong Dirong)

Basically all the software is a finite state machine (Finite-state MACHINE,FSM). It is a forward graph, consisting of a set of nodes and a corresponding set of transfer functions. In layman's terms, it is a model of an event-driven system that consists of a finite number of states, a number of inputs and transitions between States and states. At some point, one or a group of States is the current state of the FSM, the FSM receives the input events and transforms the current state into a new state according to the transformation rules. It is the combination of these three elements that makes the FSM have its own behavioral characteristics. In game development, FSM is used to implement the decision-making process of artificial intelligence and control the behavior of the game object.

1 The simplest state machine


Perhaps the above explanation is still somewhat abstract, the vast majority of articles will use "door" or "lock" example to illustrate what is the state machine, I would like to give a fresh example: there is a "monkey" NPC, it will be in the designated area to walk, and sometimes stay, when walking to the boundaries of the region, will turn itself (Fig. 1). We list the transformation rule function of the state machine (Figure 2) and write out the C + + implementation of this simple state machine according to the rules listed:


#ifndef monkey_h_#define monkey_h_#include <time.h> #include "cocos2d.h" USING_NS_CC; #define Max_stop_time 10# Define Max_walk_time 20#define max_walk_dist 100enum monkeystate{ststop, Stwalk, Stturn};class monkey:pub    Lic Node{public:monkey () {log ("Monkey ()");     } create_func (Monkey);        virtual BOOL init () {_curpos = 0;        _step = 1;        Changestate (Ststop);        This->scheduleupdate ();    return true;        } void Changestate (Monkeystate newstate) {_curstate = NewState;            _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) {switch (_curstate) {case ststop:if (Isstoptimeout ()) {     Changestate (Stwalk);           Walk ();        } break;            Case Stwalk:walk ();                if (Iswalkoutborder ()) {changestate (Stturn);            Turn ();                } else if (Iswalktimeout ()) {changestate (ststop);            Stop ();        } break;            Case Stturn:changestate (Stwalk);            Walk ();        Break    }}private:monkeystate _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_


2 Taste of Bad code


Obviously, if you continue to write the above code complete, it will work very well. But I seem to have smelled the legendary "bad Code", with long conditional judgments that grow longer as states grow. Each additional state needs to be carefully searched and modified in a long conditional judgment statement. When such conditional statements grow to require more personal collaboration, that can lead to serious maintenance and debugging problems. In addition, for a compiled language, an FSM with N states to find a correct state, on average, needs to be judged N/2 times.

In the state machine above, in fact, let this object in different states to show a different behavior characteristics, the different behavior characteristics in addition to a similar formal interface, there is basically no connection. It is natural for us to think of State mode, to maintain state with state mode, and to separate the object from the maintenance state.


Cocos2d-x Game Development Limited state Machine (FSM) (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.