State machine-based game framework

Source: Internet
Author: User
Tags fsm
A finite state machine is a finite state machine,
And can be changed from one State to another according to the corresponding operation,
The agent can only be in one state at a time.
English: Finite State Machine
Abbreviation: FSM


2. Simplest state machine: If-Else
In fact, if-else is a state machine with two statuses: true and false.


Three pseudo state machines when the two situations cannot meet our needs, we can use if-else if-...-Else,
However, for convenience, we can use switch-case instead.


First, define a group of different states. We can use macro definition or enumeration:
Enum estate {estate_min, // uninitialized status estate_one, estate_two,... estate_max, // used for exception detection };



Define an object to save the current state:
EState curState = eState_Min;




Now, you can check the current status in the switch and perform corresponding processing.
Void Update () {Switch (curstate) {Case estate_one: onstateone (); break; Case estate_min: onstatetwo (); break ;... case estate_max: break; default: // error }}




And then perform different processing on each status,
void onStateOne(){}void onStateTwo(){}...



For example, if a strange guy is in a temporary state, he or she can move it back and forth and check whether any hero unit is near,
When it is detected that a hero is approaching, it is converted to status 2, attacking the hero.
All, we also need a function to convert the state

void converToState(EState dstState){curState = dstState;}




The above state machine is implemented in C language. When a new State appears, you need to modify the corresponding code,
This game framework structure is used with General games. When the game is large and the logic is complex, the switch structure becomes very complex.




Under the three real state machines, a state machine is implemented using object-oriented polymorphism.
First, we need to define a state base class and a pure virtual function.
This pure virtual function is used to process the logic of the current state.
class BaseState{public:virtual ~BaseState() = 0;virtual void execute() = 0;BaseState::~BaseState(){}};



Then, we define a state machine class to control the state transition.


class FSM{public:void converToState(BaseState *state){if state thendelete m_curState;m_curState = NULL;endm_curState = state;}void update(){m_curState->onState()}protected: BaseState* m_curState;};




In this case, if we need to add a new state, we only need to add a new class and inherit from basestate.


For example, define a monster
class Monster{public:void update(){m_SFM->update()}protected:FSM* m_SFM;};



Define two status classes, execute the functions in the current status, and check whether the status changes,
If the status changes, switch to another status.
class StateMove:BaseState{public:~BaseState(){}void execute(){// move,check}};class StateFight:BaseState{public:~BaseState(){}void execute(){// fight,check}};



In this way, a state machine-based game framework is okay.

State machine-based game framework

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.