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