Skip to content

Instantly share code, notes, and snippets.

@nsbingham
Created June 5, 2012 19:24
Show Gist options
  • Save nsbingham/2877164 to your computer and use it in GitHub Desktop.
Save nsbingham/2877164 to your computer and use it in GitHub Desktop.
Multiplayer plugin code injection
// Code injected into the Entity class
ig.Entity.inject({
// Abstracting the controls
controls: {
up: false,
down: false,
left: false,
right: false
},
setControl: function(id) {
this.controls[id] = true;
this.publishAction(id);
},
unsetControl: function(id) {
this.controls[id] = false;
this.publishAction(id);
},
// Publishes a Entity's action
publishAction: function(id) {
ig.game.pubsub.publish(ig.game.topics.ACTION,
{
remoteName:this.remoteName,
action: id,
result: this.controls[id],
pos: this.pos
});
}
});
// Actions can be triggered in the Entity classes using the following
if(ig.input.pressed(this.inputs.left)) {
this.setControl('left');
}
// They can then be checked using
if(this.controls.left) {
// Move left
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment