ecsjs 1.0.0-beta.1 → 1.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,9 +20,9 @@ An entity component system library for JavaScript
20
20
  ### Browser Module
21
21
 
22
22
  ```js
23
- import {ecs, Component} from './some-path/ecs.js'
23
+ import {ecs} from './some-path/ecs.js'
24
24
 
25
- class PositionComponent extends Component {
25
+ class PositionComponent {
26
26
  constructor(x, y) {
27
27
  this.x = x
28
28
  this.y = y
@@ -42,7 +42,7 @@ ecs.set(entityId, new PositionComponent(25, 25))
42
42
  ```html
43
43
  <script type="application/javascript" src="./some-path/ecs.js"></script>
44
44
  <script>
45
- class PositionComponent extends ecs.Component {
45
+ class PositionComponent {
46
46
  constructor(x, y) {
47
47
  this.x = x
48
48
  this.y = y
package/dist/ecs.js CHANGED
@@ -1,296 +1,2 @@
1
- // src/definitions.ts
2
- var Component = class {
3
- };
4
-
5
- // src/key-map.ts
6
- var KeyMap = class extends Map {
7
- constructor(entries = []) {
8
- super(entries);
9
- }
10
- firstEntry() {
11
- if (this.size === 0) return [void 0, void 0];
12
- const iterator = this.entries();
13
- const result = iterator.next();
14
- return result.value;
15
- }
16
- firstKey() {
17
- if (this.size === 0) return void 0;
18
- const iterator = this.keys();
19
- const result = iterator.next();
20
- return result.value;
21
- }
22
- firstValue() {
23
- if (this.size === 0) return void 0;
24
- const iterator = this.values();
25
- const result = iterator.next();
26
- return result.value;
27
- }
28
- toJSON() {
29
- return { __KeyMap__: 1, iterable: [...this.entries()] };
30
- }
31
- toTable(excludeKeys = false) {
32
- const table = [];
33
- for (const value of this.entries()) {
34
- let entity = value[1];
35
- let meta = {};
36
- if (excludeKeys == false) meta["entity.key"] = value[0];
37
- meta["entity.type"] = entity.constructor.name;
38
- table.push({ ...meta, ...entity });
39
- }
40
- return table;
41
- }
42
- /**
43
- * Outputs keys and values in a tabular format to the console
44
- */
45
- printTable(excludeKeys = false) {
46
- console.table(this.toTable(excludeKeys));
47
- }
48
- entries() {
49
- return super.entries().filter((x) => x !== void 0 && x !== null);
50
- }
51
- keys() {
52
- return super.keys().filter((x) => x !== void 0 && x !== null);
53
- }
54
- values() {
55
- return super.values().filter((x) => x !== void 0 && x !== null);
56
- }
57
- // default iterator
58
- [Symbol.iterator]() {
59
- return this.values();
60
- }
61
- static get [Symbol.species]() {
62
- return Map;
63
- }
64
- };
65
-
66
- // src/entity-map.ts
67
- var EntityMap = class _EntityMap {
68
- // entity class maps
69
- componentMaps = new KeyMap();
70
- nextId = 0;
71
- constructor() {
72
- }
73
- /**
74
- * Registers a component class
75
- *
76
- * @example
77
- *
78
- * ```javascript
79
- * // component class
80
- * class MyComponent {
81
- * constructor(x) {
82
- * this.x = x;
83
- * }
84
- * }
85
- *
86
- * ecs.register(MyComponent);
87
- * // or
88
- * ecs.register(MyComponent1, MyComponent2);
89
- * ```
90
- */
91
- register(...component) {
92
- for (const definition of component) {
93
- const componentName = definition.name;
94
- if (componentName === void 0)
95
- throw new ReferenceError("The component class does not have a name defined. i.e. a constructor name");
96
- const componentDataMap = new KeyMap();
97
- this.componentMaps.set(componentName, componentDataMap);
98
- }
99
- return this;
100
- }
101
- /**
102
- * Retrieves an entity map by it's registered type
103
- */
104
- getMap(component) {
105
- return this.componentMaps.get(component.name);
106
- }
107
- /**
108
- * Retrieves an entity from its registered entity map
109
- *
110
- * @example
111
- *
112
- * ```javascript
113
- * // get an entity
114
- * let positionData = ecs.get(entityId, Components.PositionComponent);
115
- * ```
116
- */
117
- get(entityId, component) {
118
- let cm = this.componentMaps.get(component.name);
119
- if (cm === void 0)
120
- throw new ReferenceError(`"${component}" component class does not exist in the componentMaps`);
121
- return cm.get(entityId);
122
- }
123
- /**
124
- * Retrieves an entity from its registered entity map
125
- *
126
- * @example
127
- *
128
- * ```javascript
129
- * // get an entity
130
- * let positionData = ecs.getByKey(entityId, "PositionComponent");
131
- * ```
132
- */
133
- getByKey(entityId, componentName) {
134
- let map = this.componentMaps.get(componentName);
135
- if (map === void 0)
136
- throw new ReferenceError(`"${componentName}" component does not exist in the componentMaps`);
137
- return map.get(entityId);
138
- }
139
- /**
140
- * Checks if the entity exists in it's registed component map
141
- * The entity type must be registered before calling this method
142
- *
143
- * @example
144
- *
145
- * ```javascript
146
- * // get an entity
147
- * let exists = ecs.has(entityId, PositionComponent);
148
- * ```
149
- */
150
- has(entityId, component) {
151
- let map = this.componentMaps.get(component.name);
152
- if (map === void 0) return false;
153
- return map.has(entityId);
154
- }
155
- /**
156
- * Adds or updates an entity to it's registered entity map
157
- *
158
- * @example
159
- *
160
- * ```javascript
161
- * // add/update an entity
162
- * let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));
163
- * ```
164
- */
165
- set(entityId, componentData) {
166
- let map = this.componentMaps.get(componentData.constructor.name);
167
- if (map === void 0)
168
- throw new ReferenceError(`Component map does not exist using the name: ${componentData.constructor.name}`);
169
- map.set(entityId, componentData);
170
- return componentData;
171
- }
172
- /**
173
- * Removes an entity from its entity map.
174
- * Removes any related Node entities that are mapped to the entity
175
- *
176
- * @example
177
- *
178
- * ```javascript
179
- * // remove an entity from its entity map
180
- * EntityMap.remove(entityId, Components.PositionComponent);
181
- * ```
182
- */
183
- remove(entityId, component) {
184
- return this.removeByKey(entityId, component.name);
185
- }
186
- /**
187
- * Removes an entity from its entity map.
188
- * Removes any related Node entities that are mapped to the entity
189
- *
190
- * @example
191
- *
192
- * ```javascript
193
- * // remove an entity from its entity map
194
- * EntityMap.removeByKey(entityId, "PositionComponent");
195
- * ```
196
- */
197
- removeByKey(entityId, componentName) {
198
- let entityMap = this.componentMaps.get(componentName);
199
- if (entityMap === void 0)
200
- throw new ReferenceError(`A registered entity map does not exist for the given key: ${componentName}`);
201
- let entity = entityMap.get(entityId);
202
- if (entity === void 0) return false;
203
- return entityMap.delete(entityId);
204
- }
205
- /**
206
- * Destroys an entity across all component maps
207
- */
208
- destroyEntity(entityId) {
209
- this.componentMaps.forEach((dataMap) => {
210
- if (dataMap.has(entityId)) dataMap.delete(entityId);
211
- });
212
- }
213
- // TODO generate a non repeatable id for network play?
214
- getNextId() {
215
- this.nextId++;
216
- return this.nextId;
217
- }
218
- clear() {
219
- this.componentMaps.clear();
220
- return this;
221
- }
222
- /**
223
- * Prints all entity maps in a tabular format to the console
224
- */
225
- printTable() {
226
- this.componentMaps.forEach((map) => console.table(map.toTable(true)));
227
- return this;
228
- }
229
- /**
230
- * Parse's the JSON and returns an EntityMap object
231
- *
232
- * @example
233
- *
234
- * ```javascript
235
- * // remove an entity from its entity map
236
- * const json = JSON.stringfy(ecs);
237
- * const restoredMap = EntityMap.parse(json);
238
- * ```
239
- */
240
- static parse(json) {
241
- const ecs2 = new _EntityMap();
242
- const restored = JSON.parse(json, function(key, value) {
243
- if (value.hasOwnProperty("componentMaps")) {
244
- Reflect.setPrototypeOf(value, _EntityMap.prototype);
245
- return value;
246
- }
247
- if (value.hasOwnProperty("__KeyMap__")) {
248
- return new KeyMap(value.iterable);
249
- }
250
- return this[key];
251
- });
252
- ecs2.componentMaps = restored.componentMaps;
253
- ecs2.nextId = restored.nextId;
254
- return ecs2;
255
- }
256
- /**
257
- * A tracing method used for debugging.
258
- * Intercepts all functions specified and logs each call to the console.
259
- *
260
- * @method createWithTracing
261
- * @static
262
- * @param {Array} funcFilter A list of function names you want to intercept. If no function names are specified then will log all functions called
263
- * @return {EntityMap} A new entity map with tracing enabled
264
- */
265
- static createWithTracing(funcFilter) {
266
- let traceHandler = {
267
- get(target, propKey, receiver) {
268
- const targetValue = target[propKey];
269
- if (typeof targetValue === "function" && (funcFilter.length === 0 || funcFilter.includes(propKey))) {
270
- return function(...args) {
271
- console.groupCollapsed("ecs trace", propKey, args);
272
- console.trace();
273
- console.groupEnd();
274
- return targetValue.apply(this, args);
275
- };
276
- }
277
- return targetValue;
278
- }
279
- };
280
- return new Proxy(new _EntityMap(), traceHandler);
281
- }
282
- };
283
-
284
- // src/ecs.ts
285
- var ecs = new EntityMap();
286
- if (typeof window !== "undefined") {
287
- window.ecs = ecs;
288
- window.ecs.Component = Component;
289
- } else if (typeof module !== "undefined" && module !== null) {
290
- module.exports = { ecs, Component };
291
- }
292
- export {
293
- Component,
294
- ecs
295
- };
1
+ var s=class extends Map{constructor(e=[]){super(e)}firstEntry(){return this.size===0?void 0:this.entries().next().value}firstKey(){return this.size===0?void 0:this.keys().next().value}firstValue(){return this.size===0?void 0:this.values().next().value}toJSON(){return{__KeyMap__:1,iterable:[...this.entries()]}}toTable(e=!1){let t=[];for(let n of this.entries()){let r=n[1],o={};e==!1&&(o["entity.key"]=n[0]),o["entity.type"]=r.constructor.name,t.push({...o,...r})}return t}printTable(e=!1){console.table(this.toTable(e))}entries(){return super.entries().filter(e=>e!=null)}keys(){return super.keys().filter(e=>e!=null)}values(){return super.values().filter(e=>e!=null)}[Symbol.iterator](){return this.values()}static get[Symbol.species](){return Map}};var i=class a{componentMaps=new s;nextId=0;constructor(){}register(...e){for(let t of e){let n=t.name;if(n===void 0)throw new ReferenceError("The component class does not have a name defined. i.e. a constructor name");let r=new s;this.componentMaps.set(n,r)}return this}getMap(e){return this.componentMaps.get(e.name)}get(e,t){let n=this.componentMaps.get(t.name);if(n===void 0)throw new ReferenceError(`"${t}" component class does not exist in the componentMaps`);return n.get(e)}getByKey(e,t){let n=this.componentMaps.get(t);if(n===void 0)throw new ReferenceError(`"${t}" component does not exist in the componentMaps`);return n.get(e)}has(e,t){let n=this.componentMaps.get(t.name);return n===void 0?!1:n.has(e)}set(e,t){let n=this.componentMaps.get(t.constructor.name);if(n===void 0)throw new ReferenceError(`Component map does not exist using the name: ${t.constructor.name}`);return n.set(e,t),t}remove(e,t){return this.removeByKey(e,t.name)}removeByKey(e,t){let n=this.componentMaps.get(t);if(n===void 0)throw new ReferenceError(`A registered entity map does not exist for the given key: ${t}`);return n.get(e)===void 0?!1:n.delete(e)}destroyEntity(e){this.componentMaps.forEach(t=>{t.has(e)&&t.delete(e)})}getNextId(){return this.nextId++,this.nextId}clear(){return this.componentMaps.clear(),this}printTable(){return this.componentMaps.forEach(e=>console.table(e.toTable(!0))),this}static parse(e){let t=new a,n=JSON.parse(e,function(r,o){return o.hasOwnProperty("componentMaps")?(Reflect.setPrototypeOf(o,a.prototype),o):o.hasOwnProperty("__KeyMap__")?new s(o.iterable):this[r]});return t.componentMaps=n.componentMaps,t.nextId=n.nextId,t}static createWithTracing(e){let t={get(n,r,o){let p=n[r];return typeof p=="function"&&(e.length===0||e.includes(r))?function(...u){return console.groupCollapsed("ecs trace",r,u),console.trace(),console.groupEnd(),p.apply(this,u)}:p}};return new Proxy(new a,t)}};var l=new i;typeof window<"u"?window.ecs=l:typeof module<"u"&&module!==null&&(module.exports={ecs:l});export{l as ecs};
296
2
  //# sourceMappingURL=ecs.js.map
package/dist/ecs.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/definitions.ts", "../src/key-map.ts", "../src/entity-map.ts", "../src/ecs.ts"],
4
- "sourcesContent": ["import type { KeyMap } from './key-map.js'\n\nexport type KeyCollection<T> = { [key: string]: T }\n\nexport class Component { }\n\nexport type ComponentClass = {\n new(...args: any[]): Component\n}\n\nexport type ComponentDataMap = KeyMap<number, Component>\n\nexport type ComponentMap = KeyMap<string, ComponentDataMap>\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: any, value: any] | undefined {\n if (this.size === 0) return [undefined, undefined];\n\n const iterator = this.entries();\n const result = iterator.next();\n return result.value;\n }\n\n firstKey(): TKey | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.keys();\n const result = iterator.next();\n return result.value;\n }\n\n firstValue(): TValue | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.values();\n const result = iterator.next();\n return result.value;\n }\n\n toJSON() {\n return { __KeyMap__: 1, iterable: [...this.entries()] }\n }\n\n toTable(excludeKeys = false): any[] {\n const table = []\n for (const value of this.entries()) {\n let entity = value[1] as { new(): TValue }\n let meta: KeyCollection<any> = {}\n\n if (excludeKeys == false) meta[\"entity.key\"] = value[0]\n meta[\"entity.type\"] = entity.constructor.name\n\n table.push({ ...meta, ...entity })\n }\n return table\n }\n\n /**\n * Outputs keys and values in a tabular format to the console\n */\n printTable(excludeKeys = false): void {\n console.table(this.toTable(excludeKeys))\n }\n\n entries(): MapIterator<any> {\n return super.entries().filter(x => x !== undefined && x !== null)\n }\n\n keys(): MapIterator<any> {\n return super.keys().filter(x => x !== undefined && x !== null)\n }\n\n values(): MapIterator<any> {\n return super.values().filter(x => x !== undefined && x !== null)\n }\n\n // default iterator\n [Symbol.iterator]() {\n return this.values()\n }\n\n static get [Symbol.species]() { return Map; }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * @module ecsjs\n */\nimport {\n Component,\n type ComponentDataMap,\n type ComponentClass,\n type ComponentMap\n} from './definitions.js';\nimport { KeyMap } from './key-map.js'\n\n/**\n * Class for storing entities and their relationships\n * \n * @class EntityMap\n */\nexport class EntityMap {\n\n // entity class maps\n private componentMaps: ComponentMap = new KeyMap()\n\n private nextId: number = 0\n\n constructor() { }\n\n /**\n * Registers a component class\n * \n * @example\n *\n * ```javascript\n * // component class\n * class MyComponent {\n * constructor(x) {\n * this.x = x;\n * }\n * }\n *\n * ecs.register(MyComponent);\n * // or\n * ecs.register(MyComponent1, MyComponent2);\n * ```\n */\n register(...component: ComponentClass[]) {\n for (const definition of component) {\n const componentName = definition.name;\n if (componentName === undefined)\n throw new ReferenceError(\"The component class does not have a name defined. i.e. a constructor name\");\n\n // create the component map\n const componentDataMap: ComponentDataMap = new KeyMap();\n this.componentMaps.set(componentName, componentDataMap);\n }\n\n // chain\n return this;\n }\n\n /**\n * Retrieves an entity map by it's registered type\n */\n getMap(component: ComponentClass) {\n return this.componentMaps.get(component.name);\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.get(entityId, Components.PositionComponent);\n * ```\n */\n get(entityId: number, component: ComponentClass) {\n let cm = this.componentMaps.get(component.name);\n if (cm === undefined)\n throw new ReferenceError(`\"${component}\" component class does not exist in the componentMaps`);\n\n return cm.get(entityId);\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.getByKey(entityId, \"PositionComponent\");\n * ```\n */\n getByKey(entityId: number, componentName: string) {\n // get the component map\n let map = this.componentMaps.get(componentName);\n if (map === undefined)\n throw new ReferenceError(`\"${componentName}\" component does not exist in the componentMaps`);\n\n return map.get(entityId);\n }\n\n /**\n * Checks if the entity exists in it's registed component map\n * The entity type must be registered before calling this method\n * \n * @example\n *\n * ```javascript\n * // get an entity\n * let exists = ecs.has(entityId, PositionComponent);\n * ```\n */\n has(entityId: number, component: ComponentClass): boolean {\n // get the component map\n let map = this.componentMaps.get(component.name);\n if (map === undefined) return false\n\n return map.has(entityId);\n }\n\n /**\n * Adds or updates an entity to it's registered entity map\n *\n * @example\n *\n * ```javascript\n * // add/update an entity\n * let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));\n * ```\n */\n set<T extends Component>(entityId: number, componentData: Component): T {\n // get the component map\n let map = this.componentMaps.get(componentData.constructor.name);\n if (map === undefined)\n throw new ReferenceError(`Component map does not exist using the name: ${componentData.constructor.name}`);\n\n // set the entity on the entity map\n map.set(entityId, componentData);\n\n // return instance\n return componentData as T;\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.remove(entityId, Components.PositionComponent);\n * ```\n */\n remove(entityId: number, component: ComponentClass) {\n return this.removeByKey(entityId, component.name);\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.removeByKey(entityId, \"PositionComponent\");\n * ```\n */\n removeByKey(entityId: number, componentName: string) {\n // get the entity map\n let entityMap = this.componentMaps.get(componentName);\n\n // ensure the map is defined\n if (entityMap === undefined)\n throw new ReferenceError(`A registered entity map does not exist for the given key: ${componentName}`);\n\n // get the entity\n let entity = entityMap.get(entityId);\n if (entity === undefined) return false;\n\n // remove the entity from the entity map\n return entityMap.delete(entityId);\n }\n\n /**\n * Destroys an entity across all component maps\n */\n destroyEntity(entityId: number) {\n this.componentMaps.forEach((dataMap: ComponentDataMap) => {\n if (dataMap.has(entityId)) dataMap.delete(entityId);\n });\n }\n\n // TODO generate a non repeatable id for network play?\n getNextId() {\n this.nextId++;\n return this.nextId;\n }\n\n clear() {\n this.componentMaps.clear();\n return this;\n }\n\n /**\n * Prints all entity maps in a tabular format to the console\n */\n printTable() {\n this.componentMaps.forEach(map => console.table(map.toTable(true)));\n return this\n }\n\n /**\n * Parse's the JSON and returns an EntityMap object\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * const json = JSON.stringfy(ecs);\n * const restoredMap = EntityMap.parse(json);\n * ```\n */\n static parse(json: string) {\n const ecs = new EntityMap();\n\n const restored = JSON.parse(json, function (key: string, value: any) {\n\n if (value.hasOwnProperty('componentMaps')) {\n Reflect.setPrototypeOf(value, EntityMap.prototype);\n return value;\n }\n\n if (value.hasOwnProperty('__KeyMap__')) {\n return new KeyMap(value.iterable);\n }\n\n // TODO pass in user defined entity types ?\n // if (value.hasOwnProperty('uniqueid')) {\n // Reflect.setPrototypeOf(value, ecs.Entity.prototype);\n // return value;\n // }\n\n return this[key];\n });\n\n ecs.componentMaps = restored.componentMaps;\n ecs.nextId = restored.nextId;\n return ecs;\n }\n\n /**\n * A tracing method used for debugging.\n * Intercepts all functions specified and logs each call to the console.\n * \n * @method createWithTracing\n * @static\n * @param {Array} funcFilter A list of function names you want to intercept. If no function names are specified then will log all functions called\n * @return {EntityMap} A new entity map with tracing enabled\n */\n static createWithTracing(funcFilter: any) {\n let traceHandler = {\n get(target: any, propKey: string, receiver: any) {\n const targetValue = target[propKey]\n\n if (typeof targetValue === 'function' && (funcFilter.length === 0 || funcFilter.includes(propKey))) {\n return function (this: any, ...args: any[]) {\n console.groupCollapsed('ecs trace', propKey, args);\n console.trace();\n console.groupEnd();\n return targetValue.apply(this, args);\n }\n }\n\n return targetValue;\n }\n }\n\n return new Proxy(new EntityMap(), traceHandler)\n }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * ### An entity component system library for JavaScript\n *\n * @module ecsjs\n * @main ecsjs\n */\nimport { EntityMap } from './entity-map.js'\nimport { Component } from './definitions.js';\n\nexport { Component } from './definitions.js';\nexport const ecs = new EntityMap()\n\nif (typeof window !== 'undefined') {\n // @ts-ignore: exports to window\n window.ecs = ecs\n // @ts-ignore\n window.ecs.Component = Component\n} else if (typeof module !== 'undefined' && module !== null) {\n // exports to nodejs\n module.exports = { ecs, Component };\n}"],
5
- "mappings": ";AAIO,IAAM,YAAN,MAAgB;AAAE;;;ACkBlB,IAAM,SAAN,cAAmC,IAAkB;AAAA,EAE1D,YAAY,UAAiB,CAAC,GAAG;AAC/B,UAAM,OAAO;AAAA,EACf;AAAA,EAEA,aAAiD;AAC/C,QAAI,KAAK,SAAS,EAAG,QAAO,CAAC,QAAW,MAAS;AAEjD,UAAM,WAAW,KAAK,QAAQ;AAC9B,UAAM,SAAS,SAAS,KAAK;AAC7B,WAAO,OAAO;AAAA,EAChB;AAAA,EAEA,WAA6B;AAC3B,QAAI,KAAK,SAAS,EAAG,QAAO;AAE5B,UAAM,WAAW,KAAK,KAAK;AAC3B,UAAM,SAAS,SAAS,KAAK;AAC7B,WAAO,OAAO;AAAA,EAChB;AAAA,EAEA,aAAiC;AAC/B,QAAI,KAAK,SAAS,EAAG,QAAO;AAE5B,UAAM,WAAW,KAAK,OAAO;AAC7B,UAAM,SAAS,SAAS,KAAK;AAC7B,WAAO,OAAO;AAAA,EAChB;AAAA,EAEA,SAAS;AACP,WAAO,EAAE,YAAY,GAAG,UAAU,CAAC,GAAG,KAAK,QAAQ,CAAC,EAAE;AAAA,EACxD;AAAA,EAEA,QAAQ,cAAc,OAAc;AAClC,UAAM,QAAQ,CAAC;AACf,eAAW,SAAS,KAAK,QAAQ,GAAG;AAClC,UAAI,SAAS,MAAM,CAAC;AACpB,UAAI,OAA2B,CAAC;AAEhC,UAAI,eAAe,MAAO,MAAK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AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,OAAO,kBAAkB,YAAiB;AACxC,QAAI,eAAe;AAAA,MACjB,IAAI,QAAa,SAAiB,UAAe;AAC/C,cAAM,cAAc,OAAO,OAAO;AAElC,YAAI,OAAO,gBAAgB,eAAe,WAAW,WAAW,KAAK,WAAW,SAAS,OAAO,IAAI;AAClG,iBAAO,YAAwB,MAAa;AAC1C,oBAAQ,eAAe,aAAa,SAAS,IAAI;AACjD,oBAAQ,MAAM;AACd,oBAAQ,SAAS;AACjB,mBAAO,YAAY,MAAM,MAAM,IAAI;AAAA,UACrC;AAAA,QACF;AAEA,eAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO,IAAI,MAAM,IAAI,WAAU,GAAG,YAAY;AAAA,EAChD;AAEF;;;ACjRO,IAAM,MAAM,IAAI,UAAU;AAEjC,IAAI,OAAO,WAAW,aAAa;AAEjC,SAAO,MAAM;AAEb,SAAO,IAAI,YAAY;AACzB,WAAW,OAAO,WAAW,eAAe,WAAW,MAAM;AAE3D,SAAO,UAAU,EAAE,KAAK,UAAU;AACpC;",
6
- "names": ["ecs"]
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<any> {\n return super.entries().filter(x => x !== undefined && x !== null)\n }\n\n keys(): MapIterator<any> {\n return super.keys().filter(x => x !== undefined && x !== null)\n }\n\n values(): MapIterator<any> {\n return super.values().filter(x => x !== undefined && x !== null)\n }\n\n // default iterator\n [Symbol.iterator]() {\n return this.values()\n }\n\n static get [Symbol.species]() { return Map; }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * @module ecsjs\n */\nimport {\n type ComponentDataMap,\n type ComponentClass,\n type ComponentMap,\n type IComponent\n} from './definitions.js';\nimport { KeyMap } from './key-map.js'\n\n/**\n * Class for storing entities and their relationships\n * \n * @class EntityMap\n */\nexport class EntityMap {\n\n // entity class maps\n private componentMaps: ComponentMap = new KeyMap()\n\n private nextId: number = 0\n\n constructor() { }\n\n /**\n * Registers a component class\n * \n * @example\n *\n * ```javascript\n * // component class\n * class MyComponent {\n * constructor(x) {\n * this.x = x;\n * }\n * }\n *\n * ecs.register(MyComponent);\n * // or\n * ecs.register(MyComponent1, MyComponent2);\n * ```\n */\n register(...component: ComponentClass<any>[]) {\n for (const definition of component) {\n const componentName = definition.name;\n if (componentName === undefined)\n throw new ReferenceError(\"The component class does not have a name defined. i.e. a constructor name\");\n\n // create the component map\n const componentDataMap: ComponentDataMap<any> = new KeyMap();\n this.componentMaps.set(componentName, componentDataMap);\n }\n\n // chain\n return this;\n }\n\n /**\n * Retrieves an entity map by it's registered type\n */\n getMap<T extends IComponent>(component: ComponentClass<T>): ComponentDataMap<T> | undefined {\n return this.componentMaps.get(component.name);\n }\n\n /**\n * 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, receiver: any) {\n const targetValue = target[propKey]\n\n if (typeof targetValue === 'function' && (funcFilter.length === 0 || funcFilter.includes(propKey))) {\n return function (this: any, ...args: any[]) {\n console.groupCollapsed('ecs trace', propKey, args);\n console.trace();\n console.groupEnd();\n return targetValue.apply(this, args);\n }\n }\n\n return targetValue;\n }\n }\n\n return new Proxy(new EntityMap(), traceHandler)\n }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * ### An entity component system library for JavaScript\n *\n * @module ecsjs\n * @main ecsjs\n */\nimport { EntityMap } from './entity-map.js'\n\nexport const ecs = new EntityMap()\n\nif (typeof window !== 'undefined') {\n // @ts-ignore: exports to window\n window.ecs = ecs\n} else if (typeof module !== 'undefined' && module !== null) {\n // exports to nodejs\n module.exports = { ecs };\n}"],
5
+ "mappings": "AAsBO,IAAMA,EAAN,cAAmC,GAAkB,CAE1D,YAAYC,EAAiB,CAAC,EAAG,CAC/B,MAAMA,CAAO,CACf,CAEA,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,QAAWC,KAAS,KAAK,QAAQ,EAAG,CAClC,IAAIC,EAASD,EAAM,CAAC,EAChBE,EAA2B,CAAC,EAE5BJ,GAAe,KAAOI,EAAK,YAAY,EAAIF,EAAM,CAAC,GACtDE,EAAK,aAAa,EAAID,EAAO,YAAY,KAEzCF,EAAM,KAAK,CAAE,GAAGG,EAAM,GAAGD,CAAO,CAAC,CACnC,CACA,OAAOF,CACT,CAKA,WAAWD,EAAc,GAAa,CACpC,QAAQ,MAAM,KAAK,QAAQA,CAAW,CAAC,CACzC,CAEA,SAA4B,CAC1B,OAAO,MAAM,QAAQ,EAAE,OAAOK,GAAwBA,GAAM,IAAI,CAClE,CAEA,MAAyB,CACvB,OAAO,MAAM,KAAK,EAAE,OAAOA,GAAwBA,GAAM,IAAI,CAC/D,CAEA,QAA2B,CACzB,OAAO,MAAM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1C,eAAQ,eAAe,YAAaH,EAASG,CAAI,EACjD,QAAQ,MAAM,EACd,QAAQ,SAAS,EACVD,EAAY,MAAM,KAAMC,CAAI,CACrC,EAGKD,CACT,CACF,EAEA,OAAO,IAAI,MAAM,IAAItB,EAAakB,CAAY,CAChD,CAEF,EC7QO,IAAMM,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", "value", "entity", "meta", "x", "EntityMap", "_EntityMap", "KeyMap", "component", "definition", "componentName", "componentDataMap", "entityId", "cm", "map", "componentData", "entityMap", "dataMap", "json", "ecs", "restored", "key", "value", "funcFilter", "traceHandler", "target", "propKey", "receiver", "targetValue", "args", "ecs", "EntityMap"]
7
7
  }
@@ -2,11 +2,11 @@ import type { KeyMap } from './key-map.js';
2
2
  export type KeyCollection<T> = {
3
3
  [key: string]: T;
4
4
  };
5
- export declare class Component {
5
+ export interface IComponent {
6
6
  }
7
- export type ComponentClass = {
8
- new (...args: any[]): Component;
7
+ export type ComponentClass<T extends IComponent> = {
8
+ new (...args: any[]): T;
9
9
  };
10
- export type ComponentDataMap = KeyMap<number, Component>;
11
- export type ComponentMap = KeyMap<string, ComponentDataMap>;
10
+ export type ComponentDataMap<T> = KeyMap<number, T>;
11
+ export type ComponentMap = KeyMap<string, ComponentDataMap<any>>;
12
12
  //# sourceMappingURL=definitions.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"definitions.d.ts","sourceRoot":"","sources":["../../../src/definitions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAE1C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,CAAA;CAAE,CAAA;AAEnD,qBAAa,SAAS;CAAI;AAE1B,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAI,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,SAAS,CAAA;CAC/B,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAExD,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,gBAAgB,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;AAEnD,MAAM,WAAW,UAAU;CAAI;AAE/B,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,UAAU,IAAI;IACjD,KAAI,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;CACvB,CAAA;AAED,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;AAEnD,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAA"}
@@ -5,6 +5,5 @@
5
5
  * @main ecsjs
6
6
  */
7
7
  import { EntityMap } from './entity-map.js';
8
- export { Component } from './definitions.js';
9
8
  export declare const ecs: EntityMap;
10
9
  //# sourceMappingURL=ecs.d.ts.map
@@ -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;AAG3C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,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;AAE3C,eAAO,MAAM,GAAG,WAAkB,CAAA"}
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module ecsjs
3
3
  */
4
- import { Component, type ComponentDataMap, type ComponentClass } from './definitions.js';
4
+ import { type ComponentDataMap, type ComponentClass, type IComponent } from './definitions.js';
5
5
  /**
6
6
  * Class for storing entities and their relationships
7
7
  *
@@ -29,11 +29,11 @@ export declare class EntityMap {
29
29
  * ecs.register(MyComponent1, MyComponent2);
30
30
  * ```
31
31
  */
32
- register(...component: ComponentClass[]): this;
32
+ register(...component: ComponentClass<any>[]): this;
33
33
  /**
34
34
  * Retrieves an entity map by it's registered type
35
35
  */
36
- getMap(component: ComponentClass): ComponentDataMap | undefined;
36
+ getMap<T extends IComponent>(component: ComponentClass<T>): ComponentDataMap<T> | undefined;
37
37
  /**
38
38
  * Retrieves an entity from its registered entity map
39
39
  *
@@ -44,7 +44,7 @@ export declare class EntityMap {
44
44
  * let positionData = ecs.get(entityId, Components.PositionComponent);
45
45
  * ```
46
46
  */
47
- get(entityId: number, component: ComponentClass): Component | undefined;
47
+ get<T extends IComponent>(entityId: number, component: ComponentClass<T>): T;
48
48
  /**
49
49
  * Retrieves an entity from its registered entity map
50
50
  *
@@ -55,7 +55,7 @@ export declare class EntityMap {
55
55
  * let positionData = ecs.getByKey(entityId, "PositionComponent");
56
56
  * ```
57
57
  */
58
- getByKey(entityId: number, componentName: string): Component | undefined;
58
+ getByKey(entityId: number, componentName: string): any;
59
59
  /**
60
60
  * Checks if the entity exists in it's registed component map
61
61
  * The entity type must be registered before calling this method
@@ -67,7 +67,7 @@ export declare class EntityMap {
67
67
  * let exists = ecs.has(entityId, PositionComponent);
68
68
  * ```
69
69
  */
70
- has(entityId: number, component: ComponentClass): boolean;
70
+ has<T extends IComponent>(entityId: number, component: ComponentClass<T>): boolean;
71
71
  /**
72
72
  * Adds or updates an entity to it's registered entity map
73
73
  *
@@ -78,7 +78,7 @@ export declare class EntityMap {
78
78
  * let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));
79
79
  * ```
80
80
  */
81
- set<T extends Component>(entityId: number, componentData: Component): T;
81
+ set<T extends IComponent>(entityId: number, componentData: T): T;
82
82
  /**
83
83
  * Removes an entity from its entity map.
84
84
  * Removes any related Node entities that are mapped to the entity
@@ -90,7 +90,7 @@ export declare class EntityMap {
90
90
  * EntityMap.remove(entityId, Components.PositionComponent);
91
91
  * ```
92
92
  */
93
- remove(entityId: number, component: ComponentClass): boolean;
93
+ remove<T extends IComponent>(entityId: number, component: ComponentClass<T>): boolean;
94
94
  /**
95
95
  * Removes an entity from its entity map.
96
96
  * Removes any related Node entities that are mapped to the entity
@@ -1 +1 @@
1
- {"version":3,"file":"entity-map.d.ts","sourceRoot":"","sources":["../../../src/entity-map.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,OAAO,EACL,SAAS,EACT,KAAK,gBAAgB,EACrB,KAAK,cAAc,EAEpB,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,EAAE;IAevC;;OAEG;IACH,MAAM,CAAC,SAAS,EAAE,cAAc;IAIhC;;;;;;;;;OASG;IACH,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc;IAQ/C;;;;;;;;;OASG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM;IAShD;;;;;;;;;;OAUG;IACH,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc,GAAG,OAAO;IAQzD;;;;;;;;;OASG;IACH,GAAG,C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AAE,MAAM;IAShD;;;;;;;;;;OAUG;IACH,GAAG,CAAC,CAAC,SAAS,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,OAAO;IAQlF;;;;;;;;;OASG;IACH,GAAG,CAAC,CAAC,SAAS,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC,GAAG,CAAC;IAahE;;;;;;;;;;OAUG;IACH,MAAM,CAAC,CAAC,SAAS,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC;IAI3E;;;;;;;;;;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;;;;;;;;;;OAUG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM;IAsBzB;;;;;;;;OAQG;IACH,MAAM,CAAC,iBAAiB,CAAC,UAAU,EAAE,GAAG;CAqBzC"}
@@ -3,7 +3,7 @@
3
3
  */
4
4
  export declare class KeyMap<TKey, TValue> extends Map<TKey, TValue> {
5
5
  constructor(entries?: any[]);
6
- firstEntry(): [key: any, value: any] | undefined;
6
+ firstEntry(): [key: TKey, value: TValue] | undefined;
7
7
  firstKey(): TKey | undefined;
8
8
  firstValue(): TValue | undefined;
9
9
  toJSON(): {
@@ -1 +1 @@
1
- {"version":3,"file":"key-map.d.ts","sourceRoot":"","sources":["../../../src/key-map.ts"],"names":[],"mappings":"AAmBA;;GAEG;AACH,qBAAa,MAAM,CAAC,IAAI,EAAE,MAAM,CAAE,SAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC;gBAE7C,OAAO,GAAE,GAAG,EAAO;IAI/B,UAAU,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK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
1
  {
2
2
  "title": "ECS JS",
3
3
  "name": "ecsjs",
4
- "version": "1.0.0-beta.1",
4
+ "version": "1.0.0-beta.3",
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",
@@ -2,12 +2,12 @@ import type { KeyMap } from './key-map.js'
2
2
 
3
3
  export type KeyCollection<T> = { [key: string]: T }
4
4
 
5
- export class Component { }
5
+ export interface IComponent { }
6
6
 
7
- export type ComponentClass = {
8
- new(...args: any[]): Component
7
+ export type ComponentClass<T extends IComponent> = {
8
+ new(...args: any[]): T
9
9
  }
10
10
 
11
- export type ComponentDataMap = KeyMap<number, Component>
11
+ export type ComponentDataMap<T> = KeyMap<number, T>
12
12
 
13
- export type ComponentMap = KeyMap<string, ComponentDataMap>
13
+ export type ComponentMap = KeyMap<string, ComponentDataMap<any>>
package/src/ecs.ts CHANGED
@@ -23,17 +23,13 @@
23
23
  * @main ecsjs
24
24
  */
25
25
  import { EntityMap } from './entity-map.js'
26
- import { Component } from './definitions.js';
27
26
 
28
- export { Component } from './definitions.js';
29
27
  export const ecs = new EntityMap()
30
28
 
31
29
  if (typeof window !== 'undefined') {
32
30
  // @ts-ignore: exports to window
33
31
  window.ecs = ecs
34
- // @ts-ignore
35
- window.ecs.Component = Component
36
32
  } else if (typeof module !== 'undefined' && module !== null) {
37
33
  // exports to nodejs
38
- module.exports = { ecs, Component };
34
+ module.exports = { ecs };
39
35
  }
package/src/entity-map.ts CHANGED
@@ -20,10 +20,10 @@
20
20
  * @module ecsjs
21
21
  */
22
22
  import {
23
- Component,
24
23
  type ComponentDataMap,
25
24
  type ComponentClass,
26
- type ComponentMap
25
+ type ComponentMap,
26
+ type IComponent
27
27
  } from './definitions.js';
28
28
  import { KeyMap } from './key-map.js'
29
29
 
@@ -59,14 +59,14 @@ export class EntityMap {
59
59
  * ecs.register(MyComponent1, MyComponent2);
60
60
  * ```
61
61
  */
62
- register(...component: ComponentClass[]) {
62
+ register(...component: ComponentClass<any>[]) {
63
63
  for (const definition of component) {
64
64
  const componentName = definition.name;
65
65
  if (componentName === undefined)
66
66
  throw new ReferenceError("The component class does not have a name defined. i.e. a constructor name");
67
67
 
68
68
  // create the component map
69
- const componentDataMap: ComponentDataMap = new KeyMap();
69
+ const componentDataMap: ComponentDataMap<any> = new KeyMap();
70
70
  this.componentMaps.set(componentName, componentDataMap);
71
71
  }
72
72
 
@@ -77,7 +77,7 @@ export class EntityMap {
77
77
  /**
78
78
  * Retrieves an entity map by it's registered type
79
79
  */
80
- getMap(component: ComponentClass) {
80
+ getMap<T extends IComponent>(component: ComponentClass<T>): ComponentDataMap<T> | undefined {
81
81
  return this.componentMaps.get(component.name);
82
82
  }
83
83
 
@@ -91,7 +91,7 @@ export class EntityMap {
91
91
  * let positionData = ecs.get(entityId, Components.PositionComponent);
92
92
  * ```
93
93
  */
94
- get(entityId: number, component: ComponentClass) {
94
+ get<T extends IComponent>(entityId: number, component: ComponentClass<T>): T {
95
95
  let cm = this.componentMaps.get(component.name);
96
96
  if (cm === undefined)
97
97
  throw new ReferenceError(`"${component}" component class does not exist in the componentMaps`);
@@ -129,7 +129,7 @@ export class EntityMap {
129
129
  * let exists = ecs.has(entityId, PositionComponent);
130
130
  * ```
131
131
  */
132
- has(entityId: number, component: ComponentClass): boolean {
132
+ has<T extends IComponent>(entityId: number, component: ComponentClass<T>): boolean {
133
133
  // get the component map
134
134
  let map = this.componentMaps.get(component.name);
135
135
  if (map === undefined) return false
@@ -147,7 +147,7 @@ export class EntityMap {
147
147
  * let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));
148
148
  * ```
149
149
  */
150
- set<T extends Component>(entityId: number, componentData: Component): T {
150
+ set<T extends IComponent>(entityId: number, componentData: T): T {
151
151
  // get the component map
152
152
  let map = this.componentMaps.get(componentData.constructor.name);
153
153
  if (map === undefined)
@@ -171,7 +171,7 @@ export class EntityMap {
171
171
  * EntityMap.remove(entityId, Components.PositionComponent);
172
172
  * ```
173
173
  */
174
- remove(entityId: number, component: ComponentClass) {
174
+ remove<T extends IComponent>(entityId: number, component: ComponentClass<T>) {
175
175
  return this.removeByKey(entityId, component.name);
176
176
  }
177
177
 
@@ -206,7 +206,7 @@ export class EntityMap {
206
206
  * Destroys an entity across all component maps
207
207
  */
208
208
  destroyEntity(entityId: number) {
209
- this.componentMaps.forEach((dataMap: ComponentDataMap) => {
209
+ this.componentMaps.forEach((dataMap: ComponentDataMap<any>) => {
210
210
  if (dataMap.has(entityId)) dataMap.delete(entityId);
211
211
  });
212
212
  }
@@ -255,12 +255,6 @@ export class EntityMap {
255
255
  return new KeyMap(value.iterable);
256
256
  }
257
257
 
258
- // TODO pass in user defined entity types ?
259
- // if (value.hasOwnProperty('uniqueid')) {
260
- // Reflect.setPrototypeOf(value, ecs.Entity.prototype);
261
- // return value;
262
- // }
263
-
264
258
  return this[key];
265
259
  });
266
260
 
package/src/key-map.ts CHANGED
@@ -26,8 +26,8 @@ export class KeyMap<TKey, TValue> extends Map<TKey, TValue> {
26
26
  super(entries)
27
27
  }
28
28
 
29
- firstEntry(): [key: any, value: any] | undefined {
30
- if (this.size === 0) return [undefined, undefined];
29
+ firstEntry(): [key: TKey, value: TValue] | undefined {
30
+ if (this.size === 0) return undefined;
31
31
 
32
32
  const iterator = this.entries();
33
33
  const result = iterator.next();