Created
April 18, 2013 03:38
-
-
Save jamesflorentino/5409906 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