Skip to content

Commit

Permalink
Add FSM
Browse files Browse the repository at this point in the history
  • Loading branch information
toretore committed Oct 10, 2008
1 parent eebf5ed commit 6806c5a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions javascript/state.js
@@ -0,0 +1,23 @@
var StateMachine = function(initial){
this.current = this.initial = initial;
this.scope = this;
};

(function(p){

p.states = {};
p.events = [];

p.changeState = function(stateName){
var next = this.states[stateName];
if (!next) return false;
var current = this.states[this.current];
current.onExit && current.onExit.call(this.scope, next);
next.onEnter && next.onEnter.call(this.scope, current);
this.current = stateName;
current.onExited && current.onExited.call(this.scope, next);
next.onEntered && next.onEntered.call(this.scope, current);
return this.current;
};

})(StateMachine.prototype);

0 comments on commit 6806c5a

Please sign in to comment.