GithubHelp home page GithubHelp logo

skyformat99 / statemachine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xiahouzuoxin/statemachine

0.0 1.0 0.0 7 KB

Just a simple implemention of HSM(Hierarchical State Machine) framework

C++ 100.00%

statemachine's Introduction

Features

Just a simple implemention of HSM(Hierarchical State Machine) framework for game.

  1. C++11 implemention support Hierarchical FSM with simple user interface.
  2. All event definition (though inside class) have an identical ID
  3. Event response priority: outer hierarchical is higher

Example

Just inherit from class HSM_Region to define an FSM. Initialization of states carefully. Below is an HSM example.

#include <Windows.h>   // Just for Sleep()
#include "StateMachine.h"

class HelloHSM : public HSM_Region {
public:

	HelloHSM(const std::string& name = "Unknow", HSM_Region* parent = nullptr) 
		: HSM_Region(name, parent), state1("state1",this), state2("state2", this) {  // Specific the state initialization
		state1.addTransitionTuple(&evt1, &state2, [] {std::cout << "==>HelloHSM: Hello state2" << std::endl; return true; });
		state2.addTransitionTuple(&evt2, &state1, [] {std::cout << "==>HelloHSM: Hello state1" << std::endl; return true; });

		setInitialState(&state1);  // Each Region should set an initial state
	}

	HSM_Region state1;
	HSM_Region state2;
	HSM_Event evt1;
	HSM_Event evt2;
};

class ThankHSM : public HSM_Region {
public:
	ThankHSM(const std::string& name = "Unknow", HSM_Region* parent = nullptr) 
		: HSM_Region(name, parent), state3("state3", this), state4("state4", this) {
		state3.addTransitionTuple(&evt3, &state4, [] {std::cout << "==>ThankHSM: Hello state4" << std::endl; return true; });
		state4.addTransitionTuple(&evt4, &state3, [] {std::cout << "==>ThankHSM: Hello state3" << std::endl; return true; });

		setInitialState(&state3);
	}

	HelloHSM state3;
	HSM_Region state4;

	HSM_Event evt3;
	HSM_Event evt4;
};

int main() {
	ThankHSM hello;

	int k = 0;
	while (true)
	{
		switch (k % 7) {
		case 0:
			hello.enqueueEvent(&(hello.state3.evt1), "enqueue evt1"); break;
		case 1:
			hello.enqueueEvent(&(hello.state3.evt2), "enqueue evt2"); break;
		case 2:
			hello.enqueueEvent(&(hello.evt3), "enqueue evt3"); break; 
		case 3:
			hello.enqueueEvent(&(hello.evt4), "enqueue evt4"); break;
		case 4:
			hello.enqueueEvent(&(hello.evt3), "enqueue evt3"); break;
		case 5:
			hello.enqueueEvent(&(hello.evt4), "enqueue evt4"); break;
		case 6:
			hello.enqueueEvent(&(hello.evt4), "enqueue evt4"); break;
		default:
			break;
		}
		k++;

		hello.run();
		
		Sleep(500);
	}
}

Result:

enqueue evt1
==>HelloHSM: Hello state2
enqueue evt2
==>HelloHSM: Hello state1
enqueue evt3
==>ThankHSM: Hello state4
enqueue evt4
==>ThankHSM: Hello state3
enqueue evt3
==>ThankHSM: Hello state4
enqueue evt4
==>ThankHSM: Hello state3
enqueue evt4
enqueue evt1
==>HelloHSM: Hello state2
enqueue evt2
==>HelloHSM: Hello state1
enqueue evt3

statemachine's People

Contributors

xiahouzuoxin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.