Skip to content

Instantly share code, notes, and snippets.

@ThaisRobba
Last active January 26, 2018 21:50
Show Gist options
  • Save ThaisRobba/3210b2840a540debad47 to your computer and use it in GitHub Desktop.
Save ThaisRobba/3210b2840a540debad47 to your computer and use it in GitHub Desktop.
Tiny component & entities module
var componentsList = {};
function Component(name, func) {
componentsList[name] = func;
}
//Components are created like this:
Component('flying', function(o) {
o.flying = true;
});
var entitiesIDs = [];
function addComponent() {
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === typeof []) {
for (var j = 0; j < arguments[i].length; j++) {
componentsList[arguments[i][j]](this);
}
} else {
componentsList[arguments[i]](this);
}
}
}
function Entity() {
var obj = {
addComponent: addComponent,
id: entitiesIDs.length
};
entitiesIDs.push(obj.id);
addComponent.apply(obj, arguments);
return obj;
}
//Entities are created like this:
var user = Entity('flying');
//Bundles are made with arrays:
var orcs = ['health', 'collision', 'enemy', 'ai', 'armed', 'green'];
var enemy1 = Entity(orcs);
//You can combine multiple bundles and strings:
var enemy2 = Entity(orcs, 'rage', undead, 'axe');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment