ecos v0.2.1
#Entity Component System
Simple system to create and utilize objects in the right way.
##Create objects
var factory = require('ecos').factory;
var entityFactory = factory.create({
props: ['x', 'y'],
name: 'my_entity'
});
var entity = entityFactory.create();
console.log(entity);
{
x: undefined,
y: undefined,
id: 0,
type: 'my_entity'
}##Get objects
var objects = require('ecos').objects;
var entity = objects.get(0);
console.log(entity);
{
x: undefined,
y: undefined,
id: 0,
type: 'my_entity'
}##Ideas behind this library
- Each entity is stored in unified container
ecos.objectsand accessible by itsid - Entity should not have direct link to other entities. Parent entity store identificators of child entities instead
- To get child entity just use
ecos.objects.get(childId); - Since entities are stored only in one container then no memory leaks will occur when objects will be deleted
- Different entities can share same
methodsandextendersand inherit same behaviour without prototype inheritance
##Instalation
As npm package:
npm install ecos --saveAs bower package:
bower install ecos --save##Documentation
###Factory
To create an entity first you need to create a factory that will produce entities:
var factory = require('ecos').factory;
var entityFactory = factory.create(options);####Options:
- name - factory identificator, required string
- props - list of entity properties that will be initialized with
undefined, string - methods - list of entity methods, all methods must be registered with
factory.registerMethod, string - extend - list of extenders that will be applied to entity, all extenders must be registered with
factory.registerExtender, string - presets - list of presets that will be applied to entity, all presets must be registered with
factory.registerPreset. string - default - list of entity properties with predefined values, will overwrite
propsoptions, { propName: propValue } - custom - list of entity properties with predefined values, will overwrite
propsanddefaultoptions, is entity with this set will be accessible throughentityFactory.customEntity()call, { customEntityName: { propName: propValue } }
####Create basic entity: Will create entity with fields.
var factory = require('ecos').factory;
var entityFactory = factory.create({
props: [ 'x', 'y' ],
name: 'example'
});
entityFactory.create();Result:
{
type: 'example',
x: undefined,
y: undefined,
id: 0
}If you pass object with fields to entity constructor then factory will create entity with specified fields defined:
entityFactory.create({x: 100, y: 200});Result:
{
type: 'example',
x: 100,
y: 200,
id: 0
}####Create entity with method:
Use factory.registerMethod to register method that can be assigned to entity:
factory.registerMethod('console', function(text){
console.log(text);
});
var entityFactory = factory.create({
methods: ['console'],
name: 'example'
});
entityFactory.create();Result:
{
type: 'example',
console: function
id: 0
}####Create entity with GETSET extender: This extender will create field with defined getter and setter.
var extenders = require('ecos').extenders;
factory.registerExtender('value-extender', {
get: function(){
return this._value;
},
set: function(val){
this._value = val;
console.log('Value set to ' + val);
},
name: 'value',
type: extenders.GETSET
});
var entityFactory = factory.create({
extend: ['value-extender'],
name: 'example'
});
entityFactory.create();
entity.value = Math.random();Result:
Value set to 0.758898114If you wont specify get or set function in extender definition then resulting field wont have getter or setter defined.
####Create entity with FUNCTION extender: This extender will run custom function on object creation.
factory.registerExtender('function-extender', {
type: extenders.FUNCTION,
handler: function(thisObject){
console.log('Creating ' + JSON.stringify(thisObject));
}
});
var entityFactory = factory.create({
extend: ['function-extender'],
name: 'example'
});
entityFactory.create();Result:
Creating {"type":"example", "id":0}