@codehz/ecs 0.1.4 → 0.1.5
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 +16 -15
- 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,17 +277,23 @@ 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
|
-
removedData.set(componentType, dataArray[index]);
|
|
286
|
-
dataArray.splice(index, 1);
|
|
283
|
+
removedData.set(componentType, dataArray[index] === MISSING_COMPONENT ? undefined : dataArray[index]);
|
|
287
284
|
}
|
|
288
|
-
|
|
289
|
-
|
|
285
|
+
const lastIndex = this.entities.length - 1;
|
|
286
|
+
if (index !== lastIndex) {
|
|
287
|
+
const lastEntity = this.entities[lastIndex];
|
|
288
|
+
this.entities[index] = lastEntity;
|
|
289
|
+
this.entityToIndex.set(lastEntity, index);
|
|
290
|
+
for (const componentType of this.componentTypes) {
|
|
291
|
+
const dataArray = this.getComponentData(componentType);
|
|
292
|
+
[dataArray[index], dataArray[lastIndex]] = [dataArray[lastIndex], dataArray[index]];
|
|
293
|
+
}
|
|
290
294
|
}
|
|
295
|
+
this.entities.pop();
|
|
296
|
+
this.entityToIndex.delete(entityId);
|
|
291
297
|
return removedData;
|
|
292
298
|
}
|
|
293
299
|
exists(entityId) {
|
|
@@ -439,10 +445,6 @@ class ComponentChangeset {
|
|
|
439
445
|
}
|
|
440
446
|
return finalComponents;
|
|
441
447
|
}
|
|
442
|
-
getFinalComponentTypes(existingComponents) {
|
|
443
|
-
const finalComponents = this.applyTo(existingComponents);
|
|
444
|
-
return Array.from(finalComponents.keys()).sort((a, b) => a - b);
|
|
445
|
-
}
|
|
446
448
|
}
|
|
447
449
|
|
|
448
450
|
// src/command-buffer.ts
|
|
@@ -995,11 +997,10 @@ class World {
|
|
|
995
997
|
}
|
|
996
998
|
}
|
|
997
999
|
const finalComponents = changeset.applyTo(currentComponents);
|
|
998
|
-
const
|
|
999
|
-
const
|
|
1000
|
-
const needsArchetypeChange = finalComponentTypes.length !== currentComponentTypes.length || !finalComponentTypes.every((type, index) => type === currentComponentTypes[index]);
|
|
1000
|
+
const currentComponentTypes = currentArchetype.componentTypes;
|
|
1001
|
+
const needsArchetypeChange = finalComponents.size !== currentComponentTypes.length || !currentComponentTypes.every((type) => finalComponents.has(type));
|
|
1001
1002
|
if (needsArchetypeChange) {
|
|
1002
|
-
const newArchetype = this.ensureArchetype(
|
|
1003
|
+
const newArchetype = this.ensureArchetype(finalComponents.keys().toArray());
|
|
1003
1004
|
currentArchetype.removeEntity(entityId);
|
|
1004
1005
|
newArchetype.addEntity(entityId, finalComponents);
|
|
1005
1006
|
this.entityToArchetype.set(entityId, newArchetype);
|
|
@@ -1030,7 +1031,7 @@ class World {
|
|
|
1030
1031
|
return changeset;
|
|
1031
1032
|
}
|
|
1032
1033
|
ensureArchetype(componentTypes) {
|
|
1033
|
-
const sortedTypes =
|
|
1034
|
+
const sortedTypes = componentTypes.toSorted((a, b) => a - b);
|
|
1034
1035
|
const hashKey = this.createArchetypeSignature(sortedTypes);
|
|
1035
1036
|
return getOrCreateWithSideEffect(this.archetypeBySignature, hashKey, () => {
|
|
1036
1037
|
const newArchetype = new Archetype(sortedTypes);
|