Skip to content

Instantly share code, notes, and snippets.

@jamesflorentino
Created April 18, 2013 03:38
Show Gist options
  • Save jamesflorentino/5409906 to your computer and use it in GitHub Desktop.
Save jamesflorentino/5409906 to your computer and use it in GitHub Desktop.
// Basic entity-component closure
function Component(api) {
return function(entity) {
for(var key in api) {
if (api.hasOwnProperty(key)) {
entity[key] = api[key];
}
}
}
}
var MeleeComponent = Component({
attack: function() {
console.log('melee');
}
});
var FireComponent = Component({
fire: function() {
console.log('fireball');
}
})
var entity = {};
MeleeComponent(entity); // entity now has entity.attack()
FireComponent(entity); // entity now has entity.fire()
// test
entity.attack();
entity.fire();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment