GithubHelp home page GithubHelp logo

fsm.js's Introduction

#fsm.js

this project copy from a unity finite state machine(FSM),and i use it in html5 game engine,such like cocoscreator;

How to use this:

  1. require this project as sm, create a new hierarchicalStateMachine to manager the states:
var sm = require('stateMachine');
var hsm = new sm.hierarchicalStateMachine(this,true);
  1. write your own state class extend from sm.state,override the state function to do what you want to do...:
function state1(){
  this.onUpdate=function(dt){
  //to do list...
  }
}
state1.prototype = new sm.state(this,this.hsm,'state1');

function state2(){
 this.onUpdate=function(dt){
  //to do list...
  }
}
state2.prototype = new sm.state(this,this.hsm,'state2');

var s1 = new state1();
var s2 = new state2();
  1. register the states into state manager,and set the default state:
this.hsm.init([s1,s2],'state1');
  1. finally,invoke the update function of state machine manager,then enjoy this:
this.hsm.onUpdate(dt);
  1. to total code like this:
var sm = require('stateMachine');

function idleState(){
  var that = this;
  var time = 0;
  this.onEnter=function(){
      time = 0;
  };
  this.onUpdate=function(dt){
      time += dt;
      if(time > 10){
          this.getHSM().changeState('gameState');
      }
  };
}

function gameState(){
  var that = this;
  var time = 0;
  this.onEnter=function(){
      time = 0;
  };
  this.onUpdate=function(dt){
      time += dt;
      if(time > 10){
          this.getHSM().changeState('idleState');
      }
  };
}

// use this for initialization
{
  hsm:null;
  //initialize the all states
  onLoad: function () {
      //open debug mode,so that can watch the log
      this.hsm = new sm.hierarchicalStateMachine(this,true);

      idleState.prototype = new sm.state(this,this.hsm,'idleState');
      var idle = new idleState();

      gameState.prototype = new sm.state(this,this.hsm,'gameState');
      var game = new gameState();

      this.hsm.init([idle,game],'idleState');
  },

  // what ever engine to invoke this function
  update: function (dt) {
     //must be
      this.hsm.onUpdate(dt);
  }
}

and,there had two demo about single state and composite state in cocos creator in this project!

fsm.js's People

Contributors

wingcd avatar

Stargazers

 avatar

Watchers

 avatar  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.