ecsjs 1.0.0-beta.13 → 1.0.0-beta.14

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 CHANGED
@@ -1,2 +1,2 @@
1
- var r=class extends Error{constructor(t){super(`Component map does not exist for '${t}'`);this.componentName=t}};var u=class{constructor(e,t,...n){this.ecs=e;this.key=t;this.keyMap=this.ecs.getMap(t),this.entries=this.keyMap.entries(),this.components=n.map(s=>this.ecs.getMap(s))}keyMap;components;entries;next(){let e=this.entries.next(),{value:t,done:n}=e;if(n)return{value:t,done:n};let[s]=t;for(let o of this.components)t.push(o.get(s));return{value:t,done:n}}reset(){this.entries=this.keyMap.entries()}[Symbol.iterator](){return this}};var p=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,s]of this.entries()){let o=s,i={};e==!1&&(i["entity.key"]=n),i["entity.type"]=o.constructor.name,t.push({...i,...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 m=class a{componentMaps=new p;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 s=new p;this.componentMaps.set(n,s)}return this}getMap(e){let t=this.componentMaps.get(e.name);if(t===void 0)throw new r(e.name);return t}firstEntry(e){return this.getMap(e)?.firstEntry()}firstKey(e,...t){let n=this.getMap(e)?.firstKey();if(arguments.length===1)return n;if(n!==void 0)return[n,...t.map(s=>this.getEntity(n,s))]}firstValue(e,...t){if(arguments.length===1)return this.getMap(e)?.firstValue();let[n,s]=this.getMap(e)?.firstEntry()??[];if(n!==void 0)return[s,...t.map(o=>this.getEntity(n,o))]}getEntity(e,t){let n=this.componentMaps.get(t.name);if(n===void 0)throw new r(t.name);return n.get(e)}get(e,...t){return t.length>1?t.map(n=>this.getEntity(e,n)):this.getEntity(e,t[0])}getByKey(e,t){let n=this.componentMaps.get(t);if(n===void 0)throw new r(t);return n.get(e)}has(e,t){let n=this.componentMaps.get(t.name);return n===void 0?!1:n.has(e)}setEntity(e,t){let n=this.componentMaps.get(t.constructor.name);if(n===void 0)throw new r(t.constructor.name);return n.set(e,t),t}set(e,...t){return t.length>1?t.map(n=>this.setEntity(e,n)):this.setEntity(e,t[0])}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 r(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}iterator(e,...t){return new u(this,e,...t)}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),s={name:n.constructor.name,...n};console.table({[e]:s})}return this}static parse(e){let t=new a,n=JSON.parse(e,function(s,o){return o.hasOwnProperty("componentMaps")?(Reflect.setPrototypeOf(o,a.prototype),o):o.hasOwnProperty("__KeyMap__")?new p(o.iterable):this[s]});return t.componentMaps=n.componentMaps,t.nextId=n.nextId,t}static createWithTracing(e){let t={get(n,s){let o=n[s];return typeof o=="function"&&(e.length===0||e.includes(s))?function(...i){return console.groupCollapsed("ecs trace",s,i),console.trace(),console.groupEnd(),o.apply(this,i)}:o}};return new Proxy(new a,t)}};var l=new m;typeof window<"u"?window.ecs=l:typeof module<"u"&&module!==null&&(module.exports={ecs:l});export{u as ComponentIterator,m as EntityMap,l as ecs};
1
+ var a=class extends Error{constructor(t){super(`Component map does not exist for '${t}'`);this.componentName=t}};var u=class{constructor(e,t,...n){this.ecs=e;this.key=t;this.keyMap=this.ecs.getMap(t),this.entries=this.keyMap.entries(),this.components=n.map(s=>this.ecs.getMap(s))}keyMap;components;entries;next(){let e=this.entries.next(),{value:t,done:n}=e;if(n)return{value:t,done:n};let[s,o]=t,r=[s,o];for(let c of this.components)r.push(c.get(s));return{value:r,done:!1}}reset(){this.entries=this.keyMap.entries()}[Symbol.iterator](){return this}};var p=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,s]of this.entries()){let o=s,r={};e==!1&&(r["entity.key"]=n),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)}static get[Symbol.species](){return Map}};var m=class i{componentMaps=new p;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 s=new p;this.componentMaps.set(n,s)}return this}getMap(e){let t=this.componentMaps.get(e.name);if(t===void 0)throw new a(e.name);return t}firstEntry(e){return this.getMap(e)?.firstEntry()}firstKey(e,...t){let n=this.getMap(e)?.firstKey();if(arguments.length===1)return n;if(n!==void 0)return[n,...t.map(s=>this.getEntity(n,s))]}firstValue(e,...t){if(arguments.length===1)return this.getMap(e)?.firstValue();let[n,s]=this.getMap(e)?.firstEntry()??[];if(n!==void 0)return[s,...t.map(o=>this.getEntity(n,o))]}getEntity(e,t){let n=this.componentMaps.get(t.name);if(n===void 0)throw new a(t.name);return n.get(e)}get(e,...t){return t.length>1?t.map(n=>this.getEntity(e,n)):this.getEntity(e,t[0])}getByKey(e,t){let n=this.componentMaps.get(t);if(n===void 0)throw new a(t);return n.get(e)}has(e,t){let n=this.componentMaps.get(t.name);return n===void 0?!1:n.has(e)}setEntity(e,t){let n=this.componentMaps.get(t.constructor.name);if(n===void 0)throw new a(t.constructor.name);return n.set(e,t),t}set(e,...t){return t.length>1?t.map(n=>this.setEntity(e,n)):this.setEntity(e,t[0])}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 a(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}iterator(e,...t){return new u(this,e,...t)}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),s={name:n.constructor.name,...n};console.table({[e]:s})}return this}static parse(e){let t=new i,n=JSON.parse(e,function(s,o){return o.hasOwnProperty("componentMaps")?(Reflect.setPrototypeOf(o,i.prototype),o):o.hasOwnProperty("__KeyMap__")?new p(o.iterable):this[s]});return t.componentMaps=n.componentMaps,t.nextId=n.nextId,t}static createWithTracing(e){let t={get(n,s){let o=n[s];return typeof o=="function"&&(e.length===0||e.includes(s))?function(...r){return console.groupCollapsed("ecs trace",s,r),console.trace(),console.groupEnd(),o.apply(this,r)}:o}};return new Proxy(new i,t)}};var l=new m;typeof window<"u"?window.ecs=l:typeof module<"u"&&module!==null&&(module.exports={ecs:l});export{u as ComponentIterator,m as EntityMap,l 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/errors.ts", "../src/iterators.ts", "../src/key-map.ts", "../src/entity-map.ts", "../src/ecs.ts"],
4
- "sourcesContent": ["export class ComponentNotRegistered extends Error {\n constructor(public componentName: string) {\n super(`Component map does not exist for '${componentName}'`);\n }\n}", "import type { ComponentClass, ComponentClassMap, ComponentInstances } from './definitions.js';\nimport type { EntityMap } from './entity-map.js';\n\nexport class ComponentIterator<\n K extends ComponentClass<any>,\n T extends ComponentClass<any>[]\n> {\n\n private keyMap: ComponentClassMap<any>\n private components: ComponentClassMap<any>[]\n // iteration state\n private entries: MapIterator<[number, ComponentClass<any>]>\n\n /**\n * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // iterate each component value that is related to the Components.Player entity\n * const iterator = new ComponentIterator(ecs, Components.Player, Components.Position)\n * \n * for(const [playerId, player, position] of iterator) {\n * \n * }\n */\n constructor(public ecs: EntityMap, public key: K, ...components: T) {\n this.keyMap = this.ecs.getMap(key)!;\n this.entries = this.keyMap.entries();\n this.components = components.map(x => this.ecs.getMap(x)!);\n }\n\n next(): IteratorResult<ComponentInstances<[ComponentClass<number>, K, ...T]>> {\n const entry = this.entries.next();\n const { value, done } = entry;\n if (done) return { value: value as any, done };\n\n const [entityId] = value;\n for (const x of this.components) {\n value.push(x.get(entityId));\n }\n\n return { value: value as any, done: done as boolean };\n }\n\n /**\n * reset the iterator back to the first entry\n */\n reset() {\n this.entries = this.keyMap.entries();\n }\n\n [Symbol.iterator]() { return this; }\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 */\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 Component,\n type ComponentClass,\n type ComponentClassesMap,\n type ComponentClassMap,\n type ComponentInstances\n} from './definitions.js';\nimport { ComponentNotRegistered } from './errors.js';\nimport { ComponentIterator } from './iterators.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: ComponentClassesMap = new KeyMap()\n\n private nextId: number = 0\n\n constructor() { }\n\n /**\n * Registers component classes\n * \n * @example\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 register<TArgs extends ComponentClass<any>[]>(...componentClasses: TArgs) {\n for (const componentClass of componentClasses) {\n const componentName = componentClass.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: ComponentClassMap<any> = new KeyMap();\n this.componentMaps.set(componentName, componentDataMap);\n }\n\n // chain\n return this;\n }\n\n /**\n * Get a component class map\n * \n * @throws {ComponentNotRegistered} when the specified component is not registered\n */\n getMap<T>(component: ComponentClass<T>): ComponentClassMap<T> | undefined {\n const map = this.componentMaps.get(component.name);\n if (map === undefined) throw new ComponentNotRegistered(component.name)\n return map\n }\n\n /**\n * Get the first entity entry for a component class\n * \n * @throws {ComponentNotRegistered} when the specified component is not registered\n * \n * @example\n * const [entityId, player] = ecs.firstEntry(PlayerComponent) ?? [];\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 * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // return the first component id\n * const entityId = ecs.firstKey(PlayerComponent);\n * \n * // or multiple related values in addition to the first component id\n * const [entityId, position, direction] = ecs.firstKey(\n * PlayerComponent,\n * PositionComponent,\n * DirectionComponent\n * ) ?? [];\n */\n firstKey<TKey extends ComponentClass<any>, T extends ComponentClass<any>[]>(\n keyComponent: TKey,\n ...components: T\n ): ComponentInstances<[ComponentClass<number>, ...T]> | undefined;\n firstKey(keyComponent: ComponentClass<any>, ...components: ComponentClass<any>[]) {\n const entityId = this.getMap(keyComponent)?.firstKey();\n\n // single component key\n if (arguments.length === 1) return entityId;\n if (entityId === undefined) return undefined;\n\n // attach multiple related component values\n return [entityId, ...components.map(x => this.getEntity(entityId, x))];\n }\n\n /**\n * Get the first entity component value for a component class\n * \n * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // return the first component value\n * const player = ecs.firstValue(PlayerComponent);\n * \n * // or multiple related values in addition to the first component\n * const [player, position, direction] = ecs.firstValue(\n * PlayerComponent,\n * PositionComponent,\n * DirectionComponent\n * );\n */\n firstValue<TKey extends ComponentClass<any>, T extends ComponentClass<any>[]>(\n keyComponent: TKey,\n ...components: T\n ): ComponentInstances<[TKey, ...T]> | undefined;\n firstValue(keyComponent: ComponentClass<any>, ...components: ComponentClass<any>[]) {\n // single component\n if (arguments.length === 1) return this.getMap(keyComponent)?.firstValue();\n\n // get the first entry\n const [entityId, value] = this.getMap(keyComponent)?.firstEntry() ?? [];\n if (entityId === undefined) return undefined;\n\n // attach multiple related components\n return [value, ...components.map(x => this.getEntity(entityId, x))]\n }\n\n /**\n * @throws {ComponentNotRegistered} when the specified component is not registered \n */\n private getEntity<T>(entityId: number, component: ComponentClass<T>): T | undefined {\n const map = this.componentMaps.get(component.name);\n if (map === undefined) throw new ComponentNotRegistered(component.name)\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 * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // get one\n * const player = ecs.get(entityId, PlayerComponent);\n * \n * // or get multiple\n * const [player, position] = ecs.get(entityId, PlayerComponent, PositionComponent);\n */\n get<T extends ComponentClass<any>[]>(\n entityId: number,\n ...components: T\n ): ComponentInstances<T> | undefined;\n get<T extends Component>(entityId: number, ...components: ComponentClass<T>[]): T | (T | undefined)[] | undefined {\n if (components.length > 1) return components.map(x => this.getEntity(entityId, x))\n\n // return a single component\n return this.getEntity(entityId, components[0])\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @throws {ComponentNotRegistered} when the specified component name is not registered\n * \n * @example\n * const positionData = ecs.getByKey(entityId, \"PositionComponent\");\n */\n getByKey(entityId: number, componentName: string) {\n // get the component map\n const map = this.componentMaps.get(componentName);\n if (map === undefined) throw new ComponentNotRegistered(componentName)\n\n return map.get(entityId);\n }\n\n /**\n * Check if a component exists for an entity\n * \n * @example\n * const exists = ecs.has(entityId, PositionComponent);\n */\n has<T>(entityId: number, component: ComponentClass<T>): boolean {\n // get the component map\n const map = this.componentMaps.get(component.name);\n if (map === undefined) return false\n\n return map.has(entityId);\n }\n\n private setEntity<T extends Component>(entityId: number, componentData: T): T {\n // get the component map\n const map = this.componentMaps.get(componentData.constructor.name);\n if (map === undefined) throw new ComponentNotRegistered(componentData.constructor.name)\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 * Add or update multiple component values for an entity\n * \n * @example\n * \n * // get one\n * const player = ecs.set(entityId, new PlayerComponent());\n * \n * // or get multiple\n * const [player, position] = ecs.set(\n * entityId,\n * new PlayerComponent(),\n * new PositionComponent()\n * );\n */\n set<T extends Component>(entityId: number, component: T): T;\n set<T extends Component[]>(entityId: number, ...components: T): T;\n set(entityId: number, ...components: Component[]): Component | Component[] {\n if (components.length > 1) return components.map(x => this.setEntity(entityId, x))\n\n // set and return a single component\n return this.setEntity(entityId, components[0])\n }\n\n /**\n * Removes the specified component(s) from an entity\n * \n * @example\n * ecs.remove(entityId, Components.PositionComponent);\n */\n remove<T extends ComponentClass<any>[]>(entityId: number, ...components: T) {\n for (const component of components) {\n this.removeByKey(entityId, component.name)\n }\n }\n\n /**\n * Removes the specified component from an entity\n * \n * @throws {ComponentNotRegistered} when the specified component is not registered\n * \n * @example\n * ecs.removeByKey(entityId, \"PositionComponent\");\n */\n removeByKey(entityId: number, componentName: string) {\n // get the entity map\n const entityMap = this.componentMaps.get(componentName);\n\n // ensure the map is defined\n if (entityMap === undefined) throw new ComponentNotRegistered(componentName);\n\n // get the entity\n const 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 * Deletes an entity from all component maps\n * \n * @example\n * ecs.destroyEntity(entityId);\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 * Iterates over each component value that is related to the key component\n * \n * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // iterate each component value that is related to the Components.Player entity\n * const iterator = ecs.iterator(Components.Player, Components.Position)\n * \n * for(const [playerId, player, position] of iterator) {\n * \n * }\n */\n iterator<K extends ComponentClass<any>, T extends ComponentClass<any>[]>(\n keyComponent: ComponentClass<any>,\n ...components: T\n ): ComponentIterator<K, T>;\n iterator(keyComponent: ComponentClass<any>, ...components: ComponentClass<any>[]) {\n return new ComponentIterator(this, keyComponent, ...components);\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 * const json = JSON.stringfy(ecs);\n * const restoredMap = ecs.parse(json);\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 const 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'\nexport type { ComponentClass, ComponentClassesMap, ComponentClassMap } from './definitions.js'\nexport { EntityMap } from './entity-map.js'\nexport type { ComponentNotRegistered } from './errors.js'\nexport { ComponentIterator } from './iterators.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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AAK,YAAY,KACvB,GAAGA,CACL,EACA,QAAQ,MAAM,CAAE,CAACN,CAAQ,EAAGO,CAAQ,CAAC,CACvC,CAEA,OAAO,IACT,CASA,OAAO,MAAMC,EAAc,CACzB,IAAMC,EAAM,IAAIpB,EAEVqB,EAAW,KAAK,MAAMF,EAAM,SAAUG,EAAaT,EAAY,CAEnE,OAAIA,EAAM,eAAe,eAAe,GACtC,QAAQ,eAAeA,EAAOb,EAAU,SAAS,EAC1Ca,GAGLA,EAAM,eAAe,YAAY,EAC5B,IAAIZ,EAAOY,EAAM,QAAQ,EAG3B,KAAKS,CAAG,CACjB,CAAC,EAED,OAAAF,EAAI,cAAgBC,EAAS,cAC7BD,EAAI,OAASC,EAAS,OACfD,CACT,CAWA,OAAO,kBAAkBG,EAAiB,CACxC,IAAMC,EAAe,CACnB,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,IAAI3B,EAAawB,CAAY,CAChD,CAEF,EC9YO,IAAMK,EAAM,IAAIC,EAEnB,OAAO,OAAW,IAEpB,OAAO,IAAMD,EACJ,OAAO,OAAW,KAAe,SAAW,OAErD,OAAO,QAAU,CAAE,IAAAA,CAAI",
6
- "names": ["ComponentNotRegistered", "componentName", "ComponentIterator", "ecs", "key", "components", "x", "entry", "value", "done", "entityId", "KeyMap", "entries", "excludeKeys", "table", "key", "value", "entity", "meta", "x", "EntityMap", "_EntityMap", "KeyMap", "componentClasses", "componentClass", "componentName", "componentDataMap", "component", "map", "ComponentNotRegistered", "keyComponent", "components", "entityId", "x", "value", "componentData", "entityMap", "ComponentIterator", "data", "columns", "json", "ecs", "restored", "key", "funcFilter", "traceHandler", "target", "propKey", "targetValue", "args", "ecs", "EntityMap"]
4
+ "sourcesContent": ["export class ComponentNotRegistered extends Error {\n constructor(public componentName: string) {\n super(`Component map does not exist for '${componentName}'`);\n }\n}", "import type {\n ComponentClass,\n ComponentClassMap,\n ComponentInstances,\n IteratorResult\n} from './definitions.js';\nimport type { EntityMap } from './entity-map.js';\n\nexport class ComponentIterator<\n K extends ComponentClass<any>,\n T extends ComponentClass<any>[]\n> {\n\n private keyMap: ComponentClassMap<any>\n private components: ComponentClassMap<any>[]\n // iteration state\n private entries: MapIterator<[number, K]>\n\n /**\n * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // iterate each component value that is related to the Components.Player entity\n * const iterator = new ComponentIterator(ecs, Components.Player, Components.Position)\n * \n * for(const [playerId, player, position] of iterator) {\n * \n * }\n */\n constructor(public ecs: EntityMap, public key: K, ...components: T) {\n this.keyMap = this.ecs.getMap(key)!;\n this.entries = this.keyMap.entries();\n this.components = components.map(x => this.ecs.getMap(x)!);\n }\n\n next(): IteratorResult<ComponentInstances<[ComponentClass<number>, K, ...T]>> {\n const entry = this.entries.next();\n const { value, done } = entry;\n if (done) return { value: value as any, done };\n\n const [entityId, entityValue] = value;\n const results = [entityId, entityValue]\n for (const x of this.components) {\n results.push(x.get(entityId));\n }\n\n return { value: results as any, done: false };\n }\n\n /**\n * reset the iterator back to the first entry\n */\n reset() {\n this.entries = this.keyMap.entries();\n }\n\n [Symbol.iterator]() { return this; }\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 */\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 Component,\n type ComponentClass,\n type ComponentClassesMap,\n type ComponentClassMap,\n type ComponentInstances\n} from './definitions.js';\nimport { ComponentNotRegistered } from './errors.js';\nimport { ComponentIterator } from './iterators.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: ComponentClassesMap = new KeyMap()\n\n private nextId: number = 0\n\n constructor() { }\n\n /**\n * Registers component classes\n * \n * @example\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 register<TArgs extends ComponentClass<any>[]>(...componentClasses: TArgs) {\n for (const componentClass of componentClasses) {\n const componentName = componentClass.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: ComponentClassMap<any> = new KeyMap();\n this.componentMaps.set(componentName, componentDataMap);\n }\n\n // chain\n return this;\n }\n\n /**\n * Get a component class map\n * \n * @throws {ComponentNotRegistered} when the specified component is not registered\n */\n getMap<T>(component: ComponentClass<T>): ComponentClassMap<T> | undefined {\n const map = this.componentMaps.get(component.name);\n if (map === undefined) throw new ComponentNotRegistered(component.name)\n return map\n }\n\n /**\n * Get the first entity entry for a component class\n * \n * @throws {ComponentNotRegistered} when the specified component is not registered\n * \n * @example\n * const [entityId, player] = ecs.firstEntry(PlayerComponent) ?? [];\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 * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // return the first component id\n * const entityId = ecs.firstKey(PlayerComponent);\n * \n * // or multiple related values in addition to the first component id\n * const [entityId, position, direction] = ecs.firstKey(\n * PlayerComponent,\n * PositionComponent,\n * DirectionComponent\n * ) ?? [];\n */\n firstKey<TKey extends ComponentClass<any>, T extends ComponentClass<any>[]>(\n keyComponent: TKey,\n ...components: T\n ): ComponentInstances<[ComponentClass<number>, ...T]> | undefined;\n firstKey(keyComponent: ComponentClass<any>, ...components: ComponentClass<any>[]) {\n const entityId = this.getMap(keyComponent)?.firstKey();\n\n // single component key\n if (arguments.length === 1) return entityId;\n if (entityId === undefined) return undefined;\n\n // attach multiple related component values\n return [entityId, ...components.map(x => this.getEntity(entityId, x))];\n }\n\n /**\n * Get the first entity component value for a component class\n * \n * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // return the first component value\n * const player = ecs.firstValue(PlayerComponent);\n * \n * // or multiple related values in addition to the first component\n * const [player, position, direction] = ecs.firstValue(\n * PlayerComponent,\n * PositionComponent,\n * DirectionComponent\n * );\n */\n firstValue<TKey extends ComponentClass<any>, T extends ComponentClass<any>[]>(\n keyComponent: TKey,\n ...components: T\n ): ComponentInstances<[TKey, ...T]> | undefined;\n firstValue(keyComponent: ComponentClass<any>, ...components: ComponentClass<any>[]) {\n // single component\n if (arguments.length === 1) return this.getMap(keyComponent)?.firstValue();\n\n // get the first entry\n const [entityId, value] = this.getMap(keyComponent)?.firstEntry() ?? [];\n if (entityId === undefined) return undefined;\n\n // attach multiple related components\n return [value, ...components.map(x => this.getEntity(entityId, x))]\n }\n\n /**\n * @throws {ComponentNotRegistered} when the specified component is not registered \n */\n private getEntity<T>(entityId: number, component: ComponentClass<T>): T | undefined {\n const map = this.componentMaps.get(component.name);\n if (map === undefined) throw new ComponentNotRegistered(component.name)\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 * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // get one\n * const player = ecs.get(entityId, PlayerComponent);\n * \n * // or get multiple\n * const [player, position] = ecs.get(entityId, PlayerComponent, PositionComponent);\n */\n get<T extends ComponentClass<any>[]>(\n entityId: number,\n ...components: T\n ): ComponentInstances<T> | undefined;\n get<T extends Component>(entityId: number, ...components: ComponentClass<T>[]): T | (T | undefined)[] | undefined {\n if (components.length > 1) return components.map(x => this.getEntity(entityId, x))\n\n // return a single component\n return this.getEntity(entityId, components[0])\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @throws {ComponentNotRegistered} when the specified component name is not registered\n * \n * @example\n * const positionData = ecs.getByKey(entityId, \"PositionComponent\");\n */\n getByKey(entityId: number, componentName: string) {\n // get the component map\n const map = this.componentMaps.get(componentName);\n if (map === undefined) throw new ComponentNotRegistered(componentName)\n\n return map.get(entityId);\n }\n\n /**\n * Check if a component exists for an entity\n * \n * @example\n * const exists = ecs.has(entityId, PositionComponent);\n */\n has<T>(entityId: number, component: ComponentClass<T>): boolean {\n // get the component map\n const map = this.componentMaps.get(component.name);\n if (map === undefined) return false\n\n return map.has(entityId);\n }\n\n private setEntity<T extends Component>(entityId: number, componentData: T): T {\n // get the component map\n const map = this.componentMaps.get(componentData.constructor.name);\n if (map === undefined) throw new ComponentNotRegistered(componentData.constructor.name)\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 * Add or update multiple component values for an entity\n * \n * @example\n * \n * // get one\n * const player = ecs.set(entityId, new PlayerComponent());\n * \n * // or get multiple\n * const [player, position] = ecs.set(\n * entityId,\n * new PlayerComponent(),\n * new PositionComponent()\n * );\n */\n set<T extends Component>(entityId: number, component: T): T;\n set<T extends Component[]>(entityId: number, ...components: T): T;\n set(entityId: number, ...components: Component[]): Component | Component[] {\n if (components.length > 1) return components.map(x => this.setEntity(entityId, x))\n\n // set and return a single component\n return this.setEntity(entityId, components[0])\n }\n\n /**\n * Removes the specified component(s) from an entity\n * \n * @example\n * ecs.remove(entityId, Components.PositionComponent);\n */\n remove<T extends ComponentClass<any>[]>(entityId: number, ...components: T) {\n for (const component of components) {\n this.removeByKey(entityId, component.name)\n }\n }\n\n /**\n * Removes the specified component from an entity\n * \n * @throws {ComponentNotRegistered} when the specified component is not registered\n * \n * @example\n * ecs.removeByKey(entityId, \"PositionComponent\");\n */\n removeByKey(entityId: number, componentName: string) {\n // get the entity map\n const entityMap = this.componentMaps.get(componentName);\n\n // ensure the map is defined\n if (entityMap === undefined) throw new ComponentNotRegistered(componentName);\n\n // get the entity\n const 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 * Deletes an entity from all component maps\n * \n * @example\n * ecs.destroyEntity(entityId);\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 * Iterates over each component value that is related to the key component\n * \n * @throws {ComponentNotRegistered} when any of specified component(s) are not registered\n * \n * @example\n * // iterate each component value that is related to the Components.Player entity\n * const iterator = ecs.iterator(Components.Player, Components.Position)\n * \n * for(const [playerId, player, position] of iterator) {\n * \n * }\n * \n * // you can declare the type of iterator before it's assigned\n * let iterator: IComponentIterator<[Components.Player, Components.Position]>\n * \n * // then with late bound assignment (keeping the iterator intellisense)\n * iterator = ecs.iterator(Components.Player, Components.Position)\n * \n * for(const [playerId, player, position] of iterator) {\n * const moving = player.isMoving\n * }\n */\n iterator<K extends ComponentClass<any>, T extends ComponentClass<any>[]>(\n keyComponent: K,\n ...components: T\n ): ComponentIterator<K, T>\n iterator(keyComponent: ComponentClass<any>, ...components: ComponentClass<any>[]) {\n return new ComponentIterator(this, keyComponent, ...components) as any;\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 * const json = JSON.stringfy(ecs);\n * const restoredMap = ecs.parse(json);\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 const 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'\nexport type { ComponentClass, ComponentClassesMap, ComponentClassMap, IComponentIterator } from './definitions.js'\nexport { EntityMap } from './entity-map.js'\nexport type { ComponentNotRegistered } from './errors.js'\nexport { ComponentIterator } from './iterators.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": "AAAO,IAAMA,EAAN,cAAqC,KAAM,CAChD,YAAmBC,EAAuB,CACxC,MAAM,qCAAqCA,CAAa,GAAG,EAD1C,mBAAAA,CAEnB,CACF,ECIO,IAAMC,EAAN,KAGL,CAkBA,YAAmBC,EAAuBC,KAAWC,EAAe,CAAjD,SAAAF,EAAuB,SAAAC,EACxC,KAAK,OAAS,KAAK,IAAI,OAAOA,CAAG,EACjC,KAAK,QAAU,KAAK,OAAO,QAAQ,EACnC,KAAK,WAAaC,EAAW,IAAIC,GAAK,KAAK,IAAI,OAAOA,CAAC,CAAE,CAC3D,CApBQ,OACA,WAEA,QAmBR,MAA8E,CAC5E,IAAMC,EAAQ,KAAK,QAAQ,KAAK,EAC1B,CAAE,MAAAC,EAAO,KAAAC,CAAK,EAAIF,EACxB,GAAIE,EAAM,MAAO,CAAE,MAAOD,EAAc,KAAAC,CAAK,EAE7C,GAAM,CAACC,EAAUC,CAAW,EAAIH,EAC1BI,EAAU,CAACF,EAAUC,CAAW,EACtC,QAAWL,KAAK,KAAK,WACnBM,EAAQ,KAAKN,EAAE,IAAII,CAAQ,CAAC,EAG9B,MAAO,CAAE,MAAOE,EAAgB,KAAM,EAAM,CAC9C,CAKA,OAAQ,CACN,KAAK,QAAU,KAAK,OAAO,QAAQ,CACrC,CAEA,CA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,CACF,EAEA,OAAO,IAAI,MAAM,IAAI3B,EAAawB,CAAY,CAChD,CAEF,ECxZO,IAAMK,EAAM,IAAIC,EAEnB,OAAO,OAAW,IAEpB,OAAO,IAAMD,EACJ,OAAO,OAAW,KAAe,SAAW,OAErD,OAAO,QAAU,CAAE,IAAAA,CAAI",
6
+ "names": ["ComponentNotRegistered", "componentName", "ComponentIterator", "ecs", "key", "components", "x", "entry", "value", "done", "entityId", "entityValue", "results", "KeyMap", "entries", "excludeKeys", "table", "key", "value", "entity", "meta", "x", "EntityMap", "_EntityMap", "KeyMap", "componentClasses", "componentClass", "componentName", "componentDataMap", "component", "map", "ComponentNotRegistered", "keyComponent", "components", "entityId", "x", "value", "componentData", "entityMap", "ComponentIterator", "data", "columns", "json", "ecs", "restored", "key", "funcFilter", "traceHandler", "target", "propKey", "targetValue", "args", "ecs", "EntityMap"]
7
7
  }
@@ -5,9 +5,28 @@ export type KeyCollection<T> = {
5
5
  export interface Component {
6
6
  }
7
7
  export type ComponentClass<ComponentInstance> = new (...args: any[]) => ComponentInstance;
8
+ /**
9
+ * Used for infering generic type spread for classes
10
+ */
11
+ export type ComponentClasses<TClasses extends Component[]> = TClasses['length'] extends 1 ? TClasses[0] : {
12
+ [Index in keyof TClasses]: TClasses[Index];
13
+ };
14
+ /**
15
+ * Used for infering generic type spread for class instances
16
+ */
8
17
  export type ComponentInstances<TClasses extends ComponentClass<any>[]> = TClasses['length'] extends 1 ? TClasses[0] extends ComponentClass<infer R> ? R : never : {
9
18
  [Index in keyof TClasses]: TClasses[Index] extends ComponentClass<infer R> ? R : never;
10
19
  };
11
20
  export type ComponentClassMap<T> = KeyMap<number, T>;
12
21
  export type ComponentClassesMap = KeyMap<string, ComponentClassMap<any>>;
22
+ export type IteratorResult<T> = {
23
+ done: boolean;
24
+ value: T;
25
+ };
26
+ export type Iterator<T> = {
27
+ next: () => IteratorResult<T>;
28
+ reset: () => void;
29
+ [Symbol.iterator]: () => Iterator<T>;
30
+ };
31
+ export type IComponentIterator<T extends Component[]> = Iterator<ComponentClasses<[number, ...T]>>;
13
32
  //# 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,MAAM,WAAW,SAAS;CAAI;AAE9B,MAAM,MAAM,cAAc,CAAC,iBAAiB,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,iBAAiB,CAAA;AAEzF,MAAM,MAAM,kBAAkB,CAAC,QAAQ,SAAS,cAAc,CAAC,GAAG,CAAC,EAAE,IAEnE,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,GAEpF;KACC,KAAK,IAAI,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,SAAS,cAAc,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK;CACvF,CAAA;AAEH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;AAEpD,MAAM,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAA"}
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;A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AAA;CACrC,CAAA;AAID,MAAM,MAAM,kBAAkB,CAAC,CAAC,SAAS,SAAS,EAAE,IAChD,QAAQ,CAAC,gBAAgB,CAAC,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA"}
@@ -5,7 +5,7 @@
5
5
  * @main ecsjs
6
6
  */
7
7
  import { EntityMap } from './entity-map.js';
8
- export type { ComponentClass, ComponentClassesMap, ComponentClassMap } from './definitions.js';
8
+ export type { ComponentClass, ComponentClassesMap, ComponentClassMap, IComponentIterator } from './definitions.js';
9
9
  export { EntityMap } from './entity-map.js';
10
10
  export type { ComponentNotRegistered } from './errors.js';
11
11
  export { ComponentIterator } from './iterators.js';
@@ -1 +1 @@
1
- {"version":3,"file":"ecs.d.ts","sourceRoot":"","sources":["../../../src/ecs.ts"],"names":[],"mappings":"AAkBA;;;;;GAKG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,YAAY,EAAE,cAAc,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAC9F,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,YAAY,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAElD,eAAO,MAAM,GAAG,WAAkB,CAAA"}
1
+ {"version":3,"file":"ecs.d.ts","sourceRoot":"","sources":["../../../src/ecs.ts"],"names":[],"mappings":"AAkBA;;;;;GAKG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,YAAY,EAAE,cAAc,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AAClH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,YAAY,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAElD,eAAO,MAAM,GAAG,WAAkB,CAAA"}
@@ -168,8 +168,18 @@ export declare class EntityMap {
168
168
  * for(const [playerId, player, position] of iterator) {
169
169
  *
170
170
  * }
171
+ *
172
+ * // you can declare the type of iterator before it's assigned
173
+ * let iterator: IComponentIterator<[Components.Player, Components.Position]>
174
+ *
175
+ * // then with late bound assignment (keeping the iterator intellisense)
176
+ * iterator = ecs.iterator(Components.Player, Components.Position)
177
+ *
178
+ * for(const [playerId, player, position] of iterator) {
179
+ * const moving = player.isMoving
180
+ * }
171
181
  */
172
- iterator<K extends ComponentClass<any>, T extends ComponentClass<any>[]>(keyComponent: ComponentClass<any>, ...components: T): ComponentIterator<K, T>;
182
+ iterator<K extends ComponentClass<any>, T extends ComponentClass<any>[]>(keyComponent: K, ...components: T): ComponentIterator<K, T>;
173
183
  /**
174
184
  * Prints all component maps in a tabular format to the console
175
185
  */
@@ -1 +1 @@
1
- {"version":3,"file":"entity-map.d.ts","sourceRoot":"","sources":["../../../src/entity-map.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,OAAO,EACL,KAAK,SAAS,EACd,KAAK,cAAc,EAEnB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACxB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAGnD;;;;GAIG;AACH,qBAAa,SAAS;IAGpB,OAAO,CAAC,aAAa,CAAoC;IAEzD,OAAO,CAAC,MAAM,CAAY;;IAI1B;;;;;;;;;;;;;;OAcG;IACH,QAAQ,CAAC,KAAK,SAAS,cAAc,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,gBAAgB,EAAE,KAAK;IAexE;;;;OAIG;IACH,MAAM,CAAC,CAAC,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,GAAG,SAAS;IAMzE;;;;;;;OAOG;IACH,UAAU,CAAC,CAA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AAC,SAAS;IAYjB;;;;;;;;;;;;;;OAcG;IACH,GAAG,CAAC,CAAC,SAAS,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,GAAG,CAAC;IAC3D,GAAG,CAAC,CAAC,SAAS,SAAS,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,CAAC,GAAG,CAAC;IAQjE;;;;;OAKG;IACH,MAAM,CAAC,CAAC,SAAS,cAAc,CAAC,GAAG,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,CAAC;IAM1E;;;;;;;OAOG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM;IAenD;;;;;OAKG;IACH,aAAa,CAAC,QAAQ,EAAE,MAAM;IAO9B,SAAS;IAKT,KAAK;IAKL;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,QAAQ,CAAC,CAAC,SAAS,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,cAAc,CAAC,GAAG,CAAC,EAAE,EACrE,YAAY,EAAE,CAAC,EACf,GAAG,UAAU,EAAE,CAAC,GACf,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC;IAK1B;;OAEG;IACH,UAAU;IAKV;;OAEG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM;IAe5B;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM;IAsBzB;;;;;;;;OAQG;IACH,MAAM,CAAC,iBAAiB,CAAC,UAAU,EAAE,GAAG;CAqBzC"}
@@ -1,4 +1,4 @@
1
- import type { ComponentClass, ComponentInstances } from './definitions.js';
1
+ import type { ComponentClass, ComponentInstances, IteratorResult } from './definitions.js';
2
2
  import type { EntityMap } from './entity-map.js';
3
3
  export declare class ComponentIterator<K extends ComponentClass<any>, T extends ComponentClass<any>[]> {
4
4
  ecs: EntityMap;
@@ -1 +1 @@
1
- {"version":3,"file":"iterators.d.ts","sourceRoot":"","sources":["../../../src/iterators.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAqB,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,qBAAa,iBAAiB,CAC5B,CAAC,SAAS,cAAc,CAAC,GAAG,CAAC,EAC7B,CAAC,SAAS,cAAc,CAAC,GAAG,CAAC,EAAE;IAmBZ,GAAG,EAAE,SAAS;IAAS,GAAG,EAAE,CAAC;IAhBhD,OAAO,CAAC,MAAM,CAAwB;IACtC,OAAO,CAAC,UAAU,CAA0B;IAE5C,OAAO,CAAC,OAAO,CAA4C;IAE3D;;;;;;;;;;OAUG;gBACgB,GAAG,EAAE,SAAS,EAAS,GAAG,EAAE,CAAC,EAAE,GAAG,UAAU,EAAE,CAAC;IAMlE,IAAI,IAAI,cAAc,CAAC,kBAAkB,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;IAa7E;;OAEG;IACH,KAAK;IAIL,CAAC,MAAM,CAAC,QAAQ,CAAC;CAElB"}
1
+ {"version":3,"file":"iterators.d.ts","sourceRoot":"","sources":["../../../src/iterators.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EAEd,kBAAkB,EAClB,cAAc,EACf,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,qBAAa,iBAAiB,CAC5B,CAAC,SAAS,cAAc,CAAC,GAAG,CAAC,EAC7B,CAAC,SAAS,cAAc,CAAC,GAAG,CAAC,EAAE;IAmBZ,GAAG,EAAE,SAAS;IAAS,GAAG,EAAE,CAAC;IAhBhD,OAAO,CAAC,MAAM,CAAwB;IACtC,OAAO,CAAC,UAAU,CAA0B;IAE5C,OAAO,CAAC,OAAO,CAA0B;IAEzC;;;;;;;;;;OAUG;gBACgB,GAAG,EAAE,SAAS,EAAS,GAAG,EAAE,CAAC,EAAE,GAAG,UAAU,EAAE,CAAC;IAMlE,IAAI,IAAI,cAAc,CAAC,kBAAkB,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;IAc7E;;OAEG;IACH,KAAK;IAIL,CAAC,MAAM,CAAC,QAAQ,CAAC;CAElB"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "title": "ECS JS",
3
3
  "name": "ecsjs",
4
- "version": "1.0.0-beta.13",
4
+ "version": "1.0.0-beta.14",
5
5
  "description": "An entity component system library for JavaScript",
6
6
  "author": "2013+ pflannery (https://github.com/pflannery)",
7
7
  "license": "GNU GPL v3",
@@ -6,6 +6,20 @@ export interface Component { }
6
6
 
7
7
  export type ComponentClass<ComponentInstance> = new (...args: any[]) => ComponentInstance
8
8
 
9
+ /**
10
+ * Used for infering generic type spread for classes
11
+ */
12
+ export type ComponentClasses<TClasses extends Component[]> =
13
+ // return a single variadic item when only 1 item is specified
14
+ TClasses['length'] extends 1 ? TClasses[0]
15
+ // otherwise return an array of variadic types
16
+ : {
17
+ [Index in keyof TClasses]: TClasses[Index]
18
+ }
19
+
20
+ /**
21
+ * Used for infering generic type spread for class instances
22
+ */
9
23
  export type ComponentInstances<TClasses extends ComponentClass<any>[]> =
10
24
  // return a single variadic item when only 1 item is specified
11
25
  TClasses['length'] extends 1 ? TClasses[0] extends ComponentClass<infer R> ? R : never
@@ -16,4 +30,20 @@ export type ComponentInstances<TClasses extends ComponentClass<any>[]> =
16
30
 
17
31
  export type ComponentClassMap<T> = KeyMap<number, T>
18
32
 
19
- export type ComponentClassesMap = KeyMap<string, ComponentClassMap<any>>
33
+ export type ComponentClassesMap = KeyMap<string, ComponentClassMap<any>>
34
+
35
+ export type IteratorResult<T> = {
36
+ done: boolean,
37
+ value: T
38
+ }
39
+
40
+ export type Iterator<T> = {
41
+ next: () => IteratorResult<T>
42
+ reset: () => void
43
+ [Symbol.iterator]: () => Iterator<T>
44
+ }
45
+
46
+ // allow late bound assignment
47
+ // e.g. let iterator: IComponentIterator<[Components.Player, Components.Position]>
48
+ export type IComponentIterator<T extends Component[]>
49
+ = Iterator<ComponentClasses<[number, ...T]>>
package/src/ecs.ts CHANGED
@@ -23,7 +23,7 @@
23
23
  * @main ecsjs
24
24
  */
25
25
  import { EntityMap } from './entity-map.js'
26
- export type { ComponentClass, ComponentClassesMap, ComponentClassMap } from './definitions.js'
26
+ export type { ComponentClass, ComponentClassesMap, ComponentClassMap, IComponentIterator } from './definitions.js'
27
27
  export { EntityMap } from './entity-map.js'
28
28
  export type { ComponentNotRegistered } from './errors.js'
29
29
  export { ComponentIterator } from './iterators.js'
package/src/entity-map.ts CHANGED
@@ -332,13 +332,23 @@ export class EntityMap {
332
332
  * for(const [playerId, player, position] of iterator) {
333
333
  *
334
334
  * }
335
+ *
336
+ * // you can declare the type of iterator before it's assigned
337
+ * let iterator: IComponentIterator<[Components.Player, Components.Position]>
338
+ *
339
+ * // then with late bound assignment (keeping the iterator intellisense)
340
+ * iterator = ecs.iterator(Components.Player, Components.Position)
341
+ *
342
+ * for(const [playerId, player, position] of iterator) {
343
+ * const moving = player.isMoving
344
+ * }
335
345
  */
336
346
  iterator<K extends ComponentClass<any>, T extends ComponentClass<any>[]>(
337
- keyComponent: ComponentClass<any>,
347
+ keyComponent: K,
338
348
  ...components: T
339
- ): ComponentIterator<K, T>;
349
+ ): ComponentIterator<K, T>
340
350
  iterator(keyComponent: ComponentClass<any>, ...components: ComponentClass<any>[]) {
341
- return new ComponentIterator(this, keyComponent, ...components);
351
+ return new ComponentIterator(this, keyComponent, ...components) as any;
342
352
  }
343
353
 
344
354
  /**
package/src/iterators.ts CHANGED
@@ -1,4 +1,9 @@
1
- import type { ComponentClass, ComponentClassMap, ComponentInstances } from './definitions.js';
1
+ import type {
2
+ ComponentClass,
3
+ ComponentClassMap,
4
+ ComponentInstances,
5
+ IteratorResult
6
+ } from './definitions.js';
2
7
  import type { EntityMap } from './entity-map.js';
3
8
 
4
9
  export class ComponentIterator<
@@ -9,7 +14,7 @@ export class ComponentIterator<
9
14
  private keyMap: ComponentClassMap<any>
10
15
  private components: ComponentClassMap<any>[]
11
16
  // iteration state
12
- private entries: MapIterator<[number, ComponentClass<any>]>
17
+ private entries: MapIterator<[number, K]>
13
18
 
14
19
  /**
15
20
  * @throws {ComponentNotRegistered} when any of specified component(s) are not registered
@@ -33,12 +38,13 @@ export class ComponentIterator<
33
38
  const { value, done } = entry;
34
39
  if (done) return { value: value as any, done };
35
40
 
36
- const [entityId] = value;
41
+ const [entityId, entityValue] = value;
42
+ const results = [entityId, entityValue]
37
43
  for (const x of this.components) {
38
- value.push(x.get(entityId));
44
+ results.push(x.get(entityId));
39
45
  }
40
46
 
41
- return { value: value as any, done: done as boolean };
47
+ return { value: results as any, done: false };
42
48
  }
43
49
 
44
50
  /**