283 lines
6.5 KiB
JavaScript
283 lines
6.5 KiB
JavaScript
/**
|
|
* GameLib.EntityManager
|
|
* @param graphics GameLib.D3.Graphics
|
|
* @param apiEntityManager GameLib.API.EntityManager
|
|
* @constructor
|
|
*/
|
|
GameLib.EntityManager = function(
|
|
graphics,
|
|
apiEntityManager
|
|
) {
|
|
|
|
this.graphics = graphics;
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiEntityManager)) {
|
|
apiEntityManager = {};
|
|
}
|
|
|
|
if (apiEntityManager instanceof GameLib.EntityManager) {
|
|
return apiEntityManager;
|
|
}
|
|
|
|
GameLib.API.EntityManager.call(
|
|
this,
|
|
apiEntityManager.id,
|
|
apiEntityManager.name,
|
|
apiEntityManager.entities,
|
|
// apiEntityManager.systems,
|
|
apiEntityManager.parentEntity
|
|
);
|
|
|
|
this.entities = this.entities.map(
|
|
function(apiEntity) {
|
|
|
|
if (apiEntity instanceof GameLib.API.Entity) {
|
|
return new GameLib.Entity(
|
|
this.graphics,
|
|
apiEntity
|
|
)
|
|
} else {
|
|
console.warn('Entity not of type API.Entity');
|
|
throw new Error('Entity not of type API.Entity');
|
|
}
|
|
|
|
}.bind(this)
|
|
);
|
|
|
|
// this.systems = this.systems.map(
|
|
// function(apiSystem) {
|
|
//
|
|
// if (apiSystem instanceof GameLib.API.System) {
|
|
// return new GameLib.System(
|
|
// this.graphics,
|
|
// apiSystem
|
|
// )
|
|
// } else {
|
|
// console.warn('System not of type API.System');
|
|
// throw new Error('System not of type API.System');
|
|
// }
|
|
//
|
|
// }.bind(this)
|
|
// );
|
|
|
|
this.buildIdToObject();
|
|
|
|
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() {
|
|
/**
|
|
* Fuck the current ECS bullshit on the internet - both tiny-ecs and ecsjs SUCKS ASS
|
|
*/
|
|
return null;
|
|
};
|
|
|
|
/**
|
|
* Creates an GameLib.Entity and adds it to entities array
|
|
* @returns {*}
|
|
*/
|
|
GameLib.EntityManager.prototype.createEntity = function(name) {
|
|
|
|
var apiEntity = new GameLib.API.Entity(
|
|
null,
|
|
name
|
|
);
|
|
|
|
var entity = new GameLib.Entity(
|
|
this.graphics,
|
|
apiEntity
|
|
);
|
|
|
|
this.entities.push(entity);
|
|
|
|
return entity;
|
|
};
|
|
|
|
/**
|
|
* Adds an entity to this manager
|
|
* @param entity GameLib.Entity
|
|
*/
|
|
GameLib.EntityManager.prototype.addEntity = function(entity) {
|
|
this.entities.push(entity);
|
|
};
|
|
|
|
// /**
|
|
// * Adds a system to this manager
|
|
// * @param system GameLib.System
|
|
// */
|
|
// GameLib.EntityManager.prototype.addSystem = function(system) {
|
|
// this.systems.push(system);
|
|
// };
|
|
|
|
/**
|
|
* Returns entity by name
|
|
* @param name
|
|
* @returns {*}
|
|
*/
|
|
GameLib.EntityManager.prototype.queryByName = function(name) {
|
|
return this.entities.reduce(
|
|
function(result, entity){
|
|
if (entity.name === name) {
|
|
result = entity;
|
|
}
|
|
return result;
|
|
},
|
|
null
|
|
)
|
|
};
|
|
|
|
/**
|
|
* Removes an entity - do we remove all its components as well?
|
|
* @param entity GameLib.D3.Entity
|
|
* @returns boolean true if successful
|
|
*/
|
|
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 entities with the following components
|
|
* @param components GameLib.Component[]
|
|
*/
|
|
GameLib.EntityManager.prototype.query = function(components) {
|
|
|
|
var entities = this.entities.reduce(
|
|
function(result, entity) {
|
|
|
|
var hasAllComponents = components.reduce(
|
|
function(componentResult, component) {
|
|
if (!entity.hasComponent(component)) {
|
|
componentResult = false;
|
|
}
|
|
return componentResult;
|
|
},
|
|
true
|
|
);
|
|
|
|
if (hasAllComponents) {
|
|
result.push(entity);
|
|
}
|
|
|
|
return result;
|
|
},
|
|
[]
|
|
);
|
|
|
|
return entities;
|
|
};
|
|
|
|
/**
|
|
* Returns all actual components of all entities that contain this component
|
|
* @param constructor
|
|
*/
|
|
GameLib.EntityManager.prototype.queryComponents = function(constructor) {
|
|
|
|
var entities = this.query([constructor]);
|
|
|
|
var components = entities.reduce(
|
|
function(result, entity){
|
|
var ecs = entity.getComponent(constructor);
|
|
ecs.map(function(ec){
|
|
result.push(ec);
|
|
});
|
|
return result;
|
|
},
|
|
[]
|
|
);
|
|
|
|
return components;
|
|
};
|
|
|
|
/**
|
|
* Converts a GameLib.Entity to GameLib.API.Entity
|
|
* @returns {GameLib.API.EntityManager}
|
|
*/
|
|
GameLib.EntityManager.prototype.toApiEntityManager = function() {
|
|
|
|
var apiEntities = this.entities.map(
|
|
function (entity) {
|
|
return entity.toApiEntity();
|
|
}
|
|
);
|
|
|
|
// var apiSystems = this.systems.map(
|
|
// function (system) {
|
|
// return system.toApiSystem();
|
|
// }
|
|
// );
|
|
|
|
var apiEntityManager = new GameLib.API.EntityManager(
|
|
this.id,
|
|
this.name,
|
|
apiEntities,
|
|
// apiSystems,
|
|
this.parentEntity
|
|
);
|
|
|
|
return apiEntityManager;
|
|
};
|
|
|
|
/**
|
|
* Returns an EntityManager from an Object entity manager
|
|
* @param graphics
|
|
* @param objectEntityManager Object
|
|
* @constructor
|
|
*/
|
|
GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntityManager) {
|
|
|
|
var apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectEntityManager);
|
|
|
|
var entityManager = new GameLib.EntityManager(
|
|
graphics,
|
|
apiEntityManager
|
|
);
|
|
|
|
return entityManager;
|
|
};
|
|
|
|
/**
|
|
* Links object Ids to actual objects
|
|
* @param idToObject
|
|
*/
|
|
// GameLib.EntityManager.prototype.linkObjects = function(idToObject) {
|
|
//
|
|
// 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];
|
|
// }
|
|
//
|
|
// Object.keys(array[index].linkedObjects).map(
|
|
// function (propertyName) {
|
|
// array[index][propertyName] = idToObject[array[index][propertyName]];
|
|
// }
|
|
// );
|
|
//
|
|
// array[index].loaded = true;
|
|
// }
|
|
// )
|
|
// }
|
|
// );
|
|
//
|
|
// };
|