Skip to content

Instantly share code, notes, and snippets.

@Keita-N
Created June 4, 2015 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keita-N/8bab71f40369b2be9f1d to your computer and use it in GitHub Desktop.
Save Keita-N/8bab71f40369b2be9f1d to your computer and use it in GitHub Desktop.
State Machine Implimentation
// state machine
var stm = {
'stateA': {
events: {
'ev1': function(){
this.state = 'stateB';
// a certain action
},
'ev2': function(){
this.state = 'stateC';
// a certain action
}
}
},
'stateB': {
events: {
'ev3': function(){
this.state = 'stateC';
// a certain action
}
}
},
'stateC': {
enter: function(){
// a certain action
},
leave: function(){
// a certain action
},
events: {
'ev1': function(){
// a certain action
}
}
}
}
(function(player){
var state = player.state;
var noop = function() {};
(stm[state].events[player.eventType] || noop).apply(player)
if (state !== player.state) {
(stm[state].leave || noop).apply(player);
(stm[player.state].enter || noop).apply(player);
}
})(player)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment