Created
May 17, 2013 06:28
-
-
Save jamesflorentino/5597299 to your computer and use it in GitHub Desktop.
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
// 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