ecsjs 1.0.0-beta.2 → 1.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -2
- package/dist/ecs.js +1 -1
- package/dist/ecs.js.map +3 -3
- package/dist/types/src/definitions.d.ts +5 -5
- package/dist/types/src/definitions.d.ts.map +1 -1
- package/dist/types/src/entity-map.d.ts +38 -8
- package/dist/types/src/entity-map.d.ts.map +1 -1
- package/dist/types/src/key-map.d.ts +1 -1
- package/dist/types/src/key-map.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/definitions.ts +5 -5
- package/src/entity-map.ts +50 -17
- package/src/key-map.ts +2 -2
package/README.md
CHANGED
package/dist/ecs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var s=class extends Map{constructor(e=[]){super(e)}firstEntry(){return this.size===0?
|
|
1
|
+
var s=class extends Map{constructor(e=[]){super(e)}firstEntry(){return this.size===0?void 0:this.entries().next().value}firstKey(){return this.size===0?void 0:this.keys().next().value}firstValue(){return this.size===0?void 0:this.values().next().value}toJSON(){return{__KeyMap__:1,iterable:[...this.entries()]}}toTable(e=!1){let t=[];for(let n of this.entries()){let o=n[1],r={};e==!1&&(r["entity.key"]=n[0]),r["entity.type"]=o.constructor.name,t.push({...r,...o})}return t}printTable(e=!1){console.table(this.toTable(e))}entries(){return super.entries().filter(e=>e!=null)}keys(){return super.keys().filter(e=>e!=null)}values(){return super.values().filter(e=>e!=null)}[Symbol.iterator](){return this.values()}static get[Symbol.species](){return Map}};var i=class a{componentMaps=new s;nextId=0;constructor(){}register(...e){for(let t of e){let n=t.name;if(n===void 0)throw new ReferenceError("The component class does not have a name defined. i.e. a constructor name");let o=new s;this.componentMaps.set(n,o)}return this}getMap(e){return this.componentMaps.get(e.name)}firstEntry(e){return this.getMap(e)?.firstEntry()}firstKey(e){return this.getMap(e)?.firstKey()}firstValue(e){return this.getMap(e)?.firstValue()}get(e,t){let n=this.componentMaps.get(t.name);if(n===void 0)throw new ReferenceError(`"${t}" component class does not exist in the componentMaps`);return n.get(e)}getByKey(e,t){let n=this.componentMaps.get(t);if(n===void 0)throw new ReferenceError(`"${t}" component does not exist in the componentMaps`);return n.get(e)}has(e,t){let n=this.componentMaps.get(t.name);return n===void 0?!1:n.has(e)}set(e,t){let n=this.componentMaps.get(t.constructor.name);if(n===void 0)throw new ReferenceError(`Component map does not exist using the name: ${t.constructor.name}`);return n.set(e,t),t}remove(e,t){return this.removeByKey(e,t.name)}removeByKey(e,t){let n=this.componentMaps.get(t);if(n===void 0)throw new ReferenceError(`A registered entity map does not exist for the given key: ${t}`);return n.get(e)===void 0?!1:n.delete(e)}destroyEntity(e){this.componentMaps.forEach(t=>{t.has(e)&&t.delete(e)})}getNextId(){return this.nextId++,this.nextId}clear(){return this.componentMaps.clear(),this}printTable(){return this.componentMaps.forEach(e=>console.table(e.toTable(!0))),this}static parse(e){let t=new a,n=JSON.parse(e,function(o,r){return r.hasOwnProperty("componentMaps")?(Reflect.setPrototypeOf(r,a.prototype),r):r.hasOwnProperty("__KeyMap__")?new s(r.iterable):this[o]});return t.componentMaps=n.componentMaps,t.nextId=n.nextId,t}static createWithTracing(e){let t={get(n,o){let r=n[o];return typeof r=="function"&&(e.length===0||e.includes(o))?function(...p){return console.groupCollapsed("ecs trace",o,p),console.trace(),console.groupEnd(),r.apply(this,p)}:r}};return new Proxy(new a,t)}};var u=new i;typeof window<"u"?window.ecs=u:typeof module<"u"&&module!==null&&(module.exports={ecs:u});export{u as ecs};
|
|
2
2
|
//# sourceMappingURL=ecs.js.map
|
package/dist/ecs.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/key-map.ts", "../src/entity-map.ts", "../src/ecs.ts"],
|
|
4
|
-
"sourcesContent": ["/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\nimport type { KeyCollection } from './definitions.js';\n\n/**\n * Class for storing keys and values\n */\nexport class KeyMap<TKey, TValue> extends Map<TKey, TValue> {\n\n constructor(entries: any[] = []) {\n super(entries)\n }\n\n firstEntry(): [key: any, value: any] | undefined {\n if (this.size === 0) return [undefined, undefined];\n\n const iterator = this.entries();\n const result = iterator.next();\n return result.value;\n }\n\n firstKey(): TKey | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.keys();\n const result = iterator.next();\n return result.value;\n }\n\n firstValue(): TValue | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.values();\n const result = iterator.next();\n return result.value;\n }\n\n toJSON() {\n return { __KeyMap__: 1, iterable: [...this.entries()] }\n }\n\n toTable(excludeKeys = false): any[] {\n const table = []\n for (const value of this.entries()) {\n let entity = value[1] as { new(): TValue }\n let meta: KeyCollection<any> = {}\n\n if (excludeKeys == false) meta[\"entity.key\"] = value[0]\n meta[\"entity.type\"] = entity.constructor.name\n\n table.push({ ...meta, ...entity })\n }\n return table\n }\n\n /**\n * Outputs keys and values in a tabular format to the console\n */\n printTable(excludeKeys = false): void {\n console.table(this.toTable(excludeKeys))\n }\n\n entries(): MapIterator<any> {\n return super.entries().filter(x => x !== undefined && x !== null)\n }\n\n keys(): MapIterator<any> {\n return super.keys().filter(x => x !== undefined && x !== null)\n }\n\n values(): MapIterator<any> {\n return super.values().filter(x => x !== undefined && x !== null)\n }\n\n // default iterator\n [Symbol.iterator]() {\n return this.values()\n }\n\n static get [Symbol.species]() { return Map; }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * @module ecsjs\n */\nimport {\n Component,\n type ComponentDataMap,\n type ComponentClass,\n type ComponentMap\n} from './definitions.js';\nimport { KeyMap } from './key-map.js'\n\n/**\n * Class for storing entities and their relationships\n * \n * @class EntityMap\n */\nexport class EntityMap {\n\n // entity class maps\n private componentMaps: ComponentMap = new KeyMap()\n\n private nextId: number = 0\n\n constructor() { }\n\n /**\n * Registers a component class\n * \n * @example\n *\n * ```javascript\n * // component class\n * class MyComponent {\n * constructor(x) {\n * this.x = x;\n * }\n * }\n *\n * ecs.register(MyComponent);\n * // or\n * ecs.register(MyComponent1, MyComponent2);\n * ```\n */\n register(...component: ComponentClass[]) {\n for (const definition of component) {\n const componentName = definition.name;\n if (componentName === undefined)\n throw new ReferenceError(\"The component class does not have a name defined. i.e. a constructor name\");\n\n // create the component map\n const componentDataMap: ComponentDataMap = new KeyMap();\n this.componentMaps.set(componentName, componentDataMap);\n }\n\n // chain\n return this;\n }\n\n /**\n * Retrieves an entity map by it's registered type\n */\n getMap(component: ComponentClass) {\n return this.componentMaps.get(component.name);\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.get(entityId, Components.PositionComponent);\n * ```\n */\n get(entityId: number, component: ComponentClass) {\n let cm = this.componentMaps.get(component.name);\n if (cm === undefined)\n throw new ReferenceError(`\"${component}\" component class does not exist in the componentMaps`);\n\n return cm.get(entityId);\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.getByKey(entityId, \"PositionComponent\");\n * ```\n */\n getByKey(entityId: number, componentName: string) {\n // get the component map\n let map = this.componentMaps.get(componentName);\n if (map === undefined)\n throw new ReferenceError(`\"${componentName}\" component does not exist in the componentMaps`);\n\n return map.get(entityId);\n }\n\n /**\n * Checks if the entity exists in it's registed component map\n * The entity type must be registered before calling this method\n * \n * @example\n *\n * ```javascript\n * // get an entity\n * let exists = ecs.has(entityId, PositionComponent);\n * ```\n */\n has(entityId: number, component: ComponentClass): boolean {\n // get the component map\n let map = this.componentMaps.get(component.name);\n if (map === undefined) return false\n\n return map.has(entityId);\n }\n\n /**\n * Adds or updates an entity to it's registered entity map\n *\n * @example\n *\n * ```javascript\n * // add/update an entity\n * let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));\n * ```\n */\n set<T extends Component>(entityId: number, componentData: Component): T {\n // get the component map\n let map = this.componentMaps.get(componentData.constructor.name);\n if (map === undefined)\n throw new ReferenceError(`Component map does not exist using the name: ${componentData.constructor.name}`);\n\n // set the entity on the entity map\n map.set(entityId, componentData);\n\n // return instance\n return componentData as T;\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.remove(entityId, Components.PositionComponent);\n * ```\n */\n remove(entityId: number, component: ComponentClass) {\n return this.removeByKey(entityId, component.name);\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.removeByKey(entityId, \"PositionComponent\");\n * ```\n */\n removeByKey(entityId: number, componentName: string) {\n // get the entity map\n let entityMap = this.componentMaps.get(componentName);\n\n // ensure the map is defined\n if (entityMap === undefined)\n throw new ReferenceError(`A registered entity map does not exist for the given key: ${componentName}`);\n\n // get the entity\n let entity = entityMap.get(entityId);\n if (entity === undefined) return false;\n\n // remove the entity from the entity map\n return entityMap.delete(entityId);\n }\n\n /**\n * Destroys an entity across all component maps\n */\n destroyEntity(entityId: number) {\n this.componentMaps.forEach((dataMap: ComponentDataMap) => {\n if (dataMap.has(entityId)) dataMap.delete(entityId);\n });\n }\n\n // TODO generate a non repeatable id for network play?\n getNextId() {\n this.nextId++;\n return this.nextId;\n }\n\n clear() {\n this.componentMaps.clear();\n return this;\n }\n\n /**\n * Prints all entity maps in a tabular format to the console\n */\n printTable() {\n this.componentMaps.forEach(map => console.table(map.toTable(true)));\n return this\n }\n\n /**\n * Parse's the JSON and returns an EntityMap object\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * const json = JSON.stringfy(ecs);\n * const restoredMap = EntityMap.parse(json);\n * ```\n */\n static parse(json: string) {\n const ecs = new EntityMap();\n\n const restored = JSON.parse(json, function (key: string, value: any) {\n\n if (value.hasOwnProperty('componentMaps')) {\n Reflect.setPrototypeOf(value, EntityMap.prototype);\n return value;\n }\n\n if (value.hasOwnProperty('__KeyMap__')) {\n return new KeyMap(value.iterable);\n }\n\n // TODO pass in user defined entity types ?\n // if (value.hasOwnProperty('uniqueid')) {\n // Reflect.setPrototypeOf(value, ecs.Entity.prototype);\n // return value;\n // }\n\n return this[key];\n });\n\n ecs.componentMaps = restored.componentMaps;\n ecs.nextId = restored.nextId;\n return ecs;\n }\n\n /**\n * A tracing method used for debugging.\n * Intercepts all functions specified and logs each call to the console.\n * \n * @method createWithTracing\n * @static\n * @param {Array} funcFilter A list of function names you want to intercept. If no function names are specified then will log all functions called\n * @return {EntityMap} A new entity map with tracing enabled\n */\n static createWithTracing(funcFilter: any) {\n let traceHandler = {\n get(target: any, propKey: string, receiver: any) {\n const targetValue = target[propKey]\n\n if (typeof targetValue === 'function' && (funcFilter.length === 0 || funcFilter.includes(propKey))) {\n return function (this: any, ...args: any[]) {\n console.groupCollapsed('ecs trace', propKey, args);\n console.trace();\n console.groupEnd();\n return targetValue.apply(this, args);\n }\n }\n\n return targetValue;\n }\n }\n\n return new Proxy(new EntityMap(), traceHandler)\n }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * ### An entity component system library for JavaScript\n *\n * @module ecsjs\n * @main ecsjs\n */\nimport { EntityMap } from './entity-map.js'\n\nexport const ecs = new EntityMap()\n\nif (typeof window !== 'undefined') {\n // @ts-ignore: exports to window\n window.ecs = ecs\n} else if (typeof module !== 'undefined' && module !== null) {\n // exports to nodejs\n module.exports = { ecs };\n}"],
|
|
5
|
-
"mappings": "AAsBO,IAAMA,EAAN,cAAmC,GAAkB,CAE1D,YAAYC,EAAiB,CAAC,EAAG,CAC/B,MAAMA,CAAO,CACf,CAEA,
|
|
6
|
-
"names": ["KeyMap", "entries", "excludeKeys", "table", "value", "entity", "meta", "x", "EntityMap", "_EntityMap", "KeyMap", "component", "definition", "componentName", "componentDataMap", "entityId", "cm", "map", "componentData", "entityMap", "dataMap", "json", "ecs", "restored", "key", "value", "funcFilter", "traceHandler", "target", "propKey", "
|
|
4
|
+
"sourcesContent": ["/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\nimport type { KeyCollection } from './definitions.js';\n\n/**\n * Class for storing keys and values\n */\nexport class KeyMap<TKey, TValue> extends Map<TKey, TValue> {\n\n constructor(entries: any[] = []) {\n super(entries)\n }\n\n firstEntry(): [key: TKey, value: TValue] | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.entries();\n const result = iterator.next();\n return result.value;\n }\n\n firstKey(): TKey | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.keys();\n const result = iterator.next();\n return result.value;\n }\n\n firstValue(): TValue | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.values();\n const result = iterator.next();\n return result.value;\n }\n\n toJSON() {\n return { __KeyMap__: 1, iterable: [...this.entries()] }\n }\n\n toTable(excludeKeys = false): any[] {\n const table = []\n for (const value of this.entries()) {\n let entity = value[1] as { new(): TValue }\n let meta: KeyCollection<any> = {}\n\n if (excludeKeys == false) meta[\"entity.key\"] = value[0]\n meta[\"entity.type\"] = entity.constructor.name\n\n table.push({ ...meta, ...entity })\n }\n return table\n }\n\n /**\n * Outputs keys and values in a tabular format to the console\n */\n printTable(excludeKeys = false): void {\n console.table(this.toTable(excludeKeys))\n }\n\n entries(): MapIterator<any> {\n return super.entries().filter(x => x !== undefined && x !== null)\n }\n\n keys(): MapIterator<any> {\n return super.keys().filter(x => x !== undefined && x !== null)\n }\n\n values(): MapIterator<any> {\n return super.values().filter(x => x !== undefined && x !== null)\n }\n\n // default iterator\n [Symbol.iterator]() {\n return this.values()\n }\n\n static get [Symbol.species]() { return Map; }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * @module ecsjs\n */\nimport {\n type ComponentDataMap,\n type ComponentClass,\n type ComponentMap,\n type IComponent\n} from './definitions.js';\nimport { KeyMap } from './key-map.js'\n\n/**\n * Class for storing entities and their relationships\n * \n * @class EntityMap\n */\nexport class EntityMap {\n\n // entity class maps\n private componentMaps: ComponentMap = new KeyMap()\n\n private nextId: number = 0\n\n constructor() { }\n\n /**\n * Registers a component class\n * \n * @example\n *\n * ```javascript\n * // component class\n * class MyComponent {\n * constructor(x) {\n * this.x = x;\n * }\n * }\n *\n * ecs.register(MyComponent);\n * // or\n * ecs.register(MyComponent1, MyComponent2);\n * ```\n */\n register(...component: ComponentClass<any>[]) {\n for (const definition of component) {\n const componentName = definition.name;\n if (componentName === undefined)\n throw new ReferenceError(\"The component class does not have a name defined. i.e. a constructor name\");\n\n // create the component map\n const componentDataMap: ComponentDataMap<any> = new KeyMap();\n this.componentMaps.set(componentName, componentDataMap);\n }\n\n // chain\n return this;\n }\n\n /**\n * Retrieves an entity map by it's registered type\n */\n getMap<T extends IComponent>(component: ComponentClass<T>): ComponentDataMap<T> | undefined {\n return this.componentMaps.get(component.name);\n }\n\n /**\n * Get the first entity entry for a component class\n * \n * @example\n *\n * ```javascript\n * const [entityId, player] = ecs.firstEntry(PlayerComponent) ?? [];\n * ```\n */\n firstEntry<T extends IComponent>(component: ComponentClass<T>) {\n return this.getMap(component)?.firstEntry();\n }\n\n /**\n * Get the first entity id for a component class\n * \n * @example\n *\n * ```javascript\n * const entityId = ecs.firstKey(PlayerComponent);\n * ```\n */\n firstKey<T extends IComponent>(component: ComponentClass<T>) {\n return this.getMap(component)?.firstKey();\n }\n\n /**\n * Get the first entity component value for a component class\n * \n * @example\n *\n * ```javascript\n * const player = ecs.firstValue(PlayerComponent);\n * ```\n */\n firstValue<T extends IComponent>(component: ComponentClass<T>) {\n return this.getMap(component)?.firstValue();\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.get(entityId, Components.PositionComponent);\n * ```\n */\n get<T extends IComponent>(entityId: number, component: ComponentClass<T>): T {\n let cm = this.componentMaps.get(component.name);\n if (cm === undefined)\n throw new ReferenceError(`\"${component}\" component class does not exist in the componentMaps`);\n\n return cm.get(entityId);\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.getByKey(entityId, \"PositionComponent\");\n * ```\n */\n getByKey(entityId: number, componentName: string) {\n // get the component map\n let map = this.componentMaps.get(componentName);\n if (map === undefined)\n throw new ReferenceError(`\"${componentName}\" component does not exist in the componentMaps`);\n\n return map.get(entityId);\n }\n\n /**\n * Checks if the entity exists in it's registed component map\n * The entity type must be registered before calling this method\n * \n * @example\n *\n * ```javascript\n * // get an entity\n * let exists = ecs.has(entityId, PositionComponent);\n * ```\n */\n has<T extends IComponent>(entityId: number, component: ComponentClass<T>): boolean {\n // get the component map\n let map = this.componentMaps.get(component.name);\n if (map === undefined) return false\n\n return map.has(entityId);\n }\n\n /**\n * Adds or updates an entity to it's registered entity map\n *\n * @example\n *\n * ```javascript\n * // add/update an entity\n * let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));\n * ```\n */\n set<T extends IComponent>(entityId: number, componentData: T): T {\n // get the component map\n let map = this.componentMaps.get(componentData.constructor.name);\n if (map === undefined)\n throw new ReferenceError(`Component map does not exist using the name: ${componentData.constructor.name}`);\n\n // set the entity on the entity map\n map.set(entityId, componentData);\n\n // return instance\n return componentData as T;\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.remove(entityId, Components.PositionComponent);\n * ```\n */\n remove<T extends IComponent>(entityId: number, component: ComponentClass<T>) {\n return this.removeByKey(entityId, component.name);\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.removeByKey(entityId, \"PositionComponent\");\n * ```\n */\n removeByKey(entityId: number, componentName: string) {\n // get the entity map\n let entityMap = this.componentMaps.get(componentName);\n\n // ensure the map is defined\n if (entityMap === undefined)\n throw new ReferenceError(`A registered entity map does not exist for the given key: ${componentName}`);\n\n // get the entity\n let entity = entityMap.get(entityId);\n if (entity === undefined) return false;\n\n // remove the entity from the entity map\n return entityMap.delete(entityId);\n }\n\n /**\n * Destroys an entity across all component maps\n */\n destroyEntity(entityId: number) {\n this.componentMaps.forEach((dataMap: ComponentDataMap<any>) => {\n if (dataMap.has(entityId)) dataMap.delete(entityId);\n });\n }\n\n // TODO generate a non repeatable id for network play?\n getNextId() {\n this.nextId++;\n return this.nextId;\n }\n\n clear() {\n this.componentMaps.clear();\n return this;\n }\n\n /**\n * Prints all entity maps in a tabular format to the console\n */\n printTable() {\n this.componentMaps.forEach(map => console.table(map.toTable(true)));\n return this\n }\n\n /**\n * Parse's the JSON and returns an EntityMap object\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * const json = JSON.stringfy(ecs);\n * const restoredMap = EntityMap.parse(json);\n * ```\n */\n static parse(json: string) {\n const ecs = new EntityMap();\n\n const restored = JSON.parse(json, function (key: string, value: any) {\n\n if (value.hasOwnProperty('componentMaps')) {\n Reflect.setPrototypeOf(value, EntityMap.prototype);\n return value;\n }\n\n if (value.hasOwnProperty('__KeyMap__')) {\n return new KeyMap(value.iterable);\n }\n\n return this[key];\n });\n\n ecs.componentMaps = restored.componentMaps;\n ecs.nextId = restored.nextId;\n return ecs;\n }\n\n /**\n * A tracing method used for debugging.\n * Intercepts all functions specified and logs each call to the console.\n * \n * @method createWithTracing\n * @static\n * @param {Array} funcFilter A list of function names you want to intercept. If no function names are specified then will log all functions called\n * @return {EntityMap} A new entity map with tracing enabled\n */\n static createWithTracing(funcFilter: any) {\n let traceHandler = {\n get(target: any, propKey: string) {\n const targetValue = target[propKey]\n\n if (typeof targetValue === 'function' && (funcFilter.length === 0 || funcFilter.includes(propKey))) {\n return function (this: any, ...args: any[]) {\n console.groupCollapsed('ecs trace', propKey, args);\n console.trace();\n console.groupEnd();\n return targetValue.apply(this, args);\n }\n }\n\n return targetValue;\n }\n }\n\n return new Proxy(new EntityMap(), traceHandler)\n }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * ### An entity component system library for JavaScript\n *\n * @module ecsjs\n * @main ecsjs\n */\nimport { EntityMap } from './entity-map.js'\n\nexport const ecs = new EntityMap()\n\nif (typeof window !== 'undefined') {\n // @ts-ignore: exports to window\n window.ecs = ecs\n} else if (typeof module !== 'undefined' && module !== null) {\n // exports to nodejs\n module.exports = { ecs };\n}"],
|
|
5
|
+
"mappings": "AAsBO,IAAMA,EAAN,cAAmC,GAAkB,CAE1D,YAAYC,EAAiB,CAAC,EAAG,CAC/B,MAAMA,CAAO,CACf,CAEA,YAAqD,CACnD,OAAI,KAAK,OAAS,EAAG,OAEJ,KAAK,QAAQ,EACN,KAAK,EACf,KAChB,CAEA,UAA6B,CAC3B,OAAI,KAAK,OAAS,EAAG,OAEJ,KAAK,KAAK,EACH,KAAK,EACf,KAChB,CAEA,YAAiC,CAC/B,OAAI,KAAK,OAAS,EAAG,OAEJ,KAAK,OAAO,EACL,KAAK,EACf,KAChB,CAEA,QAAS,CACP,MAAO,CAAE,WAAY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ityMap", "_EntityMap", "KeyMap", "component", "definition", "componentName", "componentDataMap", "entityId", "cm", "map", "componentData", "entityMap", "dataMap", "json", "ecs", "restored", "key", "value", "funcFilter", "traceHandler", "target", "propKey", "targetValue", "args", "ecs", "EntityMap"]
|
|
7
7
|
}
|
|
@@ -2,11 +2,11 @@ import type { KeyMap } from './key-map.js';
|
|
|
2
2
|
export type KeyCollection<T> = {
|
|
3
3
|
[key: string]: T;
|
|
4
4
|
};
|
|
5
|
-
export
|
|
5
|
+
export interface IComponent {
|
|
6
6
|
}
|
|
7
|
-
export type ComponentClass = {
|
|
8
|
-
new (...args: any[]):
|
|
7
|
+
export type ComponentClass<T extends IComponent> = {
|
|
8
|
+
new (...args: any[]): T;
|
|
9
9
|
};
|
|
10
|
-
export type ComponentDataMap = KeyMap<number,
|
|
11
|
-
export type ComponentMap = KeyMap<string, ComponentDataMap
|
|
10
|
+
export type ComponentDataMap<T> = KeyMap<number, T>;
|
|
11
|
+
export type ComponentMap = KeyMap<string, ComponentDataMap<any>>;
|
|
12
12
|
//# sourceMappingURL=definitions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definitions.d.ts","sourceRoot":"","sources":["../../../src/definitions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAE1C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,CAAA;CAAE,CAAA;AAEnD,
|
|
1
|
+
{"version":3,"file":"definitions.d.ts","sourceRoot":"","sources":["../../../src/definitions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAE1C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,CAAA;CAAE,CAAA;AAEnD,MAAM,WAAW,UAAU;CAAI;AAE/B,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,UAAU,IAAI;IACjD,KAAI,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;CACvB,CAAA;AAED,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;AAEnD,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAA"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module ecsjs
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { type ComponentDataMap, type ComponentClass, type IComponent } from './definitions.js';
|
|
5
5
|
/**
|
|
6
6
|
* Class for storing entities and their relationships
|
|
7
7
|
*
|
|
@@ -29,11 +29,41 @@ export declare class EntityMap {
|
|
|
29
29
|
* ecs.register(MyComponent1, MyComponent2);
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
register(...component: ComponentClass[]): this;
|
|
32
|
+
register(...component: ComponentClass<any>[]): this;
|
|
33
33
|
/**
|
|
34
34
|
* Retrieves an entity map by it's registered type
|
|
35
35
|
*/
|
|
36
|
-
getMap(component: ComponentClass): ComponentDataMap | undefined;
|
|
36
|
+
getMap<T extends IComponent>(component: ComponentClass<T>): ComponentDataMap<T> | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Get the first entity entry for a component class
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
*
|
|
42
|
+
* ```javascript
|
|
43
|
+
* const [entityId, player] = ecs.firstEntry(PlayerComponent) ?? [];
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
firstEntry<T extends IComponent>(component: ComponentClass<T>): [key: number, value: T] | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Get the first entity id for a component class
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
*
|
|
52
|
+
* ```javascript
|
|
53
|
+
* const entityId = ecs.firstKey(PlayerComponent);
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
firstKey<T extends IComponent>(component: ComponentClass<T>): number | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Get the first entity component value for a component class
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
*
|
|
62
|
+
* ```javascript
|
|
63
|
+
* const player = ecs.firstValue(PlayerComponent);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
firstValue<T extends IComponent>(component: ComponentClass<T>): T | undefined;
|
|
37
67
|
/**
|
|
38
68
|
* Retrieves an entity from its registered entity map
|
|
39
69
|
*
|
|
@@ -44,7 +74,7 @@ export declare class EntityMap {
|
|
|
44
74
|
* let positionData = ecs.get(entityId, Components.PositionComponent);
|
|
45
75
|
* ```
|
|
46
76
|
*/
|
|
47
|
-
get(entityId: number, component: ComponentClass):
|
|
77
|
+
get<T extends IComponent>(entityId: number, component: ComponentClass<T>): T;
|
|
48
78
|
/**
|
|
49
79
|
* Retrieves an entity from its registered entity map
|
|
50
80
|
*
|
|
@@ -55,7 +85,7 @@ export declare class EntityMap {
|
|
|
55
85
|
* let positionData = ecs.getByKey(entityId, "PositionComponent");
|
|
56
86
|
* ```
|
|
57
87
|
*/
|
|
58
|
-
getByKey(entityId: number, componentName: string):
|
|
88
|
+
getByKey(entityId: number, componentName: string): any;
|
|
59
89
|
/**
|
|
60
90
|
* Checks if the entity exists in it's registed component map
|
|
61
91
|
* The entity type must be registered before calling this method
|
|
@@ -67,7 +97,7 @@ export declare class EntityMap {
|
|
|
67
97
|
* let exists = ecs.has(entityId, PositionComponent);
|
|
68
98
|
* ```
|
|
69
99
|
*/
|
|
70
|
-
has(entityId: number, component: ComponentClass): boolean;
|
|
100
|
+
has<T extends IComponent>(entityId: number, component: ComponentClass<T>): boolean;
|
|
71
101
|
/**
|
|
72
102
|
* Adds or updates an entity to it's registered entity map
|
|
73
103
|
*
|
|
@@ -78,7 +108,7 @@ export declare class EntityMap {
|
|
|
78
108
|
* let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));
|
|
79
109
|
* ```
|
|
80
110
|
*/
|
|
81
|
-
set<T extends
|
|
111
|
+
set<T extends IComponent>(entityId: number, componentData: T): T;
|
|
82
112
|
/**
|
|
83
113
|
* Removes an entity from its entity map.
|
|
84
114
|
* Removes any related Node entities that are mapped to the entity
|
|
@@ -90,7 +120,7 @@ export declare class EntityMap {
|
|
|
90
120
|
* EntityMap.remove(entityId, Components.PositionComponent);
|
|
91
121
|
* ```
|
|
92
122
|
*/
|
|
93
|
-
remove(entityId: number, component: ComponentClass): boolean;
|
|
123
|
+
remove<T extends IComponent>(entityId: number, component: ComponentClass<T>): boolean;
|
|
94
124
|
/**
|
|
95
125
|
* Removes an entity from its entity map.
|
|
96
126
|
* Removes any related Node entities that are mapped to the entity
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entity-map.d.ts","sourceRoot":"","sources":["../../../src/entity-map.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"entity-map.d.ts","sourceRoot":"","sources":["../../../src/entity-map.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,cAAc,EAEnB,KAAK,UAAU,EAChB,MAAM,kBAAkB,CAAC;AAG1B;;;;GAIG;AACH,qBAAa,SAAS;IAGpB,OAAO,CAAC,aAAa,CAA6B;IAElD,OAAO,CAAC,MAAM,CAAY;;IAI1B;;;;;;;;;;;;;;;;;OAiBG;IACH,QAAQ,CAAC,GAAG,SAAS,EAAE,cAAc,CAAC,GAAG,CAAC,EAAE;IAe5C;;OAEG;IACH,MAAM,CAAC,CAAC,SAAS,UAAU,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS;IAI3F;;;;;;;;OAQG;IACH,UAAU,CAAC,CAAC,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
|
|
4
4
|
export declare class KeyMap<TKey, TValue> extends Map<TKey, TValue> {
|
|
5
5
|
constructor(entries?: any[]);
|
|
6
|
-
firstEntry(): [key:
|
|
6
|
+
firstEntry(): [key: TKey, value: TValue] | undefined;
|
|
7
7
|
firstKey(): TKey | undefined;
|
|
8
8
|
firstValue(): TValue | undefined;
|
|
9
9
|
toJSON(): {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-map.d.ts","sourceRoot":"","sources":["../../../src/key-map.ts"],"names":[],"mappings":"AAmBA;;GAEG;AACH,qBAAa,MAAM,CAAC,IAAI,EAAE,MAAM,CAAE,SAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC;gBAE7C,OAAO,GAAE,GAAG,EAAO;IAI/B,UAAU,IAAI,CAAC,GAAG,EAAE,
|
|
1
|
+
{"version":3,"file":"key-map.d.ts","sourceRoot":"","sources":["../../../src/key-map.ts"],"names":[],"mappings":"AAmBA;;GAEG;AACH,qBAAa,MAAM,CAAC,IAAI,EAAE,MAAM,CAAE,SAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC;gBAE7C,OAAO,GAAE,GAAG,EAAO;IAI/B,UAAU,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,SAAS;IAQpD,QAAQ,IAAI,IAAI,GAAG,SAAS;IAQ5B,UAAU,IAAI,MAAM,GAAG,SAAS;IAQhC,MAAM;;;;IAIN,OAAO,CAAC,WAAW,UAAQ,GAAG,GAAG,EAAE;IAcnC;;OAEG;IACH,UAAU,CAAC,WAAW,UAAQ,GAAG,IAAI;IAIrC,OAAO,IAAI,WAAW,CAAC,GAAG,CAAC;IAI3B,IAAI,IAAI,WAAW,CAAC,GAAG,CAAC;IAIxB,MAAM,IAAI,WAAW,CAAC,GAAG,CAAC;IAK1B,CAAC,MAAM,CAAC,QAAQ,CAAC;IAIjB,MAAM,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,mBAAkB;CAE9C"}
|
package/package.json
CHANGED
package/src/definitions.ts
CHANGED
|
@@ -2,12 +2,12 @@ import type { KeyMap } from './key-map.js'
|
|
|
2
2
|
|
|
3
3
|
export type KeyCollection<T> = { [key: string]: T }
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export interface IComponent { }
|
|
6
6
|
|
|
7
|
-
export type ComponentClass = {
|
|
8
|
-
new(...args: any[]):
|
|
7
|
+
export type ComponentClass<T extends IComponent> = {
|
|
8
|
+
new(...args: any[]): T
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export type ComponentDataMap = KeyMap<number,
|
|
11
|
+
export type ComponentDataMap<T> = KeyMap<number, T>
|
|
12
12
|
|
|
13
|
-
export type ComponentMap = KeyMap<string, ComponentDataMap
|
|
13
|
+
export type ComponentMap = KeyMap<string, ComponentDataMap<any>>
|
package/src/entity-map.ts
CHANGED
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
* @module ecsjs
|
|
21
21
|
*/
|
|
22
22
|
import {
|
|
23
|
-
Component,
|
|
24
23
|
type ComponentDataMap,
|
|
25
24
|
type ComponentClass,
|
|
26
|
-
type ComponentMap
|
|
25
|
+
type ComponentMap,
|
|
26
|
+
type IComponent
|
|
27
27
|
} from './definitions.js';
|
|
28
28
|
import { KeyMap } from './key-map.js'
|
|
29
29
|
|
|
@@ -59,14 +59,14 @@ export class EntityMap {
|
|
|
59
59
|
* ecs.register(MyComponent1, MyComponent2);
|
|
60
60
|
* ```
|
|
61
61
|
*/
|
|
62
|
-
register(...component: ComponentClass[]) {
|
|
62
|
+
register(...component: ComponentClass<any>[]) {
|
|
63
63
|
for (const definition of component) {
|
|
64
64
|
const componentName = definition.name;
|
|
65
65
|
if (componentName === undefined)
|
|
66
66
|
throw new ReferenceError("The component class does not have a name defined. i.e. a constructor name");
|
|
67
67
|
|
|
68
68
|
// create the component map
|
|
69
|
-
const componentDataMap: ComponentDataMap = new KeyMap();
|
|
69
|
+
const componentDataMap: ComponentDataMap<any> = new KeyMap();
|
|
70
70
|
this.componentMaps.set(componentName, componentDataMap);
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -77,10 +77,49 @@ export class EntityMap {
|
|
|
77
77
|
/**
|
|
78
78
|
* Retrieves an entity map by it's registered type
|
|
79
79
|
*/
|
|
80
|
-
getMap(component: ComponentClass) {
|
|
80
|
+
getMap<T extends IComponent>(component: ComponentClass<T>): ComponentDataMap<T> | undefined {
|
|
81
81
|
return this.componentMaps.get(component.name);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Get the first entity entry for a component class
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
*
|
|
89
|
+
* ```javascript
|
|
90
|
+
* const [entityId, player] = ecs.firstEntry(PlayerComponent) ?? [];
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
firstEntry<T extends IComponent>(component: ComponentClass<T>) {
|
|
94
|
+
return this.getMap(component)?.firstEntry();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Get the first entity id for a component class
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
*
|
|
102
|
+
* ```javascript
|
|
103
|
+
* const entityId = ecs.firstKey(PlayerComponent);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
firstKey<T extends IComponent>(component: ComponentClass<T>) {
|
|
107
|
+
return this.getMap(component)?.firstKey();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Get the first entity component value for a component class
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
*
|
|
115
|
+
* ```javascript
|
|
116
|
+
* const player = ecs.firstValue(PlayerComponent);
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
firstValue<T extends IComponent>(component: ComponentClass<T>) {
|
|
120
|
+
return this.getMap(component)?.firstValue();
|
|
121
|
+
}
|
|
122
|
+
|
|
84
123
|
/**
|
|
85
124
|
* Retrieves an entity from its registered entity map
|
|
86
125
|
*
|
|
@@ -91,7 +130,7 @@ export class EntityMap {
|
|
|
91
130
|
* let positionData = ecs.get(entityId, Components.PositionComponent);
|
|
92
131
|
* ```
|
|
93
132
|
*/
|
|
94
|
-
get(entityId: number, component: ComponentClass) {
|
|
133
|
+
get<T extends IComponent>(entityId: number, component: ComponentClass<T>): T {
|
|
95
134
|
let cm = this.componentMaps.get(component.name);
|
|
96
135
|
if (cm === undefined)
|
|
97
136
|
throw new ReferenceError(`"${component}" component class does not exist in the componentMaps`);
|
|
@@ -129,7 +168,7 @@ export class EntityMap {
|
|
|
129
168
|
* let exists = ecs.has(entityId, PositionComponent);
|
|
130
169
|
* ```
|
|
131
170
|
*/
|
|
132
|
-
has(entityId: number, component: ComponentClass): boolean {
|
|
171
|
+
has<T extends IComponent>(entityId: number, component: ComponentClass<T>): boolean {
|
|
133
172
|
// get the component map
|
|
134
173
|
let map = this.componentMaps.get(component.name);
|
|
135
174
|
if (map === undefined) return false
|
|
@@ -147,7 +186,7 @@ export class EntityMap {
|
|
|
147
186
|
* let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));
|
|
148
187
|
* ```
|
|
149
188
|
*/
|
|
150
|
-
set<T extends
|
|
189
|
+
set<T extends IComponent>(entityId: number, componentData: T): T {
|
|
151
190
|
// get the component map
|
|
152
191
|
let map = this.componentMaps.get(componentData.constructor.name);
|
|
153
192
|
if (map === undefined)
|
|
@@ -171,7 +210,7 @@ export class EntityMap {
|
|
|
171
210
|
* EntityMap.remove(entityId, Components.PositionComponent);
|
|
172
211
|
* ```
|
|
173
212
|
*/
|
|
174
|
-
remove(entityId: number, component: ComponentClass) {
|
|
213
|
+
remove<T extends IComponent>(entityId: number, component: ComponentClass<T>) {
|
|
175
214
|
return this.removeByKey(entityId, component.name);
|
|
176
215
|
}
|
|
177
216
|
|
|
@@ -206,7 +245,7 @@ export class EntityMap {
|
|
|
206
245
|
* Destroys an entity across all component maps
|
|
207
246
|
*/
|
|
208
247
|
destroyEntity(entityId: number) {
|
|
209
|
-
this.componentMaps.forEach((dataMap: ComponentDataMap) => {
|
|
248
|
+
this.componentMaps.forEach((dataMap: ComponentDataMap<any>) => {
|
|
210
249
|
if (dataMap.has(entityId)) dataMap.delete(entityId);
|
|
211
250
|
});
|
|
212
251
|
}
|
|
@@ -255,12 +294,6 @@ export class EntityMap {
|
|
|
255
294
|
return new KeyMap(value.iterable);
|
|
256
295
|
}
|
|
257
296
|
|
|
258
|
-
// TODO pass in user defined entity types ?
|
|
259
|
-
// if (value.hasOwnProperty('uniqueid')) {
|
|
260
|
-
// Reflect.setPrototypeOf(value, ecs.Entity.prototype);
|
|
261
|
-
// return value;
|
|
262
|
-
// }
|
|
263
|
-
|
|
264
297
|
return this[key];
|
|
265
298
|
});
|
|
266
299
|
|
|
@@ -280,7 +313,7 @@ export class EntityMap {
|
|
|
280
313
|
*/
|
|
281
314
|
static createWithTracing(funcFilter: any) {
|
|
282
315
|
let traceHandler = {
|
|
283
|
-
get(target: any, propKey: string
|
|
316
|
+
get(target: any, propKey: string) {
|
|
284
317
|
const targetValue = target[propKey]
|
|
285
318
|
|
|
286
319
|
if (typeof targetValue === 'function' && (funcFilter.length === 0 || funcFilter.includes(propKey))) {
|
package/src/key-map.ts
CHANGED
|
@@ -26,8 +26,8 @@ export class KeyMap<TKey, TValue> extends Map<TKey, TValue> {
|
|
|
26
26
|
super(entries)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
firstEntry(): [key:
|
|
30
|
-
if (this.size === 0) return
|
|
29
|
+
firstEntry(): [key: TKey, value: TValue] | undefined {
|
|
30
|
+
if (this.size === 0) return undefined;
|
|
31
31
|
|
|
32
32
|
const iterator = this.entries();
|
|
33
33
|
const result = iterator.next();
|