Created
June 3, 2012 01:40
-
-
Save robdodson/2860884 to your computer and use it in GitHub Desktop.
states in progress
This file contains 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
var player = { | |
state: undefined, | |
states: { | |
base: { | |
initialize:function(target) { | |
this.target = target; | |
}, | |
enter: function() { | |
// Implement me in your state objects | |
}, | |
execute: function() { | |
// Implement me in your state objects | |
}, | |
play: function() { | |
this.target.changeState(this.target.states.playing); | |
}, | |
stop: function() { | |
this.target.changeState(this.target.states.stopping); | |
}, | |
pause: function() { | |
this.target.changeState(this.target.states.pausing); | |
}, | |
exit: function() { | |
// Implement me in your state objects | |
} | |
} | |
}, | |
initialize: function() { | |
this.states.playing = $.extend(true, {}, this.states.base, { | |
enter: function() { | |
console.log('foobar!'); | |
} | |
}); | |
this.states.playing.initialize(this); | |
//this.states.stopping.initialize(this); | |
//this.states.pausing.initialize(this); | |
//this.state = this.states.stopping; | |
}, | |
play: function() { | |
this.state.play(); | |
}, | |
stop: function() { | |
this.state.stop(); | |
}, | |
pause: function() { | |
this.state.pause(); | |
}, | |
changeState: function(state) { | |
if (this.state !== state) { | |
this.state.exit(); | |
this.state = state; | |
this.state.enter(); | |
this.state.execute(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment