Skip to content

Instantly share code, notes, and snippets.

@robdodson
Created June 3, 2012 01:40
Show Gist options
  • Save robdodson/2860884 to your computer and use it in GitHub Desktop.
Save robdodson/2860884 to your computer and use it in GitHub Desktop.
states in progress
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