Created
June 4, 2015 12:44
-
-
Save Keita-N/8bab71f40369b2be9f1d to your computer and use it in GitHub Desktop.
State Machine Implimentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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