Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/excaliburjs/ecs. Extracted and Slightly Modified Excalibur ECS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149import { Entity } from './entity.ts';import { World } from './world.ts';
// Add/Remove entities and components
export class EntityManager<TContext = any> { public entities: Entity[] = []; public _entityIndex: { [entityId: string]: Entity } = {}; private _childAddedHandlerMap = new Map<Entity, (entity: Entity) => void>(); private _childRemovedHandlerMap = new Map<Entity, (entity: Entity) => void>();
constructor(private _world: World) { }
/** * Runs the entity lifecycle * @param context * @param elapsed */ public updateEntities(context: TContext, elapsed: number) { for (let entityIndex = 0; entityIndex < this.entities.length; entityIndex++) { const entity = this.entities[entityIndex]; entity.update(context, elapsed); if (!entity.isActive) { this.removeEntity(entity); } } }
public findEntitiesForRemoval() { for (let entityIndex = 0; entityIndex < this.entities.length; entityIndex++) { const entity = this.entities[entityIndex]; if (!entity.isActive) { this.removeEntity(entity); } } }
private _createChildAddedHandler = () => (e: Entity) => { this.addEntity(e); };
private _createChildRemovedHandler = () => (e: Entity) => { this.removeEntity(e, false); };
/** * Adds an entity to be tracked by the EntityManager * @param entity */ public addEntity(entity: Entity): void { entity.isActive = true; entity.context = this._world.context; if (entity && !this._entityIndex[entity.id]) { this._entityIndex[entity.id] = entity; this.entities.push(entity); this._world.queryManager.addEntity(entity);
// if entity has children entity.children.forEach((c) => { c.context = entity.context; this.addEntity(c); }); const childAdded = this._createChildAddedHandler(); this._childAddedHandlerMap.set(entity, childAdded); const childRemoved = this._createChildRemovedHandler(); this._childRemovedHandlerMap.set(entity, childRemoved); entity.childrenAdded$.subscribe(childAdded); entity.childrenRemoved$.subscribe(childRemoved); } }
public removeEntity(entity: Entity, deferred?: boolean): void; public removeEntity(id: number, deferred?: boolean): void; public removeEntity(idOrEntity: number | Entity, deferred = true): void { let id = 0; if (idOrEntity instanceof Entity) { id = idOrEntity.id; } else { id = idOrEntity; } const entity = this._entityIndex[id]; if (entity && entity.isActive) { entity.isActive = false; }
if (entity && deferred) { this._entitiesToRemove.push(entity); return; }
delete this._entityIndex[id]; if (entity) { entity.context = null; const index = this.entities.indexOf(entity); if (index > -1) { this.entities.splice(index, 1); } this._world.queryManager.removeEntity(entity);
// if entity has children entity.children.forEach((c) => { c.context = null; this.removeEntity(c, deferred); }); const childAddedHandler = this._childAddedHandlerMap.get(entity); if (childAddedHandler) { entity.childrenAdded$.unsubscribe(childAddedHandler); } const childRemovedHandler = this._childRemovedHandlerMap.get(entity); if (childRemovedHandler) { entity.childrenRemoved$.unsubscribe(childRemovedHandler); } } }
private _entitiesToRemove: Entity[] = []; public processEntityRemovals(): void { for (let entityIndex = 0; entityIndex < this._entitiesToRemove.length; entityIndex++) { const entity = this._entitiesToRemove[entityIndex]; if (entity.isActive) { continue; } this.removeEntity(entity, false); } this._entitiesToRemove.length = 0; }
public processComponentRemovals(): void { for (let entityIndex = 0; entityIndex < this.entities.length; entityIndex++) { const entity = this.entities[entityIndex]; entity.processComponentRemoval(); } }
public getById(id: number): Entity | undefined { return this._entityIndex[id]; }
public getByName(name: string): Entity[] { return this.entities.filter((e) => e.name === name); }
public clear(): void { for (let i = this.entities.length - 1; i >= 0; i--) { this.removeEntity(this.entities[i]); } }}