ecsjs 1.0.0-beta.6 → 1.0.0-beta.8
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/dist/ecs.js +1 -1
- package/dist/ecs.js.map +3 -3
- package/dist/types/src/definitions.d.ts +1 -1
- package/dist/types/src/definitions.d.ts.map +1 -1
- package/dist/types/src/entity-map.d.ts +41 -9
- package/dist/types/src/entity-map.d.ts.map +1 -1
- package/dist/types/src/key-map.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/definitions.ts +1 -1
- package/src/entity-map.ts +88 -20
- package/src/key-map.ts +4 -6
package/dist/ecs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var a=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,r]of this.entries()){let o=r,s={};e==!1&&(s["entity.key"]=n),s["entity.type"]=o.constructor.name,t.push({...s,...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)}static get[Symbol.species](){return Map}};var p=class i{componentMaps=new a;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 r=new a;this.componentMaps.set(n,r)}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)}getValues(e,...t){return t.map(n=>this.get(e,n))}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}setValues(e,...t){return t.map(n=>this.set(e,n))}remove(e,...t){for(let n of t)this.removeByKey(e,n.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){for(let t of this.componentMaps.values())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}printEntity(e){for(let t of this.componentMaps.values()){if(t.has(e)===!1)continue;let n=t.get(e),r={name:n.constructor.name,...n};console.table({[e]:r})}return this}static parse(e){let t=new i,n=JSON.parse(e,function(r,o){return o.hasOwnProperty("componentMaps")?(Reflect.setPrototypeOf(o,i.prototype),o):o.hasOwnProperty("__KeyMap__")?new a(o.iterable):this[r]});return t.componentMaps=n.componentMaps,t.nextId=n.nextId,t}static createWithTracing(e){let t={get(n,r){let o=n[r];return typeof o=="function"&&(e.length===0||e.includes(r))?function(...s){return console.groupCollapsed("ecs trace",r,s),console.trace(),console.groupEnd(),o.apply(this,s)}:o}};return new Proxy(new i,t)}};var u=new p;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: 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<[TKey, TValue]> {\n return super.entries().filter(x => x !== undefined && x !== null)\n }\n\n keys(): MapIterator<TKey> {\n return super.keys().filter(x => x !== undefined && x !== null)\n }\n\n values(): MapIterator<TValue> {\n return super.values().filter(x => x !== undefined && x !== null)\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,EAAG,SAAU,CAAC,GAAG,KAAK,QAAQ,CAAC,CAAE,CACxD,CAEA,QAAQC,EAAc,GAAc,CAClC,IAAMC,EAAQ,CAAC,EACf,
|
|
6
|
-
"names": ["KeyMap", "entries", "excludeKeys", "table", "value", "entity", "meta", "x", "EntityMap", "_EntityMap", "KeyMap", "component", "definition", "componentName", "componentDataMap", "entityId", "
|
|
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 [key, value] of this.entries()) {\n const entity = value as { new(): TValue }\n const meta: KeyCollection<any> = {}\n if (excludeKeys == false) meta[\"entity.key\"] = key\n meta[\"entity.type\"] = entity.constructor.name\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<[TKey, TValue]> {\n return super.entries().filter(x => x !== undefined && x !== null)\n }\n\n keys(): MapIterator<TKey> {\n return super.keys().filter(x => x !== undefined && x !== null)\n }\n\n values(): MapIterator<TValue> {\n return super.values().filter(x => x !== undefined && x !== null)\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 ComponentClass,\n type ComponentDataMap,\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>(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>(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>(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>(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>(entityId: number, component: ComponentClass<T>): T {\n const map = this.componentMaps.get(component.name);\n if (map === undefined)\n throw new ReferenceError(`\"${component}\" component class does not exist in the componentMaps`);\n\n return map.get(entityId);\n }\n\n /**\n * Get multiple component values related to an entity\n * \n * @param entityId\n * @param components: ComponentClass<any>[]\n * \n * @example\n *\n * ```javascript\n * // get an entity\n * const [player, position] = ecs.getValues(entityId, PlayerComponent, PositionComponent);\n * ```\n */\n getValues<T1, T2, T3, T4, T5, T6, T7, T8>(\n entityId: number,\n c1: ComponentClass<T1>,\n c2: ComponentClass<T2>,\n c3?: ComponentClass<T3>,\n c4?: ComponentClass<T4>,\n c5?: ComponentClass<T5>,\n c6?: ComponentClass<T6>,\n c7?: ComponentClass<T7>,\n c8?: ComponentClass<T8>\n ): [T1, T2, T3, T4, T5, T6, T7, T8];\n getValues<T>(entityId: number, ...components: ComponentClass<T>[]): T[] {\n return components.map(x => this.get(entityId, x))\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>(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(\n `Component map does not exist using the name: ${componentData.constructor.name}`\n );\n\n // set the entity on the entity map\n map.set(entityId, componentData);\n\n // return instance\n return componentData;\n }\n\n /**\n * Set multiple component values for an entity\n * \n * @param entityId\n * @param components: ComponentClass<any>[]\n * \n * @example\n *\n * ```javascript\n * // write the component values to the entity\n * ecs.setValues(entityId, new PlayerComponent(), new PositionComponent());\n * ```\n */\n setValues<T extends IComponent[]>(entityId: number, ...args: [...T]): [...T];\n setValues(entityId: number, ...components: IComponent[]): IComponent[] {\n return components.map(x => this.set(entityId, x))\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>(entityId: number, ...components: ComponentClass<T>[]) {\n for (const component of components) {\n this.removeByKey(entityId, component.name)\n }\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 for (const map of this.componentMaps.values()) {\n if (map.has(entityId)) map.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 component 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 * Prints an entity component data in tabular format to the console\n */\n printEntity(entityId: number) {\n for (const map of this.componentMaps.values()) {\n if (map.has(entityId) === false) continue;\n\n const data = map.get(entityId);\n const columns = {\n name: data.constructor.name,\n ...data\n };\n console.table({ [entityId]: columns });\n }\n\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,YAA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LA,EAAM,eAAe,YAAY,EAC5B,IAAIjB,EAAOiB,EAAM,QAAQ,EAG3B,KAAKD,CAAG,CACjB,CAAC,EAED,OAAAF,EAAI,cAAgBC,EAAS,cAC7BD,EAAI,OAASC,EAAS,OACfD,CACT,CAWA,OAAO,kBAAkBI,EAAiB,CACxC,IAAIC,EAAe,CACjB,IAAIC,EAAaC,EAAiB,CAChC,IAAMC,EAAcF,EAAOC,CAAO,EAElC,OAAI,OAAOC,GAAgB,aAAeJ,EAAW,SAAW,GAAKA,EAAW,SAASG,CAAO,GACvF,YAAwBE,EAAa,CAC1C,eAAQ,eAAe,YAAaF,EAASE,CAAI,EACjD,QAAQ,MAAM,EACd,QAAQ,SAAS,EACVD,EAAY,MAAM,KAAMC,CAAI,CACrC,EAGKD,CACT,CACF,EAEA,OAAO,IAAI,MAAM,IAAIvB,EAAaoB,CAAY,CAChD,CAEF,ECxXO,IAAMK,EAAM,IAAIC,EAEnB,OAAO,OAAW,IAEpB,OAAO,IAAMD,EACJ,OAAO,OAAW,KAAe,SAAW,OAErD,OAAO,QAAU,CAAE,IAAAA,CAAI",
|
|
6
|
+
"names": ["KeyMap", "entries", "excludeKeys", "table", "key", "value", "entity", "meta", "x", "EntityMap", "_EntityMap", "KeyMap", "component", "definition", "componentName", "componentDataMap", "entityId", "map", "components", "x", "componentData", "entityMap", "data", "columns", "json", "ecs", "restored", "key", "value", "funcFilter", "traceHandler", "target", "propKey", "targetValue", "args", "ecs", "EntityMap"]
|
|
7
7
|
}
|
|
@@ -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,MAAM,WAAW,UAAU;CAAI;AAE/B,MAAM,MAAM,cAAc,CAAC,CAAC,
|
|
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,IAAI;IAC9B,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 { type
|
|
4
|
+
import { type ComponentClass, type ComponentDataMap, type IComponent } from './definitions.js';
|
|
5
5
|
/**
|
|
6
6
|
* Class for storing entities and their relationships
|
|
7
7
|
*
|
|
@@ -33,7 +33,7 @@ export declare class EntityMap {
|
|
|
33
33
|
/**
|
|
34
34
|
* Retrieves an entity map by it's registered type
|
|
35
35
|
*/
|
|
36
|
-
getMap<T
|
|
36
|
+
getMap<T>(component: ComponentClass<T>): ComponentDataMap<T> | undefined;
|
|
37
37
|
/**
|
|
38
38
|
* Get the first entity entry for a component class
|
|
39
39
|
*
|
|
@@ -43,7 +43,7 @@ export declare class EntityMap {
|
|
|
43
43
|
* const [entityId, player] = ecs.firstEntry(PlayerComponent) ?? [];
|
|
44
44
|
* ```
|
|
45
45
|
*/
|
|
46
|
-
firstEntry<T
|
|
46
|
+
firstEntry<T>(component: ComponentClass<T>): [key: number, value: T] | undefined;
|
|
47
47
|
/**
|
|
48
48
|
* Get the first entity id for a component class
|
|
49
49
|
*
|
|
@@ -53,7 +53,7 @@ export declare class EntityMap {
|
|
|
53
53
|
* const entityId = ecs.firstKey(PlayerComponent);
|
|
54
54
|
* ```
|
|
55
55
|
*/
|
|
56
|
-
firstKey<T
|
|
56
|
+
firstKey<T>(component: ComponentClass<T>): number | undefined;
|
|
57
57
|
/**
|
|
58
58
|
* Get the first entity component value for a component class
|
|
59
59
|
*
|
|
@@ -63,7 +63,7 @@ export declare class EntityMap {
|
|
|
63
63
|
* const player = ecs.firstValue(PlayerComponent);
|
|
64
64
|
* ```
|
|
65
65
|
*/
|
|
66
|
-
firstValue<T
|
|
66
|
+
firstValue<T>(component: ComponentClass<T>): T | undefined;
|
|
67
67
|
/**
|
|
68
68
|
* Retrieves an entity from its registered entity map
|
|
69
69
|
*
|
|
@@ -74,7 +74,21 @@ export declare class EntityMap {
|
|
|
74
74
|
* let positionData = ecs.get(entityId, Components.PositionComponent);
|
|
75
75
|
* ```
|
|
76
76
|
*/
|
|
77
|
-
get<T
|
|
77
|
+
get<T>(entityId: number, component: ComponentClass<T>): T;
|
|
78
|
+
/**
|
|
79
|
+
* Get multiple component values related to an entity
|
|
80
|
+
*
|
|
81
|
+
* @param entityId
|
|
82
|
+
* @param components: ComponentClass<any>[]
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
*
|
|
86
|
+
* ```javascript
|
|
87
|
+
* // get an entity
|
|
88
|
+
* const [player, position] = ecs.getValues(entityId, PlayerComponent, PositionComponent);
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
getValues<T1, T2, T3, T4, T5, T6, T7, T8>(entityId: number, c1: ComponentClass<T1>, c2: ComponentClass<T2>, c3?: ComponentClass<T3>, c4?: ComponentClass<T4>, c5?: ComponentClass<T5>, c6?: ComponentClass<T6>, c7?: ComponentClass<T7>, c8?: ComponentClass<T8>): [T1, T2, T3, T4, T5, T6, T7, T8];
|
|
78
92
|
/**
|
|
79
93
|
* Retrieves an entity from its registered entity map
|
|
80
94
|
*
|
|
@@ -97,7 +111,7 @@ export declare class EntityMap {
|
|
|
97
111
|
* let exists = ecs.has(entityId, PositionComponent);
|
|
98
112
|
* ```
|
|
99
113
|
*/
|
|
100
|
-
has<T
|
|
114
|
+
has<T>(entityId: number, component: ComponentClass<T>): boolean;
|
|
101
115
|
/**
|
|
102
116
|
* Adds or updates an entity to it's registered entity map
|
|
103
117
|
*
|
|
@@ -109,6 +123,20 @@ export declare class EntityMap {
|
|
|
109
123
|
* ```
|
|
110
124
|
*/
|
|
111
125
|
set<T extends IComponent>(entityId: number, componentData: T): T;
|
|
126
|
+
/**
|
|
127
|
+
* Set multiple component values for an entity
|
|
128
|
+
*
|
|
129
|
+
* @param entityId
|
|
130
|
+
* @param components: ComponentClass<any>[]
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
*
|
|
134
|
+
* ```javascript
|
|
135
|
+
* // write the component values to the entity
|
|
136
|
+
* ecs.setValues(entityId, new PlayerComponent(), new PositionComponent());
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
setValues<T extends IComponent[]>(entityId: number, ...args: [...T]): [...T];
|
|
112
140
|
/**
|
|
113
141
|
* Removes an entity from its entity map.
|
|
114
142
|
* Removes any related Node entities that are mapped to the entity
|
|
@@ -120,7 +148,7 @@ export declare class EntityMap {
|
|
|
120
148
|
* EntityMap.remove(entityId, Components.PositionComponent);
|
|
121
149
|
* ```
|
|
122
150
|
*/
|
|
123
|
-
remove<T
|
|
151
|
+
remove<T>(entityId: number, ...components: ComponentClass<T>[]): void;
|
|
124
152
|
/**
|
|
125
153
|
* Removes an entity from its entity map.
|
|
126
154
|
* Removes any related Node entities that are mapped to the entity
|
|
@@ -140,9 +168,13 @@ export declare class EntityMap {
|
|
|
140
168
|
getNextId(): number;
|
|
141
169
|
clear(): this;
|
|
142
170
|
/**
|
|
143
|
-
* Prints all
|
|
171
|
+
* Prints all component maps in a tabular format to the console
|
|
144
172
|
*/
|
|
145
173
|
printTable(): this;
|
|
174
|
+
/**
|
|
175
|
+
* Prints an entity component data in tabular format to the console
|
|
176
|
+
*/
|
|
177
|
+
printEntity(entityId: number): this;
|
|
146
178
|
/**
|
|
147
179
|
* Parse's the JSON and returns an EntityMap object
|
|
148
180
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entity-map.d.ts","sourceRoot":"","sources":["../../../src/entity-map.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,OAAO,EACL,KAAK,
|
|
1
|
+
{"version":3,"file":"entity-map.d.ts","sourceRoot":"","sources":["../../../src/entity-map.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAErB,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,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS;IAIxE;;;;;;;;OAQG;IACH,UAAU,CAAC,CAAC,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC;IAI1C;;;;;;;;OAQG;IACH,QAAQ,CAAC,CAAC,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC;IAIxC;;;;;;;;OAQG;IACH,UAAU,CAAC,CAAC,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC;IAI1C;;;;;;;;;OASG;IACH,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc,CAA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AM,EAAE,GAAG,UAAU,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE;IAM9D;;;;;;;;;;OAUG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM;IAgBnD;;OAEG;IACH,aAAa,CAAC,QAAQ,EAAE,MAAM;IAO9B,SAAS;IAKT,KAAK;IAKL;;OAEG;IACH,UAAU;IAKV;;OAEG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM;IAe5B;;;;;;;;;;OAUG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM;IAsBzB;;;;;;;;OAQG;IACH,MAAM,CAAC,iBAAiB,CAAC,UAAU,EAAE,GAAG;CAqBzC"}
|
|
@@ -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,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;
|
|
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;IAYnC;;OAEG;IACH,UAAU,CAAC,WAAW,UAAQ,GAAG,IAAI;IAIrC,OAAO,IAAI,WAAW,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAItC,IAAI,IAAI,WAAW,CAAC,IAAI,CAAC;IAIzB,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC;IAI7B,MAAM,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,mBAAkB;CAE9C"}
|
package/package.json
CHANGED
package/src/definitions.ts
CHANGED
package/src/entity-map.ts
CHANGED
|
@@ -20,12 +20,12 @@
|
|
|
20
20
|
* @module ecsjs
|
|
21
21
|
*/
|
|
22
22
|
import {
|
|
23
|
-
type ComponentDataMap,
|
|
24
23
|
type ComponentClass,
|
|
24
|
+
type ComponentDataMap,
|
|
25
25
|
type ComponentMap,
|
|
26
26
|
type IComponent
|
|
27
27
|
} from './definitions.js';
|
|
28
|
-
import { KeyMap } from './key-map.js'
|
|
28
|
+
import { KeyMap } from './key-map.js';
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Class for storing entities and their relationships
|
|
@@ -77,7 +77,7 @@ export class EntityMap {
|
|
|
77
77
|
/**
|
|
78
78
|
* Retrieves an entity map by it's registered type
|
|
79
79
|
*/
|
|
80
|
-
getMap<T
|
|
80
|
+
getMap<T>(component: ComponentClass<T>): ComponentDataMap<T> | undefined {
|
|
81
81
|
return this.componentMaps.get(component.name);
|
|
82
82
|
}
|
|
83
83
|
|
|
@@ -90,7 +90,7 @@ export class EntityMap {
|
|
|
90
90
|
* const [entityId, player] = ecs.firstEntry(PlayerComponent) ?? [];
|
|
91
91
|
* ```
|
|
92
92
|
*/
|
|
93
|
-
firstEntry<T
|
|
93
|
+
firstEntry<T>(component: ComponentClass<T>) {
|
|
94
94
|
return this.getMap(component)?.firstEntry();
|
|
95
95
|
}
|
|
96
96
|
|
|
@@ -103,7 +103,7 @@ export class EntityMap {
|
|
|
103
103
|
* const entityId = ecs.firstKey(PlayerComponent);
|
|
104
104
|
* ```
|
|
105
105
|
*/
|
|
106
|
-
firstKey<T
|
|
106
|
+
firstKey<T>(component: ComponentClass<T>) {
|
|
107
107
|
return this.getMap(component)?.firstKey();
|
|
108
108
|
}
|
|
109
109
|
|
|
@@ -116,7 +116,7 @@ export class EntityMap {
|
|
|
116
116
|
* const player = ecs.firstValue(PlayerComponent);
|
|
117
117
|
* ```
|
|
118
118
|
*/
|
|
119
|
-
firstValue<T
|
|
119
|
+
firstValue<T>(component: ComponentClass<T>) {
|
|
120
120
|
return this.getMap(component)?.firstValue();
|
|
121
121
|
}
|
|
122
122
|
|
|
@@ -130,12 +130,40 @@ export class EntityMap {
|
|
|
130
130
|
* let positionData = ecs.get(entityId, Components.PositionComponent);
|
|
131
131
|
* ```
|
|
132
132
|
*/
|
|
133
|
-
get<T
|
|
134
|
-
|
|
135
|
-
if (
|
|
133
|
+
get<T>(entityId: number, component: ComponentClass<T>): T {
|
|
134
|
+
const map = this.componentMaps.get(component.name);
|
|
135
|
+
if (map === undefined)
|
|
136
136
|
throw new ReferenceError(`"${component}" component class does not exist in the componentMaps`);
|
|
137
137
|
|
|
138
|
-
return
|
|
138
|
+
return map.get(entityId);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Get multiple component values related to an entity
|
|
143
|
+
*
|
|
144
|
+
* @param entityId
|
|
145
|
+
* @param components: ComponentClass<any>[]
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
*
|
|
149
|
+
* ```javascript
|
|
150
|
+
* // get an entity
|
|
151
|
+
* const [player, position] = ecs.getValues(entityId, PlayerComponent, PositionComponent);
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
getValues<T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
155
|
+
entityId: number,
|
|
156
|
+
c1: ComponentClass<T1>,
|
|
157
|
+
c2: ComponentClass<T2>,
|
|
158
|
+
c3?: ComponentClass<T3>,
|
|
159
|
+
c4?: ComponentClass<T4>,
|
|
160
|
+
c5?: ComponentClass<T5>,
|
|
161
|
+
c6?: ComponentClass<T6>,
|
|
162
|
+
c7?: ComponentClass<T7>,
|
|
163
|
+
c8?: ComponentClass<T8>
|
|
164
|
+
): [T1, T2, T3, T4, T5, T6, T7, T8];
|
|
165
|
+
getValues<T>(entityId: number, ...components: ComponentClass<T>[]): T[] {
|
|
166
|
+
return components.map(x => this.get(entityId, x))
|
|
139
167
|
}
|
|
140
168
|
|
|
141
169
|
/**
|
|
@@ -168,7 +196,7 @@ export class EntityMap {
|
|
|
168
196
|
* let exists = ecs.has(entityId, PositionComponent);
|
|
169
197
|
* ```
|
|
170
198
|
*/
|
|
171
|
-
has<T
|
|
199
|
+
has<T>(entityId: number, component: ComponentClass<T>): boolean {
|
|
172
200
|
// get the component map
|
|
173
201
|
let map = this.componentMaps.get(component.name);
|
|
174
202
|
if (map === undefined) return false
|
|
@@ -190,13 +218,33 @@ export class EntityMap {
|
|
|
190
218
|
// get the component map
|
|
191
219
|
let map = this.componentMaps.get(componentData.constructor.name);
|
|
192
220
|
if (map === undefined)
|
|
193
|
-
throw new ReferenceError(
|
|
221
|
+
throw new ReferenceError(
|
|
222
|
+
`Component map does not exist using the name: ${componentData.constructor.name}`
|
|
223
|
+
);
|
|
194
224
|
|
|
195
225
|
// set the entity on the entity map
|
|
196
226
|
map.set(entityId, componentData);
|
|
197
227
|
|
|
198
228
|
// return instance
|
|
199
|
-
return componentData
|
|
229
|
+
return componentData;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Set multiple component values for an entity
|
|
234
|
+
*
|
|
235
|
+
* @param entityId
|
|
236
|
+
* @param components: ComponentClass<any>[]
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
*
|
|
240
|
+
* ```javascript
|
|
241
|
+
* // write the component values to the entity
|
|
242
|
+
* ecs.setValues(entityId, new PlayerComponent(), new PositionComponent());
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
setValues<T extends IComponent[]>(entityId: number, ...args: [...T]): [...T];
|
|
246
|
+
setValues(entityId: number, ...components: IComponent[]): IComponent[] {
|
|
247
|
+
return components.map(x => this.set(entityId, x))
|
|
200
248
|
}
|
|
201
249
|
|
|
202
250
|
/**
|
|
@@ -210,8 +258,10 @@ export class EntityMap {
|
|
|
210
258
|
* EntityMap.remove(entityId, Components.PositionComponent);
|
|
211
259
|
* ```
|
|
212
260
|
*/
|
|
213
|
-
remove<T
|
|
214
|
-
|
|
261
|
+
remove<T>(entityId: number, ...components: ComponentClass<T>[]) {
|
|
262
|
+
for (const component of components) {
|
|
263
|
+
this.removeByKey(entityId, component.name)
|
|
264
|
+
}
|
|
215
265
|
}
|
|
216
266
|
|
|
217
267
|
/**
|
|
@@ -245,9 +295,9 @@ export class EntityMap {
|
|
|
245
295
|
* Destroys an entity across all component maps
|
|
246
296
|
*/
|
|
247
297
|
destroyEntity(entityId: number) {
|
|
248
|
-
this.componentMaps.
|
|
249
|
-
if (
|
|
250
|
-
}
|
|
298
|
+
for (const map of this.componentMaps.values()) {
|
|
299
|
+
if (map.has(entityId)) map.delete(entityId);
|
|
300
|
+
}
|
|
251
301
|
}
|
|
252
302
|
|
|
253
303
|
// TODO generate a non repeatable id for network play?
|
|
@@ -262,11 +312,29 @@ export class EntityMap {
|
|
|
262
312
|
}
|
|
263
313
|
|
|
264
314
|
/**
|
|
265
|
-
* Prints all
|
|
315
|
+
* Prints all component maps in a tabular format to the console
|
|
266
316
|
*/
|
|
267
317
|
printTable() {
|
|
268
318
|
this.componentMaps.forEach(map => console.table(map.toTable(true)));
|
|
269
|
-
return this
|
|
319
|
+
return this;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Prints an entity component data in tabular format to the console
|
|
324
|
+
*/
|
|
325
|
+
printEntity(entityId: number) {
|
|
326
|
+
for (const map of this.componentMaps.values()) {
|
|
327
|
+
if (map.has(entityId) === false) continue;
|
|
328
|
+
|
|
329
|
+
const data = map.get(entityId);
|
|
330
|
+
const columns = {
|
|
331
|
+
name: data.constructor.name,
|
|
332
|
+
...data
|
|
333
|
+
};
|
|
334
|
+
console.table({ [entityId]: columns });
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return this;
|
|
270
338
|
}
|
|
271
339
|
|
|
272
340
|
/**
|
package/src/key-map.ts
CHANGED
|
@@ -56,13 +56,11 @@ export class KeyMap<TKey, TValue> extends Map<TKey, TValue> {
|
|
|
56
56
|
|
|
57
57
|
toTable(excludeKeys = false): any[] {
|
|
58
58
|
const table = []
|
|
59
|
-
for (const value of this.entries()) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (excludeKeys == false) meta["entity.key"] = value[0]
|
|
59
|
+
for (const [key, value] of this.entries()) {
|
|
60
|
+
const entity = value as { new(): TValue }
|
|
61
|
+
const meta: KeyCollection<any> = {}
|
|
62
|
+
if (excludeKeys == false) meta["entity.key"] = key
|
|
64
63
|
meta["entity.type"] = entity.constructor.name
|
|
65
|
-
|
|
66
64
|
table.push({ ...meta, ...entity })
|
|
67
65
|
}
|
|
68
66
|
return table
|