2016-12-15 14:53:39 +01:00
|
|
|
/**
|
|
|
|
* GameLib.EntityManager
|
2016-12-16 16:03:48 +01:00
|
|
|
* @param parentObject
|
|
|
|
* @param apiEntityManager GameLib.API.EntityManager
|
2016-12-15 14:53:39 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
2016-12-16 16:03:48 +01:00
|
|
|
GameLib.EntityManager = function(
|
|
|
|
parentObject,
|
|
|
|
apiEntityManager
|
|
|
|
) {
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
|
|
|
|
parentObject = null;
|
|
|
|
}
|
|
|
|
this.parentObject = parentObject;
|
2016-12-15 14:53:39 +01:00
|
|
|
|
|
|
|
GameLib.API.EntityManager.call(
|
|
|
|
this,
|
2016-12-16 16:03:48 +01:00
|
|
|
apiEntityManager.entities
|
2016-12-15 14:53:39 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.instance = this.createInstance();
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.EntityManager.prototype = Object.create(GameLib.API.EntityManager.prototype);
|
|
|
|
GameLib.EntityManager.prototype.constructor = GameLib.EntityManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an Entity Manager instance
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.createInstance = function() {
|
2016-12-16 16:03:48 +01:00
|
|
|
/**
|
|
|
|
* Fuck the current ECS bullshit on the internet - both tiny-ecs and ecsjs SUCKS ASS
|
|
|
|
*/
|
|
|
|
return null;
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an GameLib.Entity
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2016-12-16 16:03:48 +01:00
|
|
|
GameLib.EntityManager.prototype.createEntity = function(name) {
|
|
|
|
|
|
|
|
var apiEntity = new GameLib.API.Entity(null, name);
|
2016-12-15 14:53:39 +01:00
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
var entity = new GameLib.Entity(this, this, apiEntity);
|
2016-12-15 14:53:39 +01:00
|
|
|
|
|
|
|
this.entities.push(entity);
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an entity
|
|
|
|
* @param entity GameLib.D3.Entity
|
2016-12-16 16:03:48 +01:00
|
|
|
* @returns boolean true if successful
|
2016-12-15 14:53:39 +01:00
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.removeEntity = function(entity) {
|
|
|
|
|
|
|
|
var index = this.entities.indexOf(entity);
|
|
|
|
|
|
|
|
if (index == -1) {
|
|
|
|
console.log('failed to remove entity : ', entity);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.entities.splice(index, 1);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all the objects with the following components
|
|
|
|
* @param components GameLib.Component[]
|
|
|
|
*/
|
2016-12-16 16:03:48 +01:00
|
|
|
GameLib.EntityManager.prototype.query = function(components) {
|
|
|
|
|
2016-12-19 17:44:15 +01:00
|
|
|
var result = this.entities.reduce(
|
2016-12-16 16:03:48 +01:00
|
|
|
function(__queryComponents) {
|
2016-12-19 17:44:15 +01:00
|
|
|
return function(result, entity) {
|
|
|
|
|
|
|
|
var results = __queryComponents.reduce(
|
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
function(__entity) {
|
2016-12-19 17:44:15 +01:00
|
|
|
return function(componentResult, queryComponent) {
|
|
|
|
|
|
|
|
var components = __entity.components.reduce(
|
2016-12-16 16:03:48 +01:00
|
|
|
function(__queryComponent) {
|
2016-12-19 17:44:15 +01:00
|
|
|
return function(__components, entityComponent) {
|
|
|
|
|
|
|
|
if (__queryComponent == entityComponent.constructor) {
|
2016-12-16 16:03:48 +01:00
|
|
|
// arrow should point towards a window or sergej --->
|
2016-12-19 17:44:15 +01:00
|
|
|
__components[entityComponent.id] = entityComponent;
|
2016-12-16 16:03:48 +01:00
|
|
|
}
|
2016-12-19 17:44:15 +01:00
|
|
|
|
|
|
|
return __components;
|
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
}
|
2016-12-19 17:44:15 +01:00
|
|
|
}(queryComponent),
|
|
|
|
{}
|
2016-12-16 16:03:48 +01:00
|
|
|
);
|
2016-12-19 17:44:15 +01:00
|
|
|
|
|
|
|
for (var property in components) {
|
|
|
|
if (components.hasOwnProperty(property)) {
|
|
|
|
componentResult[property] = components[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return componentResult;
|
2016-12-16 16:03:48 +01:00
|
|
|
}
|
2016-12-19 17:44:15 +01:00
|
|
|
}(entity),
|
|
|
|
{}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
for (var property in results) {
|
|
|
|
if (results.hasOwnProperty(property)) {
|
|
|
|
result[property] = results[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
}
|
2016-12-19 17:44:15 +01:00
|
|
|
}(components),
|
|
|
|
{}
|
2016-12-16 16:03:48 +01:00
|
|
|
);
|
|
|
|
|
2016-12-19 17:44:15 +01:00
|
|
|
var array = [];
|
|
|
|
|
|
|
|
for (var property in result) {
|
|
|
|
if (result.hasOwnProperty(property)) {
|
|
|
|
array.push(result[property]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a GameLib.Entity to GameLib.API.Entity
|
2016-12-16 16:03:48 +01:00
|
|
|
* @returns {GameLib.API.EntityManager}
|
2016-12-15 14:53:39 +01:00
|
|
|
*/
|
2016-12-15 15:28:00 +01:00
|
|
|
GameLib.EntityManager.prototype.toApiEntityManager = function() {
|
2016-12-15 14:53:39 +01:00
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
var apiEntities = this.entities.map(
|
|
|
|
function (entity) {
|
|
|
|
return entity.toApiEntity();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
var apiEntityManager = new GameLib.API.EntityManager(
|
|
|
|
apiEntities
|
|
|
|
);
|
|
|
|
|
|
|
|
return apiEntityManager;
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2016-12-16 16:03:48 +01:00
|
|
|
* @param graphics
|
|
|
|
* @param objectEntities Object
|
2016-12-15 14:53:39 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
2016-12-16 16:03:48 +01:00
|
|
|
GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntities) {
|
|
|
|
|
|
|
|
var apiEntities = objectEntities.entities.map(
|
2016-12-19 17:44:15 +01:00
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
function (objectEntity) {
|
|
|
|
|
|
|
|
objectEntity.components = objectEntity.components.map(
|
|
|
|
function (component) {
|
|
|
|
if (component instanceof Object) {
|
2016-12-19 17:44:15 +01:00
|
|
|
if (component.componentType === GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) {
|
2016-12-16 16:03:48 +01:00
|
|
|
return GameLib.D3.PathFollowing.FromObjectComponent(graphics, component);
|
2016-12-19 17:44:15 +01:00
|
|
|
} else if (component.componentType === GameLib.Component.COMPONENT_RENDERABLE) {
|
|
|
|
return GameLib.D3.Renderable.FromObjectComponent(graphics, component);
|
|
|
|
} else if (component.componentType === GameLib.Component.COMPONENT_RENDERER) {
|
|
|
|
return GameLib.D3.Renderer.FromObjectComponent(graphics, component);
|
|
|
|
} else if (component.componentType === GameLib.Component.COMPONENT_EDITOR_INPUT) {
|
|
|
|
return GameLib.D3.Input.Editor.FromObjectComponent(graphics, component);
|
|
|
|
} else if (component.componentType === GameLib.Component.COMPONENT_LOOK_AT) {
|
|
|
|
return GameLib.D3.LookAt.FromObjectComponent(graphics, component);
|
2016-12-16 16:03:48 +01:00
|
|
|
} else {
|
|
|
|
console.warn('no component was associated with this object');
|
|
|
|
throw new Error('no component was associated with this object');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return new GameLib.API.Entity(
|
|
|
|
objectEntity.id,
|
|
|
|
objectEntity.name,
|
|
|
|
objectEntity.components
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
var apiEntityManager = new GameLib.API.EntityManager(
|
|
|
|
apiEntities
|
|
|
|
);
|
|
|
|
|
|
|
|
var entityManager = new GameLib.EntityManager(
|
|
|
|
this,
|
|
|
|
apiEntityManager
|
|
|
|
);
|
|
|
|
|
|
|
|
entityManager.entities = entityManager.entities.map(
|
|
|
|
function(apiEntity) {
|
|
|
|
return new GameLib.Entity(
|
|
|
|
entityManager,
|
|
|
|
entityManager,
|
|
|
|
apiEntity
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return entityManager;
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Links object Ids to actual objects
|
|
|
|
* @param idToObject
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.linkObjects = function(idToObject) {
|
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
this.entities.map(
|
|
|
|
function(entity) {
|
|
|
|
entity.components.map(
|
|
|
|
function (componentId, index, array) {
|
|
|
|
if (componentId instanceof GameLib.Component) {
|
|
|
|
array[index] = componentId;
|
|
|
|
} else {
|
|
|
|
array[index] = idToObject[componentId];
|
|
|
|
}
|
2016-12-19 17:44:15 +01:00
|
|
|
|
|
|
|
array[index].linkedObjects.map(
|
|
|
|
function (propertyName) {
|
|
|
|
array[index][propertyName] = idToObject[array[index][propertyName]];
|
|
|
|
}
|
|
|
|
)
|
2016-12-16 16:03:48 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|