@codehz/ecs 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. package/index.js +17 -15
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -259,7 +259,7 @@ class Archetype {
259
259
  this.entityToIndex.set(entityId, index);
260
260
  for (const componentType of this.componentTypes) {
261
261
  const data = componentData.get(componentType);
262
- this.componentData.get(componentType).push(data === undefined ? MISSING_COMPONENT : data);
262
+ this.getComponentData(componentType).push(data === undefined ? MISSING_COMPONENT : data);
263
263
  }
264
264
  }
265
265
  removeEntity(entityId) {
@@ -271,7 +271,7 @@ class Archetype {
271
271
  this.entityToIndex.delete(entityId);
272
272
  const removedData = new Map;
273
273
  for (const componentType of this.componentTypes) {
274
- const dataArray = this.componentData.get(componentType);
274
+ const dataArray = this.getComponentData(componentType);
275
275
  removedData.set(componentType, dataArray[index]);
276
276
  dataArray.splice(index, 1);
277
277
  }
@@ -295,7 +295,7 @@ class Archetype {
295
295
  for (const relType of this.componentTypes) {
296
296
  const relDetailed = getDetailedIdType(relType);
297
297
  if ((relDetailed.type === "entity-relation" || relDetailed.type === "component-relation") && relDetailed.componentId === componentId) {
298
- const dataArray = this.componentData.get(relType);
298
+ const dataArray = this.getComponentData(relType);
299
299
  if (dataArray && dataArray[index] !== undefined) {
300
300
  const data = dataArray[index];
301
301
  relations.push([relDetailed.targetId, data === MISSING_COMPONENT ? undefined : data]);
@@ -304,7 +304,7 @@ class Archetype {
304
304
  }
305
305
  return relations;
306
306
  } else {
307
- const data = this.componentData.get(componentType)?.[index];
307
+ const data = this.getComponentData(componentType)[index];
308
308
  return data === MISSING_COMPONENT ? undefined : data;
309
309
  }
310
310
  }
@@ -316,14 +316,18 @@ class Archetype {
316
316
  if (index === undefined) {
317
317
  throw new Error(`Entity ${entityId} is not in this archetype`);
318
318
  }
319
- const dataArray = this.componentData.get(componentType);
319
+ const dataArray = this.getComponentData(componentType);
320
320
  dataArray[index] = data;
321
321
  }
322
322
  getEntities() {
323
- return [...this.entities];
323
+ return this.entities;
324
324
  }
325
325
  getComponentData(componentType) {
326
- return [...this.componentData.get(componentType) || []];
326
+ const data = this.componentData.get(componentType);
327
+ if (!data) {
328
+ throw new Error(`Component type ${componentType} is not in this archetype`);
329
+ }
330
+ return data;
327
331
  }
328
332
  getEntitiesWithComponents(componentTypes) {
329
333
  const result = [];
@@ -347,7 +351,7 @@ class Archetype {
347
351
  });
348
352
  return matchingRelations;
349
353
  } else {
350
- return this.componentData.get(compType);
354
+ return this.getComponentData(compType);
351
355
  }
352
356
  });
353
357
  });
@@ -359,12 +363,10 @@ class Archetype {
359
363
  const matchingRelations = dataSource;
360
364
  const relations = [];
361
365
  for (const relType of matchingRelations) {
362
- const dataArray = this.componentData.get(relType);
363
- if (dataArray && dataArray[entityIndex] !== undefined) {
364
- const data = dataArray[entityIndex];
365
- const decodedRel = decodeRelationId(relType);
366
- relations.push([decodedRel.targetId, data === MISSING_COMPONENT ? undefined : data]);
367
- }
366
+ const dataArray = this.getComponentData(relType);
367
+ const data = dataArray[entityIndex];
368
+ const decodedRel = decodeRelationId(relType);
369
+ relations.push([decodedRel.targetId, data === MISSING_COMPONENT ? undefined : data]);
368
370
  }
369
371
  return relations;
370
372
  } else {
@@ -380,7 +382,7 @@ class Archetype {
380
382
  for (let i = 0;i < this.entities.length; i++) {
381
383
  const components = new Map;
382
384
  for (const componentType of this.componentTypes) {
383
- const data = this.componentData.get(componentType)[i];
385
+ const data = this.getComponentData(componentType)[i];
384
386
  components.set(componentType, data === MISSING_COMPONENT ? undefined : data);
385
387
  }
386
388
  callback(this.entities[i], components);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codehz/ecs",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",