@codehz/ecs 0.1.4 → 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.
- package/changeset.d.ts +0 -4
- package/index.js +22 -20
- package/package.json +1 -1
package/changeset.d.ts
CHANGED
|
@@ -29,8 +29,4 @@ export declare class ComponentChangeset {
|
|
|
29
29
|
* Apply the changeset to existing components and return the final state
|
|
30
30
|
*/
|
|
31
31
|
applyTo(existingComponents: Map<EntityId<any>, any>): Map<EntityId<any>, any>;
|
|
32
|
-
/**
|
|
33
|
-
* Get the final component types after applying changes
|
|
34
|
-
*/
|
|
35
|
-
getFinalComponentTypes(existingComponents: Map<EntityId<any>, any>): EntityId<any>[];
|
|
36
32
|
}
|
package/index.js
CHANGED
|
@@ -277,16 +277,25 @@ class Archetype {
|
|
|
277
277
|
if (index === undefined) {
|
|
278
278
|
return;
|
|
279
279
|
}
|
|
280
|
-
this.entities.splice(index, 1);
|
|
281
|
-
this.entityToIndex.delete(entityId);
|
|
282
280
|
const removedData = new Map;
|
|
283
281
|
for (const componentType of this.componentTypes) {
|
|
284
282
|
const dataArray = this.getComponentData(componentType);
|
|
285
283
|
removedData.set(componentType, dataArray[index]);
|
|
286
|
-
dataArray.splice(index, 1);
|
|
287
284
|
}
|
|
288
|
-
|
|
289
|
-
|
|
285
|
+
this.entityToIndex.delete(entityId);
|
|
286
|
+
const lastIndex = this.entities.length - 1;
|
|
287
|
+
if (index !== lastIndex) {
|
|
288
|
+
const lastEntity = this.entities[lastIndex];
|
|
289
|
+
this.entities[index] = lastEntity;
|
|
290
|
+
this.entityToIndex.set(lastEntity, index);
|
|
291
|
+
for (const componentType of this.componentTypes) {
|
|
292
|
+
const dataArray = this.getComponentData(componentType);
|
|
293
|
+
dataArray[index] = dataArray[lastIndex];
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
this.entities.pop();
|
|
297
|
+
for (const componentType of this.componentTypes) {
|
|
298
|
+
this.getComponentData(componentType).pop();
|
|
290
299
|
}
|
|
291
300
|
return removedData;
|
|
292
301
|
}
|
|
@@ -430,18 +439,13 @@ class ComponentChangeset {
|
|
|
430
439
|
}
|
|
431
440
|
}
|
|
432
441
|
applyTo(existingComponents) {
|
|
433
|
-
const finalComponents = new Map(existingComponents);
|
|
434
442
|
for (const componentType of this.removes) {
|
|
435
|
-
|
|
443
|
+
existingComponents.delete(componentType);
|
|
436
444
|
}
|
|
437
445
|
for (const [componentType, component2] of this.adds) {
|
|
438
|
-
|
|
446
|
+
existingComponents.set(componentType, component2);
|
|
439
447
|
}
|
|
440
|
-
return
|
|
441
|
-
}
|
|
442
|
-
getFinalComponentTypes(existingComponents) {
|
|
443
|
-
const finalComponents = this.applyTo(existingComponents);
|
|
444
|
-
return Array.from(finalComponents.keys()).sort((a, b) => a - b);
|
|
448
|
+
return existingComponents;
|
|
445
449
|
}
|
|
446
450
|
}
|
|
447
451
|
|
|
@@ -732,8 +736,7 @@ class World {
|
|
|
732
736
|
currentComponents.set(archetypeComponentType, componentData);
|
|
733
737
|
}
|
|
734
738
|
}
|
|
735
|
-
const
|
|
736
|
-
const newArchetype = this.ensureArchetype(newComponentTypes);
|
|
739
|
+
const newArchetype = this.ensureArchetype(currentComponents.keys());
|
|
737
740
|
sourceArchetype.removeEntity(sourceEntityId);
|
|
738
741
|
if (sourceArchetype.getEntities().length === 0) {
|
|
739
742
|
this.cleanupEmptyArchetype(sourceArchetype);
|
|
@@ -995,11 +998,10 @@ class World {
|
|
|
995
998
|
}
|
|
996
999
|
}
|
|
997
1000
|
const finalComponents = changeset.applyTo(currentComponents);
|
|
998
|
-
const
|
|
999
|
-
const
|
|
1000
|
-
const needsArchetypeChange = finalComponentTypes.length !== currentComponentTypes.length || !finalComponentTypes.every((type, index) => type === currentComponentTypes[index]);
|
|
1001
|
+
const currentComponentTypes = currentArchetype.componentTypes;
|
|
1002
|
+
const needsArchetypeChange = finalComponents.size !== currentComponentTypes.length || !currentComponentTypes.every((type) => finalComponents.has(type));
|
|
1001
1003
|
if (needsArchetypeChange) {
|
|
1002
|
-
const newArchetype = this.ensureArchetype(
|
|
1004
|
+
const newArchetype = this.ensureArchetype(finalComponents.keys());
|
|
1003
1005
|
currentArchetype.removeEntity(entityId);
|
|
1004
1006
|
newArchetype.addEntity(entityId, finalComponents);
|
|
1005
1007
|
this.entityToArchetype.set(entityId, newArchetype);
|
|
@@ -1030,7 +1032,7 @@ class World {
|
|
|
1030
1032
|
return changeset;
|
|
1031
1033
|
}
|
|
1032
1034
|
ensureArchetype(componentTypes) {
|
|
1033
|
-
const sortedTypes =
|
|
1035
|
+
const sortedTypes = Array.from(componentTypes).sort((a, b) => a - b);
|
|
1034
1036
|
const hashKey = this.createArchetypeSignature(sortedTypes);
|
|
1035
1037
|
return getOrCreateWithSideEffect(this.archetypeBySignature, hashKey, () => {
|
|
1036
1038
|
const newArchetype = new Archetype(sortedTypes);
|