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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125import { Component, ComponentCtor } from './component.ts';import { Entity } from './entity.ts';import { EntityManager } from './entity-manager.ts';import { Query } from './query.ts';import { QueryManager } from './query-manager.ts';import { System, SystemType } from './system.ts';import { SystemCtor, SystemManager, isSystemConstructor } from './system-manager.ts';import { TagQuery } from './tag-query.ts';
/** * The World is a self-contained entity component system for a particular context. */export class World<TContext = any> { public queryManager: QueryManager = new QueryManager(this); public entityManager: EntityManager = new EntityManager(this); public systemManager: SystemManager = new SystemManager(this);
/** * The context type is passed to the system updates * @param context */ constructor(public context: TContext) { }
/** * Query the ECS world for entities that match your components * @param requiredTypes */ query<TKnownComponentCtors extends ComponentCtor<Component>>(requiredTypes: TKnownComponentCtors[]): Query<TKnownComponentCtors> { return this.queryManager.createQuery(requiredTypes); }
queryTags<TKnownTags extends string>(requiredTags: TKnownTags[]): TagQuery<TKnownTags> { return this.queryManager.createTagQuery(requiredTags); }
/** * Update systems by type and time elapsed in milliseconds */ update(type: SystemType, elapsed: number) { if (type === SystemType.Update) { this.entityManager.updateEntities(this.context, elapsed); } this.systemManager.updateSystems(type, this.context, elapsed); this.entityManager.findEntitiesForRemoval(); this.entityManager.processComponentRemovals(); this.entityManager.processEntityRemovals(); }
/** * Add an entity to the ECS world * @param entity */ add(entity: Entity): void; /** * Add a system to the ECS world * @param system */ add(system: System): void; add(system: SystemCtor<System>): void; add(entityOrSystem: Entity | System | SystemCtor<System>): void { if (entityOrSystem instanceof Entity) { this.entityManager.addEntity(entityOrSystem); return; }
if (entityOrSystem instanceof System || isSystemConstructor(entityOrSystem)) { this.systemManager.addSystem(entityOrSystem); return; }
//console.warn( // `Could not add entity/system ${(entityOrSystem as any).constructor.name} to Excalibur!\n\n` + // `If this looks like an Excalibur type, this can be caused by 2 versions of excalibur being included on the page.\n\n` + // `Check your bundler settings to make sure this is not the case! Excalibur has ESM & UMD bundles be sure one 1 is loaded.` //); }
/** * Get a system out of the ECS world */ get(system: SystemCtor<System>): System<TContext> | null { return this.systemManager.get(system); }
/** * Remove an entity from the ECS world * @param entity */ remove(entity: Entity, deferred?: boolean): void; /** * Remove a system from the ECS world * @param system */ remove(system: System): void; remove(entityOrSystem: Entity | System, deferred = true): void { if (entityOrSystem instanceof Entity) { this.entityManager.removeEntity(entityOrSystem, deferred); return; }
if (entityOrSystem instanceof System) { this.systemManager.removeSystem(entityOrSystem); return; }
//this._logger.warn( // `Could not remove entity/system ${(entityOrSystem as any).constructor.name} to Excalibur!\n\n` + // `If this looks like an Excalibur type, this can be caused by 2 versions of excalibur being included on the page.\n\n` + // `Check your bundler settings to make sure this is not the case! Excalibur has ESM & UMD bundles be sure one 1 is loaded.` //); }
get entities(): Entity<any, TContext>[] { return this.entityManager.entities; }
clearEntities(): void { this.entityManager.clear(); }
clearSystems(): void { this.systemManager.clear(); }}