@codehz/ecs 0.1.5 → 0.1.6

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 +12 -11
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -280,8 +280,9 @@ class Archetype {
280
280
  const removedData = new Map;
281
281
  for (const componentType of this.componentTypes) {
282
282
  const dataArray = this.getComponentData(componentType);
283
- removedData.set(componentType, dataArray[index] === MISSING_COMPONENT ? undefined : dataArray[index]);
283
+ removedData.set(componentType, dataArray[index]);
284
284
  }
285
+ this.entityToIndex.delete(entityId);
285
286
  const lastIndex = this.entities.length - 1;
286
287
  if (index !== lastIndex) {
287
288
  const lastEntity = this.entities[lastIndex];
@@ -289,11 +290,13 @@ class Archetype {
289
290
  this.entityToIndex.set(lastEntity, index);
290
291
  for (const componentType of this.componentTypes) {
291
292
  const dataArray = this.getComponentData(componentType);
292
- [dataArray[index], dataArray[lastIndex]] = [dataArray[lastIndex], dataArray[index]];
293
+ dataArray[index] = dataArray[lastIndex];
293
294
  }
294
295
  }
295
296
  this.entities.pop();
296
- this.entityToIndex.delete(entityId);
297
+ for (const componentType of this.componentTypes) {
298
+ this.getComponentData(componentType).pop();
299
+ }
297
300
  return removedData;
298
301
  }
299
302
  exists(entityId) {
@@ -436,14 +439,13 @@ class ComponentChangeset {
436
439
  }
437
440
  }
438
441
  applyTo(existingComponents) {
439
- const finalComponents = new Map(existingComponents);
440
442
  for (const componentType of this.removes) {
441
- finalComponents.delete(componentType);
443
+ existingComponents.delete(componentType);
442
444
  }
443
445
  for (const [componentType, component2] of this.adds) {
444
- finalComponents.set(componentType, component2);
446
+ existingComponents.set(componentType, component2);
445
447
  }
446
- return finalComponents;
448
+ return existingComponents;
447
449
  }
448
450
  }
449
451
 
@@ -734,8 +736,7 @@ class World {
734
736
  currentComponents.set(archetypeComponentType, componentData);
735
737
  }
736
738
  }
737
- const newComponentTypes = Array.from(currentComponents.keys()).sort((a, b) => a - b);
738
- const newArchetype = this.ensureArchetype(newComponentTypes);
739
+ const newArchetype = this.ensureArchetype(currentComponents.keys());
739
740
  sourceArchetype.removeEntity(sourceEntityId);
740
741
  if (sourceArchetype.getEntities().length === 0) {
741
742
  this.cleanupEmptyArchetype(sourceArchetype);
@@ -1000,7 +1001,7 @@ class World {
1000
1001
  const currentComponentTypes = currentArchetype.componentTypes;
1001
1002
  const needsArchetypeChange = finalComponents.size !== currentComponentTypes.length || !currentComponentTypes.every((type) => finalComponents.has(type));
1002
1003
  if (needsArchetypeChange) {
1003
- const newArchetype = this.ensureArchetype(finalComponents.keys().toArray());
1004
+ const newArchetype = this.ensureArchetype(finalComponents.keys());
1004
1005
  currentArchetype.removeEntity(entityId);
1005
1006
  newArchetype.addEntity(entityId, finalComponents);
1006
1007
  this.entityToArchetype.set(entityId, newArchetype);
@@ -1031,7 +1032,7 @@ class World {
1031
1032
  return changeset;
1032
1033
  }
1033
1034
  ensureArchetype(componentTypes) {
1034
- const sortedTypes = componentTypes.toSorted((a, b) => a - b);
1035
+ const sortedTypes = Array.from(componentTypes).sort((a, b) => a - b);
1035
1036
  const hashKey = this.createArchetypeSignature(sortedTypes);
1036
1037
  return getOrCreateWithSideEffect(this.archetypeBySignature, hashKey, () => {
1037
1038
  const newArchetype = new Archetype(sortedTypes);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codehz/ecs",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",