Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save caracal7/e30c206724674bd11fa8b735df4efd5d to your computer and use it in GitHub Desktop.
Save caracal7/e30c206724674bd11fa8b735df4efd5d 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