12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793 |
- /**
- * Return the name of a component
- * @param {Component} Component
- * @private
- */
- /**
- * Get a key from a list of components
- * @param {Array(Component)} Components Array of components to generate the key
- * @private
- */
- function queryKey(Components) {
- var ids = [];
- for (var n = 0; n < Components.length; n++) {
- var T = Components[n];
- if (!componentRegistered(T)) {
- throw new Error(`Tried to create a query with an unregistered component`);
- }
- if (typeof T === "object") {
- var operator = T.operator === "not" ? "!" : T.operator;
- ids.push(operator + T.Component._typeId);
- } else {
- ids.push(T._typeId);
- }
- }
- return ids.sort().join("-");
- }
- // Detector for browser's "window"
- const hasWindow = typeof window !== "undefined";
- // performance.now() "polyfill"
- const now =
- hasWindow && typeof window.performance !== "undefined"
- ? performance.now.bind(performance)
- : Date.now.bind(Date);
- function componentRegistered(T) {
- return (
- (typeof T === "object" && T.Component._typeId !== undefined) ||
- (T.isComponent && T._typeId !== undefined)
- );
- }
- class SystemManager {
- constructor(world) {
- this._systems = [];
- this._executeSystems = []; // Systems that have `execute` method
- this.world = world;
- this.lastExecutedSystem = null;
- }
- registerSystem(SystemClass, attributes) {
- if (!SystemClass.isSystem) {
- throw new Error(
- `System '${SystemClass.name}' does not extend 'System' class`
- );
- }
- if (this.getSystem(SystemClass) !== undefined) {
- console.warn(`System '${SystemClass.getName()}' already registered.`);
- return this;
- }
- var system = new SystemClass(this.world, attributes);
- if (system.init) system.init(attributes);
- system.order = this._systems.length;
- this._systems.push(system);
- if (system.execute) {
- this._executeSystems.push(system);
- this.sortSystems();
- }
- return this;
- }
- unregisterSystem(SystemClass) {
- let system = this.getSystem(SystemClass);
- if (system === undefined) {
- console.warn(
- `Can unregister system '${SystemClass.getName()}'. It doesn't exist.`
- );
- return this;
- }
- this._systems.splice(this._systems.indexOf(system), 1);
- if (system.execute) {
- this._executeSystems.splice(this._executeSystems.indexOf(system), 1);
- }
- // @todo Add system.unregister() call to free resources
- return this;
- }
- sortSystems() {
- this._executeSystems.sort((a, b) => {
- return a.priority - b.priority || a.order - b.order;
- });
- }
- getSystem(SystemClass) {
- return this._systems.find((s) => s instanceof SystemClass);
- }
- getSystems() {
- return this._systems;
- }
- removeSystem(SystemClass) {
- var index = this._systems.indexOf(SystemClass);
- if (!~index) return;
- this._systems.splice(index, 1);
- }
- executeSystem(system, delta, time) {
- if (system.initialized) {
- if (system.canExecute()) {
- let startTime = now();
- system.execute(delta, time);
- system.executeTime = now() - startTime;
- this.lastExecutedSystem = system;
- system.clearEvents();
- }
- }
- }
- stop() {
- this._executeSystems.forEach((system) => system.stop());
- }
- execute(delta, time, forcePlay) {
- this._executeSystems.forEach(
- (system) =>
- (forcePlay || system.enabled) && this.executeSystem(system, delta, time)
- );
- }
- stats() {
- var stats = {
- numSystems: this._systems.length,
- systems: {},
- };
- for (var i = 0; i < this._systems.length; i++) {
- var system = this._systems[i];
- var systemStats = (stats.systems[system.getName()] = {
- queries: {},
- executeTime: system.executeTime,
- });
- for (var name in system.ctx) {
- systemStats.queries[name] = system.ctx[name].stats();
- }
- }
- return stats;
- }
- }
- class ObjectPool {
- // @todo Add initial size
- constructor(T, initialSize) {
- this.freeList = [];
- this.count = 0;
- this.T = T;
- this.isObjectPool = true;
- if (typeof initialSize !== "undefined") {
- this.expand(initialSize);
- }
- }
- acquire() {
- // Grow the list by 20%ish if we're out
- if (this.freeList.length <= 0) {
- this.expand(Math.round(this.count * 0.2) + 1);
- }
- var item = this.freeList.pop();
- return item;
- }
- release(item) {
- item.reset();
- this.freeList.push(item);
- }
- expand(count) {
- for (var n = 0; n < count; n++) {
- var clone = new this.T();
- clone._pool = this;
- this.freeList.push(clone);
- }
- this.count += count;
- }
- totalSize() {
- return this.count;
- }
- totalFree() {
- return this.freeList.length;
- }
- totalUsed() {
- return this.count - this.freeList.length;
- }
- }
- /**
- * @private
- * @class EventDispatcher
- */
- class EventDispatcher {
- constructor() {
- this._listeners = {};
- this.stats = {
- fired: 0,
- handled: 0,
- };
- }
- /**
- * Add an event listener
- * @param {String} eventName Name of the event to listen
- * @param {Function} listener Callback to trigger when the event is fired
- */
- addEventListener(eventName, listener) {
- let listeners = this._listeners;
- if (listeners[eventName] === undefined) {
- listeners[eventName] = [];
- }
- if (listeners[eventName].indexOf(listener) === -1) {
- listeners[eventName].push(listener);
- }
- }
- /**
- * Check if an event listener is already added to the list of listeners
- * @param {String} eventName Name of the event to check
- * @param {Function} listener Callback for the specified event
- */
- hasEventListener(eventName, listener) {
- return (
- this._listeners[eventName] !== undefined &&
- this._listeners[eventName].indexOf(listener) !== -1
- );
- }
- /**
- * Remove an event listener
- * @param {String} eventName Name of the event to remove
- * @param {Function} listener Callback for the specified event
- */
- removeEventListener(eventName, listener) {
- var listenerArray = this._listeners[eventName];
- if (listenerArray !== undefined) {
- var index = listenerArray.indexOf(listener);
- if (index !== -1) {
- listenerArray.splice(index, 1);
- }
- }
- }
- /**
- * Dispatch an event
- * @param {String} eventName Name of the event to dispatch
- * @param {Entity} entity (Optional) Entity to emit
- * @param {Component} component
- */
- dispatchEvent(eventName, entity, component) {
- this.stats.fired++;
- var listenerArray = this._listeners[eventName];
- if (listenerArray !== undefined) {
- var array = listenerArray.slice(0);
- for (var i = 0; i < array.length; i++) {
- array[i].call(this, entity, component);
- }
- }
- }
- /**
- * Reset stats counters
- */
- resetCounters() {
- this.stats.fired = this.stats.handled = 0;
- }
- }
- class Query {
- /**
- * @param {Array(Component)} Components List of types of components to query
- */
- constructor(Components, manager) {
- this.Components = [];
- this.NotComponents = [];
- Components.forEach((component) => {
- if (typeof component === "object") {
- this.NotComponents.push(component.Component);
- } else {
- this.Components.push(component);
- }
- });
- if (this.Components.length === 0) {
- throw new Error("Can't create a query without components");
- }
- this.entities = [];
- this.eventDispatcher = new EventDispatcher();
- // This query is being used by a reactive system
- this.reactive = false;
- this.key = queryKey(Components);
- // Fill the query with the existing entities
- for (var i = 0; i < manager._entities.length; i++) {
- var entity = manager._entities[i];
- if (this.match(entity)) {
- // @todo ??? this.addEntity(entity); => preventing the event to be generated
- entity.queries.push(this);
- this.entities.push(entity);
- }
- }
- }
- /**
- * Add entity to this query
- * @param {Entity} entity
- */
- addEntity(entity) {
- entity.queries.push(this);
- this.entities.push(entity);
- this.eventDispatcher.dispatchEvent(Query.prototype.ENTITY_ADDED, entity);
- }
- /**
- * Remove entity from this query
- * @param {Entity} entity
- */
- removeEntity(entity) {
- let index = this.entities.indexOf(entity);
- if (~index) {
- this.entities.splice(index, 1);
- index = entity.queries.indexOf(this);
- entity.queries.splice(index, 1);
- this.eventDispatcher.dispatchEvent(
- Query.prototype.ENTITY_REMOVED,
- entity
- );
- }
- }
- match(entity) {
- return (
- entity.hasAllComponents(this.Components) &&
- !entity.hasAnyComponents(this.NotComponents)
- );
- }
- toJSON() {
- return {
- key: this.key,
- reactive: this.reactive,
- components: {
- included: this.Components.map((C) => C.name),
- not: this.NotComponents.map((C) => C.name),
- },
- numEntities: this.entities.length,
- };
- }
- /**
- * Return stats for this query
- */
- stats() {
- return {
- numComponents: this.Components.length,
- numEntities: this.entities.length,
- };
- }
- }
- Query.prototype.ENTITY_ADDED = "Query#ENTITY_ADDED";
- Query.prototype.ENTITY_REMOVED = "Query#ENTITY_REMOVED";
- Query.prototype.COMPONENT_CHANGED = "Query#COMPONENT_CHANGED";
- /**
- * @private
- * @class QueryManager
- */
- class QueryManager {
- constructor(world) {
- this._world = world;
- // Queries indexed by a unique identifier for the components it has
- this._queries = {};
- }
- onEntityRemoved(entity) {
- for (var queryName in this._queries) {
- var query = this._queries[queryName];
- if (entity.queries.indexOf(query) !== -1) {
- query.removeEntity(entity);
- }
- }
- }
- /**
- * Callback when a component is added to an entity
- * @param {Entity} entity Entity that just got the new component
- * @param {Component} Component Component added to the entity
- */
- onEntityComponentAdded(entity, Component) {
- // @todo Use bitmask for checking components?
- // Check each indexed query to see if we need to add this entity to the list
- for (var queryName in this._queries) {
- var query = this._queries[queryName];
- if (
- !!~query.NotComponents.indexOf(Component) &&
- ~query.entities.indexOf(entity)
- ) {
- query.removeEntity(entity);
- continue;
- }
- // Add the entity only if:
- // Component is in the query
- // and Entity has ALL the components of the query
- // and Entity is not already in the query
- if (
- !~query.Components.indexOf(Component) ||
- !query.match(entity) ||
- ~query.entities.indexOf(entity)
- )
- continue;
- query.addEntity(entity);
- }
- }
- /**
- * Callback when a component is removed from an entity
- * @param {Entity} entity Entity to remove the component from
- * @param {Component} Component Component to remove from the entity
- */
- onEntityComponentRemoved(entity, Component) {
- for (var queryName in this._queries) {
- var query = this._queries[queryName];
- if (
- !!~query.NotComponents.indexOf(Component) &&
- !~query.entities.indexOf(entity) &&
- query.match(entity)
- ) {
- query.addEntity(entity);
- continue;
- }
- if (
- !!~query.Components.indexOf(Component) &&
- !!~query.entities.indexOf(entity) &&
- !query.match(entity)
- ) {
- query.removeEntity(entity);
- continue;
- }
- }
- }
- /**
- * Get a query for the specified components
- * @param {Component} Components Components that the query should have
- */
- getQuery(Components) {
- var key = queryKey(Components);
- var query = this._queries[key];
- if (!query) {
- this._queries[key] = query = new Query(Components, this._world);
- }
- return query;
- }
- /**
- * Return some stats from this class
- */
- stats() {
- var stats = {};
- for (var queryName in this._queries) {
- stats[queryName] = this._queries[queryName].stats();
- }
- return stats;
- }
- }
- class Component {
- constructor(props) {
- if (props !== false) {
- const schema = this.constructor.schema;
- for (const key in schema) {
- if (props && props.hasOwnProperty(key)) {
- this[key] = props[key];
- } else {
- const schemaProp = schema[key];
- if (schemaProp.hasOwnProperty("default")) {
- this[key] = schemaProp.type.clone(schemaProp.default);
- } else {
- const type = schemaProp.type;
- this[key] = type.clone(type.default);
- }
- }
- }
- if ( props !== undefined) {
- this.checkUndefinedAttributes(props);
- }
- }
- this._pool = null;
- }
- copy(source) {
- const schema = this.constructor.schema;
- for (const key in schema) {
- const prop = schema[key];
- if (source.hasOwnProperty(key)) {
- this[key] = prop.type.copy(source[key], this[key]);
- }
- }
- // @DEBUG
- {
- this.checkUndefinedAttributes(source);
- }
- return this;
- }
- clone() {
- return new this.constructor().copy(this);
- }
- reset() {
- const schema = this.constructor.schema;
- for (const key in schema) {
- const schemaProp = schema[key];
- if (schemaProp.hasOwnProperty("default")) {
- this[key] = schemaProp.type.copy(schemaProp.default, this[key]);
- } else {
- const type = schemaProp.type;
- this[key] = type.copy(type.default, this[key]);
- }
- }
- }
- dispose() {
- if (this._pool) {
- this._pool.release(this);
- }
- }
- getName() {
- return this.constructor.getName();
- }
- checkUndefinedAttributes(src) {
- const schema = this.constructor.schema;
- // Check that the attributes defined in source are also defined in the schema
- Object.keys(src).forEach((srcKey) => {
- if (!schema.hasOwnProperty(srcKey)) {
- console.warn(
- `Trying to set attribute '${srcKey}' not defined in the '${this.constructor.name}' schema. Please fix the schema, the attribute value won't be set`
- );
- }
- });
- }
- }
- Component.schema = {};
- Component.isComponent = true;
- Component.getName = function () {
- return this.displayName || this.name;
- };
- class SystemStateComponent extends Component {}
- SystemStateComponent.isSystemStateComponent = true;
- class EntityPool extends ObjectPool {
- constructor(entityManager, entityClass, initialSize) {
- super(entityClass, undefined);
- this.entityManager = entityManager;
- if (typeof initialSize !== "undefined") {
- this.expand(initialSize);
- }
- }
- expand(count) {
- for (var n = 0; n < count; n++) {
- var clone = new this.T(this.entityManager);
- clone._pool = this;
- this.freeList.push(clone);
- }
- this.count += count;
- }
- }
- /**
- * @private
- * @class EntityManager
- */
- class EntityManager {
- constructor(world) {
- this.world = world;
- this.componentsManager = world.componentsManager;
- // All the entities in this instance
- this._entities = [];
- this._nextEntityId = 0;
- this._entitiesByNames = {};
- this._queryManager = new QueryManager(this);
- this.eventDispatcher = new EventDispatcher();
- this._entityPool = new EntityPool(
- this,
- this.world.options.entityClass,
- this.world.options.entityPoolSize
- );
- // Deferred deletion
- this.entitiesWithComponentsToRemove = [];
- this.entitiesToRemove = [];
- this.deferredRemovalEnabled = true;
- }
- getEntityByName(name) {
- return this._entitiesByNames[name];
- }
- /**
- * Create a new entity
- */
- createEntity(name) {
- var entity = this._entityPool.acquire();
- entity.alive = true;
- entity.name = name || "";
- if (name) {
- if (this._entitiesByNames[name]) {
- console.warn(`Entity name '${name}' already exist`);
- } else {
- this._entitiesByNames[name] = entity;
- }
- }
- this._entities.push(entity);
- this.eventDispatcher.dispatchEvent(ENTITY_CREATED, entity);
- return entity;
- }
- // COMPONENTS
- /**
- * Add a component to an entity
- * @param {Entity} entity Entity where the component will be added
- * @param {Component} Component Component to be added to the entity
- * @param {Object} values Optional values to replace the default attributes
- */
- entityAddComponent(entity, Component, values) {
- // @todo Probably define Component._typeId with a default value and avoid using typeof
- if (
- typeof Component._typeId === "undefined" &&
- !this.world.componentsManager._ComponentsMap[Component._typeId]
- ) {
- throw new Error(
- `Attempted to add unregistered component "${Component.getName()}"`
- );
- }
- if (~entity._ComponentTypes.indexOf(Component)) {
- {
- console.warn(
- "Component type already exists on entity.",
- entity,
- Component.getName()
- );
- }
- return;
- }
- entity._ComponentTypes.push(Component);
- if (Component.__proto__ === SystemStateComponent) {
- entity.numStateComponents++;
- }
- var componentPool = this.world.componentsManager.getComponentsPool(
- Component
- );
- var component = componentPool
- ? componentPool.acquire()
- : new Component(values);
- if (componentPool && values) {
- component.copy(values);
- }
- entity._components[Component._typeId] = component;
- this._queryManager.onEntityComponentAdded(entity, Component);
- this.world.componentsManager.componentAddedToEntity(Component);
- this.eventDispatcher.dispatchEvent(COMPONENT_ADDED, entity, Component);
- }
- /**
- * Remove a component from an entity
- * @param {Entity} entity Entity which will get removed the component
- * @param {*} Component Component to remove from the entity
- * @param {Bool} immediately If you want to remove the component immediately instead of deferred (Default is false)
- */
- entityRemoveComponent(entity, Component, immediately) {
- var index = entity._ComponentTypes.indexOf(Component);
- if (!~index) return;
- this.eventDispatcher.dispatchEvent(COMPONENT_REMOVE, entity, Component);
- if (immediately) {
- this._entityRemoveComponentSync(entity, Component, index);
- } else {
- if (entity._ComponentTypesToRemove.length === 0)
- this.entitiesWithComponentsToRemove.push(entity);
- entity._ComponentTypes.splice(index, 1);
- entity._ComponentTypesToRemove.push(Component);
- entity._componentsToRemove[Component._typeId] =
- entity._components[Component._typeId];
- delete entity._components[Component._typeId];
- }
- // Check each indexed query to see if we need to remove it
- this._queryManager.onEntityComponentRemoved(entity, Component);
- if (Component.__proto__ === SystemStateComponent) {
- entity.numStateComponents--;
- // Check if the entity was a ghost waiting for the last system state component to be removed
- if (entity.numStateComponents === 0 && !entity.alive) {
- entity.remove();
- }
- }
- }
- _entityRemoveComponentSync(entity, Component, index) {
- // Remove T listing on entity and property ref, then free the component.
- entity._ComponentTypes.splice(index, 1);
- var component = entity._components[Component._typeId];
- delete entity._components[Component._typeId];
- component.dispose();
- this.world.componentsManager.componentRemovedFromEntity(Component);
- }
- /**
- * Remove all the components from an entity
- * @param {Entity} entity Entity from which the components will be removed
- */
- entityRemoveAllComponents(entity, immediately) {
- let Components = entity._ComponentTypes;
- for (let j = Components.length - 1; j >= 0; j--) {
- if (Components[j].__proto__ !== SystemStateComponent)
- this.entityRemoveComponent(entity, Components[j], immediately);
- }
- }
- /**
- * Remove the entity from this manager. It will clear also its components
- * @param {Entity} entity Entity to remove from the manager
- * @param {Bool} immediately If you want to remove the component immediately instead of deferred (Default is false)
- */
- removeEntity(entity, immediately) {
- var index = this._entities.indexOf(entity);
- if (!~index) throw new Error("Tried to remove entity not in list");
- entity.alive = false;
- this.entityRemoveAllComponents(entity, immediately);
- if (entity.numStateComponents === 0) {
- // Remove from entity list
- this.eventDispatcher.dispatchEvent(ENTITY_REMOVED, entity);
- this._queryManager.onEntityRemoved(entity);
- if (immediately === true) {
- this._releaseEntity(entity, index);
- } else {
- this.entitiesToRemove.push(entity);
- }
- }
- }
- _releaseEntity(entity, index) {
- this._entities.splice(index, 1);
- if (this._entitiesByNames[entity.name]) {
- delete this._entitiesByNames[entity.name];
- }
- entity._pool.release(entity);
- }
- /**
- * Remove all entities from this manager
- */
- removeAllEntities() {
- for (var i = this._entities.length - 1; i >= 0; i--) {
- this.removeEntity(this._entities[i]);
- }
- }
- processDeferredRemoval() {
- if (!this.deferredRemovalEnabled) {
- return;
- }
- for (let i = 0; i < this.entitiesToRemove.length; i++) {
- let entity = this.entitiesToRemove[i];
- let index = this._entities.indexOf(entity);
- this._releaseEntity(entity, index);
- }
- this.entitiesToRemove.length = 0;
- for (let i = 0; i < this.entitiesWithComponentsToRemove.length; i++) {
- let entity = this.entitiesWithComponentsToRemove[i];
- while (entity._ComponentTypesToRemove.length > 0) {
- let Component = entity._ComponentTypesToRemove.pop();
- var component = entity._componentsToRemove[Component._typeId];
- delete entity._componentsToRemove[Component._typeId];
- component.dispose();
- this.world.componentsManager.componentRemovedFromEntity(Component);
- //this._entityRemoveComponentSync(entity, Component, index);
- }
- }
- this.entitiesWithComponentsToRemove.length = 0;
- }
- /**
- * Get a query based on a list of components
- * @param {Array(Component)} Components List of components that will form the query
- */
- queryComponents(Components) {
- return this._queryManager.getQuery(Components);
- }
- // EXTRAS
- /**
- * Return number of entities
- */
- count() {
- return this._entities.length;
- }
- /**
- * Return some stats
- */
- stats() {
- var stats = {
- numEntities: this._entities.length,
- numQueries: Object.keys(this._queryManager._queries).length,
- queries: this._queryManager.stats(),
- numComponentPool: Object.keys(this.componentsManager._componentPool)
- .length,
- componentPool: {},
- eventDispatcher: this.eventDispatcher.stats,
- };
- for (var ecsyComponentId in this.componentsManager._componentPool) {
- var pool = this.componentsManager._componentPool[ecsyComponentId];
- stats.componentPool[pool.T.getName()] = {
- used: pool.totalUsed(),
- size: pool.count,
- };
- }
- return stats;
- }
- }
- const ENTITY_CREATED = "EntityManager#ENTITY_CREATE";
- const ENTITY_REMOVED = "EntityManager#ENTITY_REMOVED";
- const COMPONENT_ADDED = "EntityManager#COMPONENT_ADDED";
- const COMPONENT_REMOVE = "EntityManager#COMPONENT_REMOVE";
- class ComponentManager {
- constructor() {
- this.Components = [];
- this._ComponentsMap = {};
- this._componentPool = {};
- this.numComponents = {};
- this.nextComponentId = 0;
- }
- hasComponent(Component) {
- return this.Components.indexOf(Component) !== -1;
- }
- registerComponent(Component, objectPool) {
- if (this.Components.indexOf(Component) !== -1) {
- console.warn(
- `Component type: '${Component.getName()}' already registered.`
- );
- return;
- }
- const schema = Component.schema;
- if (!schema) {
- throw new Error(
- `Component "${Component.getName()}" has no schema property.`
- );
- }
- for (const propName in schema) {
- const prop = schema[propName];
- if (!prop.type) {
- throw new Error(
- `Invalid schema for component "${Component.getName()}". Missing type for "${propName}" property.`
- );
- }
- }
- Component._typeId = this.nextComponentId++;
- this.Components.push(Component);
- this._ComponentsMap[Component._typeId] = Component;
- this.numComponents[Component._typeId] = 0;
- if (objectPool === undefined) {
- objectPool = new ObjectPool(Component);
- } else if (objectPool === false) {
- objectPool = undefined;
- }
- this._componentPool[Component._typeId] = objectPool;
- }
- componentAddedToEntity(Component) {
- this.numComponents[Component._typeId]++;
- }
- componentRemovedFromEntity(Component) {
- this.numComponents[Component._typeId]--;
- }
- getComponentsPool(Component) {
- return this._componentPool[Component._typeId];
- }
- }
- const Version = "0.3.1";
- const proxyMap = new WeakMap();
- const proxyHandler = {
- set(target, prop) {
- throw new Error(
- `Tried to write to "${target.constructor.getName()}#${String(
- prop
- )}" on immutable component. Use .getMutableComponent() to modify a component.`
- );
- },
- };
- function wrapImmutableComponent(T, component) {
- if (component === undefined) {
- return undefined;
- }
- let wrappedComponent = proxyMap.get(component);
- if (!wrappedComponent) {
- wrappedComponent = new Proxy(component, proxyHandler);
- proxyMap.set(component, wrappedComponent);
- }
- return wrappedComponent;
- }
- class Entity {
- constructor(entityManager) {
- this._entityManager = entityManager || null;
- // Unique ID for this entity
- this.id = entityManager._nextEntityId++;
- // List of components types the entity has
- this._ComponentTypes = [];
- // Instance of the components
- this._components = {};
- this._componentsToRemove = {};
- // Queries where the entity is added
- this.queries = [];
- // Used for deferred removal
- this._ComponentTypesToRemove = [];
- this.alive = false;
- //if there are state components on a entity, it can't be removed completely
- this.numStateComponents = 0;
- }
- // COMPONENTS
- getComponent(Component, includeRemoved) {
- var component = this._components[Component._typeId];
- if (!component && includeRemoved === true) {
- component = this._componentsToRemove[Component._typeId];
- }
- return wrapImmutableComponent(Component, component)
- ;
- }
- getRemovedComponent(Component) {
- const component = this._componentsToRemove[Component._typeId];
- return wrapImmutableComponent(Component, component)
- ;
- }
- getComponents() {
- return this._components;
- }
- getComponentsToRemove() {
- return this._componentsToRemove;
- }
- getComponentTypes() {
- return this._ComponentTypes;
- }
- getMutableComponent(Component) {
- var component = this._components[Component._typeId];
- if (!component) {
- return;
- }
- for (var i = 0; i < this.queries.length; i++) {
- var query = this.queries[i];
- // @todo accelerate this check. Maybe having query._Components as an object
- // @todo add Not components
- if (query.reactive && query.Components.indexOf(Component) !== -1) {
- query.eventDispatcher.dispatchEvent(
- Query.prototype.COMPONENT_CHANGED,
- this,
- component
- );
- }
- }
- return component;
- }
- addComponent(Component, values) {
- this._entityManager.entityAddComponent(this, Component, values);
- return this;
- }
- removeComponent(Component, forceImmediate) {
- this._entityManager.entityRemoveComponent(this, Component, forceImmediate);
- return this;
- }
- hasComponent(Component, includeRemoved) {
- return (
- !!~this._ComponentTypes.indexOf(Component) ||
- (includeRemoved === true && this.hasRemovedComponent(Component))
- );
- }
- hasRemovedComponent(Component) {
- return !!~this._ComponentTypesToRemove.indexOf(Component);
- }
- hasAllComponents(Components) {
- for (var i = 0; i < Components.length; i++) {
- if (!this.hasComponent(Components[i])) return false;
- }
- return true;
- }
- hasAnyComponents(Components) {
- for (var i = 0; i < Components.length; i++) {
- if (this.hasComponent(Components[i])) return true;
- }
- return false;
- }
- removeAllComponents(forceImmediate) {
- return this._entityManager.entityRemoveAllComponents(this, forceImmediate);
- }
- copy(src) {
- // TODO: This can definitely be optimized
- for (var ecsyComponentId in src._components) {
- var srcComponent = src._components[ecsyComponentId];
- this.addComponent(srcComponent.constructor);
- var component = this.getComponent(srcComponent.constructor);
- component.copy(srcComponent);
- }
- return this;
- }
- clone() {
- return new Entity(this._entityManager).copy(this);
- }
- reset() {
- this.id = this._entityManager._nextEntityId++;
- this._ComponentTypes.length = 0;
- this.queries.length = 0;
- for (var ecsyComponentId in this._components) {
- delete this._components[ecsyComponentId];
- }
- }
- remove(forceImmediate) {
- return this._entityManager.removeEntity(this, forceImmediate);
- }
- }
- const DEFAULT_OPTIONS = {
- entityPoolSize: 0,
- entityClass: Entity,
- };
- class World {
- constructor(options = {}) {
- this.options = Object.assign({}, DEFAULT_OPTIONS, options);
- this.componentsManager = new ComponentManager(this);
- this.entityManager = new EntityManager(this);
- this.systemManager = new SystemManager(this);
- this.enabled = true;
- this.eventQueues = {};
- if (hasWindow && typeof CustomEvent !== "undefined") {
- var event = new CustomEvent("ecsy-world-created", {
- detail: { world: this, version: Version },
- });
- window.dispatchEvent(event);
- }
- this.lastTime = now() / 1000;
- }
- registerComponent(Component, objectPool) {
- this.componentsManager.registerComponent(Component, objectPool);
- return this;
- }
- registerSystem(System, attributes) {
- this.systemManager.registerSystem(System, attributes);
- return this;
- }
- hasRegisteredComponent(Component) {
- return this.componentsManager.hasComponent(Component);
- }
- unregisterSystem(System) {
- this.systemManager.unregisterSystem(System);
- return this;
- }
- getSystem(SystemClass) {
- return this.systemManager.getSystem(SystemClass);
- }
- getSystems() {
- return this.systemManager.getSystems();
- }
- execute(delta, time) {
- if (!delta) {
- time = now() / 1000;
- delta = time - this.lastTime;
- this.lastTime = time;
- }
- if (this.enabled) {
- this.systemManager.execute(delta, time);
- this.entityManager.processDeferredRemoval();
- }
- }
- stop() {
- this.enabled = false;
- }
- play() {
- this.enabled = true;
- }
- createEntity(name) {
- return this.entityManager.createEntity(name);
- }
- stats() {
- var stats = {
- entities: this.entityManager.stats(),
- system: this.systemManager.stats(),
- };
- return stats;
- }
- }
- class System {
- canExecute() {
- if (this._mandatoryQueries.length === 0) return true;
- for (let i = 0; i < this._mandatoryQueries.length; i++) {
- var query = this._mandatoryQueries[i];
- if (query.entities.length === 0) {
- return false;
- }
- }
- return true;
- }
- getName() {
- return this.constructor.getName();
- }
- constructor(world, attributes) {
- this.world = world;
- this.enabled = true;
- // @todo Better naming :)
- this._queries = {};
- this.queries = {};
- this.priority = 0;
- // Used for stats
- this.executeTime = 0;
- if (attributes && attributes.priority) {
- this.priority = attributes.priority;
- }
- this._mandatoryQueries = [];
- this.initialized = true;
- if (this.constructor.queries) {
- for (var queryName in this.constructor.queries) {
- var queryConfig = this.constructor.queries[queryName];
- var Components = queryConfig.components;
- if (!Components || Components.length === 0) {
- throw new Error("'components' attribute can't be empty in a query");
- }
- // Detect if the components have already been registered
- let unregisteredComponents = Components.filter(
- (Component) => !componentRegistered(Component)
- );
- if (unregisteredComponents.length > 0) {
- throw new Error(
- `Tried to create a query '${
- this.constructor.name
- }.${queryName}' with unregistered components: [${unregisteredComponents
- .map((c) => c.getName())
- .join(", ")}]`
- );
- }
- var query = this.world.entityManager.queryComponents(Components);
- this._queries[queryName] = query;
- if (queryConfig.mandatory === true) {
- this._mandatoryQueries.push(query);
- }
- this.queries[queryName] = {
- results: query.entities,
- };
- // Reactive configuration added/removed/changed
- var validEvents = ["added", "removed", "changed"];
- const eventMapping = {
- added: Query.prototype.ENTITY_ADDED,
- removed: Query.prototype.ENTITY_REMOVED,
- changed: Query.prototype.COMPONENT_CHANGED, // Query.prototype.ENTITY_CHANGED
- };
- if (queryConfig.listen) {
- validEvents.forEach((eventName) => {
- if (!this.execute) {
- console.warn(
- `System '${this.getName()}' has defined listen events (${validEvents.join(
- ", "
- )}) for query '${queryName}' but it does not implement the 'execute' method.`
- );
- }
- // Is the event enabled on this system's query?
- if (queryConfig.listen[eventName]) {
- let event = queryConfig.listen[eventName];
- if (eventName === "changed") {
- query.reactive = true;
- if (event === true) {
- // Any change on the entity from the components in the query
- let eventList = (this.queries[queryName][eventName] = []);
- query.eventDispatcher.addEventListener(
- Query.prototype.COMPONENT_CHANGED,
- (entity) => {
- // Avoid duplicates
- if (eventList.indexOf(entity) === -1) {
- eventList.push(entity);
- }
- }
- );
- } else if (Array.isArray(event)) {
- let eventList = (this.queries[queryName][eventName] = []);
- query.eventDispatcher.addEventListener(
- Query.prototype.COMPONENT_CHANGED,
- (entity, changedComponent) => {
- // Avoid duplicates
- if (
- event.indexOf(changedComponent.constructor) !== -1 &&
- eventList.indexOf(entity) === -1
- ) {
- eventList.push(entity);
- }
- }
- );
- }
- } else {
- let eventList = (this.queries[queryName][eventName] = []);
- query.eventDispatcher.addEventListener(
- eventMapping[eventName],
- (entity) => {
- // @fixme overhead?
- if (eventList.indexOf(entity) === -1)
- eventList.push(entity);
- }
- );
- }
- }
- });
- }
- }
- }
- }
- stop() {
- this.executeTime = 0;
- this.enabled = false;
- }
- play() {
- this.enabled = true;
- }
- // @question rename to clear queues?
- clearEvents() {
- for (let queryName in this.queries) {
- var query = this.queries[queryName];
- if (query.added) {
- query.added.length = 0;
- }
- if (query.removed) {
- query.removed.length = 0;
- }
- if (query.changed) {
- if (Array.isArray(query.changed)) {
- query.changed.length = 0;
- } else {
- for (let name in query.changed) {
- query.changed[name].length = 0;
- }
- }
- }
- }
- }
- toJSON() {
- var json = {
- name: this.getName(),
- enabled: this.enabled,
- executeTime: this.executeTime,
- priority: this.priority,
- queries: {},
- };
- if (this.constructor.queries) {
- var queries = this.constructor.queries;
- for (let queryName in queries) {
- let query = this.queries[queryName];
- let queryDefinition = queries[queryName];
- let jsonQuery = (json.queries[queryName] = {
- key: this._queries[queryName].key,
- });
- jsonQuery.mandatory = queryDefinition.mandatory === true;
- jsonQuery.reactive =
- queryDefinition.listen &&
- (queryDefinition.listen.added === true ||
- queryDefinition.listen.removed === true ||
- queryDefinition.listen.changed === true ||
- Array.isArray(queryDefinition.listen.changed));
- if (jsonQuery.reactive) {
- jsonQuery.listen = {};
- const methods = ["added", "removed", "changed"];
- methods.forEach((method) => {
- if (query[method]) {
- jsonQuery.listen[method] = {
- entities: query[method].length,
- };
- }
- });
- }
- }
- }
- return json;
- }
- }
- System.isSystem = true;
- System.getName = function () {
- return this.displayName || this.name;
- };
- function Not(Component) {
- return {
- operator: "not",
- Component: Component,
- };
- }
- class TagComponent extends Component {
- constructor() {
- super(false);
- }
- }
- TagComponent.isTagComponent = true;
- const copyValue = (src) => src;
- const cloneValue = (src) => src;
- const copyArray = (src, dest) => {
- if (!src) {
- return src;
- }
- if (!dest) {
- return src.slice();
- }
- dest.length = 0;
- for (let i = 0; i < src.length; i++) {
- dest.push(src[i]);
- }
- return dest;
- };
- const cloneArray = (src) => src && src.slice();
- const copyJSON = (src) => JSON.parse(JSON.stringify(src));
- const cloneJSON = (src) => JSON.parse(JSON.stringify(src));
- const copyCopyable = (src, dest) => {
- if (!src) {
- return src;
- }
- if (!dest) {
- return src.clone();
- }
- return dest.copy(src);
- };
- const cloneClonable = (src) => src && src.clone();
- function createType(typeDefinition) {
- var mandatoryProperties = ["name", "default", "copy", "clone"];
- var undefinedProperties = mandatoryProperties.filter((p) => {
- return !typeDefinition.hasOwnProperty(p);
- });
- if (undefinedProperties.length > 0) {
- throw new Error(
- `createType expects a type definition with the following properties: ${undefinedProperties.join(
- ", "
- )}`
- );
- }
- typeDefinition.isType = true;
- return typeDefinition;
- }
- /**
- * Standard types
- */
- const Types = {
- Number: createType({
- name: "Number",
- default: 0,
- copy: copyValue,
- clone: cloneValue,
- }),
- Boolean: createType({
- name: "Boolean",
- default: false,
- copy: copyValue,
- clone: cloneValue,
- }),
- String: createType({
- name: "String",
- default: "",
- copy: copyValue,
- clone: cloneValue,
- }),
- Array: createType({
- name: "Array",
- default: [],
- copy: copyArray,
- clone: cloneArray,
- }),
- Ref: createType({
- name: "Ref",
- default: undefined,
- copy: copyValue,
- clone: cloneValue,
- }),
- JSON: createType({
- name: "JSON",
- default: null,
- copy: copyJSON,
- clone: cloneJSON,
- }),
- };
- function generateId(length) {
- var result = "";
- var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- var charactersLength = characters.length;
- for (var i = 0; i < length; i++) {
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
- }
- return result;
- }
- function injectScript(src, onLoad) {
- var script = document.createElement("script");
- // @todo Use link to the ecsy-devtools repo?
- script.src = src;
- script.onload = onLoad;
- (document.head || document.documentElement).appendChild(script);
- }
- /* global Peer */
- function hookConsoleAndErrors(connection) {
- var wrapFunctions = ["error", "warning", "log"];
- wrapFunctions.forEach((key) => {
- if (typeof console[key] === "function") {
- var fn = console[key].bind(console);
- console[key] = (...args) => {
- connection.send({
- method: "console",
- type: key,
- args: JSON.stringify(args),
- });
- return fn.apply(null, args);
- };
- }
- });
- window.addEventListener("error", (error) => {
- connection.send({
- method: "error",
- error: JSON.stringify({
- message: error.error.message,
- stack: error.error.stack,
- }),
- });
- });
- }
- function includeRemoteIdHTML(remoteId) {
- let infoDiv = document.createElement("div");
- infoDiv.style.cssText = `
- align-items: center;
- background-color: #333;
- color: #aaa;
- display:flex;
- font-family: Arial;
- font-size: 1.1em;
- height: 40px;
- justify-content: center;
- left: 0;
- opacity: 0.9;
- position: absolute;
- right: 0;
- text-align: center;
- top: 0;
- `;
- infoDiv.innerHTML = `Open ECSY devtools to connect to this page using the code: <b style="color: #fff">${remoteId}</b> <button onClick="generateNewCode()">Generate new code</button>`;
- document.body.appendChild(infoDiv);
- return infoDiv;
- }
- function enableRemoteDevtools(remoteId) {
- if (!hasWindow) {
- console.warn("Remote devtools not available outside the browser");
- return;
- }
- window.generateNewCode = () => {
- window.localStorage.clear();
- remoteId = generateId(6);
- window.localStorage.setItem("ecsyRemoteId", remoteId);
- window.location.reload(false);
- };
- remoteId = remoteId || window.localStorage.getItem("ecsyRemoteId");
- if (!remoteId) {
- remoteId = generateId(6);
- window.localStorage.setItem("ecsyRemoteId", remoteId);
- }
- let infoDiv = includeRemoteIdHTML(remoteId);
- window.__ECSY_REMOTE_DEVTOOLS_INJECTED = true;
- window.__ECSY_REMOTE_DEVTOOLS = {};
- let Version = "";
- // This is used to collect the worlds created before the communication is being established
- let worldsBeforeLoading = [];
- let onWorldCreated = (e) => {
- var world = e.detail.world;
- Version = e.detail.version;
- worldsBeforeLoading.push(world);
- };
- window.addEventListener("ecsy-world-created", onWorldCreated);
- let onLoaded = () => {
- // var peer = new Peer(remoteId);
- var peer = new Peer(remoteId, {
- host: "peerjs.ecsy.io",
- secure: true,
- port: 443,
- config: {
- iceServers: [
- { url: "stun:stun.l.google.com:19302" },
- { url: "stun:stun1.l.google.com:19302" },
- { url: "stun:stun2.l.google.com:19302" },
- { url: "stun:stun3.l.google.com:19302" },
- { url: "stun:stun4.l.google.com:19302" },
- ],
- },
- debug: 3,
- });
- peer.on("open", (/* id */) => {
- peer.on("connection", (connection) => {
- window.__ECSY_REMOTE_DEVTOOLS.connection = connection;
- connection.on("open", function () {
- // infoDiv.style.visibility = "hidden";
- infoDiv.innerHTML = "Connected";
- // Receive messages
- connection.on("data", function (data) {
- if (data.type === "init") {
- var script = document.createElement("script");
- script.setAttribute("type", "text/javascript");
- script.onload = () => {
- script.parentNode.removeChild(script);
- // Once the script is injected we don't need to listen
- window.removeEventListener(
- "ecsy-world-created",
- onWorldCreated
- );
- worldsBeforeLoading.forEach((world) => {
- var event = new CustomEvent("ecsy-world-created", {
- detail: { world: world, version: Version },
- });
- window.dispatchEvent(event);
- });
- };
- script.innerHTML = data.script;
- (document.head || document.documentElement).appendChild(script);
- script.onload();
- hookConsoleAndErrors(connection);
- } else if (data.type === "executeScript") {
- let value = eval(data.script);
- if (data.returnEval) {
- connection.send({
- method: "evalReturn",
- value: value,
- });
- }
- }
- });
- });
- });
- });
- };
- // Inject PeerJS script
- injectScript(
- "https://cdn.jsdelivr.net/npm/peerjs@0.3.20/dist/peer.min.js",
- onLoaded
- );
- }
- if (hasWindow) {
- const urlParams = new URLSearchParams(window.location.search);
- // @todo Provide a way to disable it if needed
- if (urlParams.has("enable-remote-devtools")) {
- enableRemoteDevtools();
- }
- }
- export { Component, Not, ObjectPool, System, SystemStateComponent, TagComponent, Types, Version, World, Entity as _Entity, cloneArray, cloneClonable, cloneJSON, cloneValue, copyArray, copyCopyable, copyJSON, copyValue, createType, enableRemoteDevtools };
|