fix: revert indexing opts

This commit is contained in:
cha0s 2024-08-07 14:27:55 -05:00
parent 0898dabb62
commit 2dec51008d

View File

@ -23,8 +23,6 @@ export default class Ecs {
deferredChanges = {}
$$deindexing = new Set();
$$destructionDependencies = new Map();
$$detached = new Set();
@ -35,8 +33,6 @@ export default class Ecs {
$$entityFactory = new EntityFactory();
$$reindexing = new Set();
Systems = {};
constructor({Systems, Components} = {}) {
@ -145,9 +141,9 @@ export default class Ecs {
attach(entityIds) {
for (const entityId of entityIds) {
this.$$detached.delete(entityId);
this.$$reindexing.add(entityId);
this.applyDeferredChanges(entityId);
}
this.reindex(entityIds);
}
changed(criteria) {
@ -242,13 +238,13 @@ export default class Ecs {
await Promise.all(promises);
for (let i = 0; i < specificsList.length; i++) {
const [entityId, components] = specificsList[i];
this.$$reindexing.add(entityId);
this.rebuild(entityId, () => Object.keys(components));
if (this.$$detached.has(entityId)) {
continue;
}
this.applyDeferredChanges(entityId);
}
this.reindex(entityIds);
return entityIds;
}
@ -315,9 +311,8 @@ export default class Ecs {
destroyMany(entityIds) {
const destroying = {};
// this.deindex(entityIds);
this.deindex(entityIds);
for (const entityId of entityIds) {
this.$$deindexing.add(entityId);
if (!this.$$entities[entityId]) {
throw new Error(`can't destroy non-existent entity ${entityId}`);
}
@ -333,15 +328,16 @@ export default class Ecs {
}
for (const entityId of entityIds) {
delete this.$$entities[entityId];
delete this.deferredChanges[entityId];
this.diff[entityId] = false;
}
}
detach(entityIds) {
for (const entityId of entityIds) {
this.$$deindexing.add(entityId);
this.$$detached.add(entityId);
}
this.deindex(entityIds);
}
get entities() {
@ -381,9 +377,9 @@ export default class Ecs {
}
await Promise.all(promises);
for (const [entityId, components] of entities) {
this.$$reindexing.add(entityId);
this.rebuild(entityId, (componentNames) => [...new Set(componentNames.concat(Object.keys(components)))]);
}
this.reindex(unique);
}
markChange(entityId, components) {
@ -510,9 +506,9 @@ export default class Ecs {
this.Components[componentName].destroyMany(removing[componentName]);
}
for (const [entityId, components] of entities) {
this.$$reindexing.add(entityId);
this.rebuild(entityId, (componentNames) => componentNames.filter((type) => !components.includes(type)));
}
this.reindex(unique);
}
static serialize(ecs, view) {
@ -563,15 +559,6 @@ export default class Ecs {
this.$$destructionDependencies.delete(entityId);
}
}
// update indices
if (this.$$deindexing.size > 0) {
this.deindex(this.$$deindexing);
this.$$deindexing.clear();
}
if (this.$$reindexing.size > 0) {
this.reindex(this.$$reindexing);
this.$$reindexing.clear();
}
}
toJSON() {
@ -597,14 +584,12 @@ export default class Ecs {
const updating = {};
const unique = new Set();
for (const [entityId, components] of entities) {
this.rebuild(entityId);
for (const componentName in components) {
if (!updating[componentName]) {
updating[componentName] = [];
}
updating[componentName].push([entityId, components[componentName]]);
}
this.$$reindexing.add(entityId);
unique.add(entityId);
}
const promises = [];
@ -612,6 +597,7 @@ export default class Ecs {
promises.push(this.Components[componentName].updateMany(updating[componentName]));
}
await Promise.all(promises);
this.reindex(unique);
}
}