perf: entity updates

This commit is contained in:
cha0s 2024-08-05 02:23:41 -05:00
parent 51bdda5eb9
commit 14add7e8bb
2 changed files with 28 additions and 21 deletions

View File

@ -88,22 +88,26 @@ export default class Ecs {
if (componentsToRemove.length > 0) { if (componentsToRemove.length > 0) {
removing.push([entityId, componentsToRemove]); removing.push([entityId, componentsToRemove]);
} }
if (this.$$entities[entityId]) { if (this.$$entities[entityIdString]) {
const entity = this.$$entities[entityId]; const entity = this.$$entities[entityIdString];
let isInserting = false;
const entityInserts = {}; const entityInserts = {};
let isUpdating = false;
const entityUpdates = {}; const entityUpdates = {};
for (const componentName in componentsToUpdate) { for (const componentName in componentsToUpdate) {
if (entity[componentName]) { if (entity[componentName]) {
entityUpdates[componentName] = componentsToUpdate[componentName]; entityUpdates[componentName] = componentsToUpdate[componentName];
isUpdating = true;
} }
else { else {
entityInserts[componentName] = componentsToUpdate[componentName]; entityInserts[componentName] = componentsToUpdate[componentName];
isInserting = true;
} }
} }
if (Object.keys(entityInserts).length > 0) { if (isInserting) {
inserting.push([entityId, entityInserts]); inserting.push([entityId, entityInserts]);
} }
if (Object.keys(entityUpdates).length > 0) { if (isUpdating) {
updating.push([entityId, entityUpdates]); updating.push([entityId, entityUpdates]);
} }
} }
@ -111,9 +115,6 @@ export default class Ecs {
creating.push([entityId, componentsToUpdate]); creating.push([entityId, componentsToUpdate]);
} }
} }
if (destroying.size > 0) {
this.destroyMany(destroying);
}
const promises = []; const promises = [];
if (inserting.length > 0) { if (inserting.length > 0) {
promises.push(this.insertMany(inserting)); promises.push(this.insertMany(inserting));
@ -125,6 +126,9 @@ export default class Ecs {
promises.push(this.createManySpecific(creating)); promises.push(this.createManySpecific(creating));
} }
await Promise.all(promises); await Promise.all(promises);
if (destroying.size > 0) {
this.destroyMany(destroying);
}
if (removing.length > 0) { if (removing.length > 0) {
this.removeMany(removing); this.removeMany(removing);
} }

View File

@ -29,33 +29,36 @@ export default function Entities({monopolizers, particleWorker}) {
interactionFilters[0].brightness = (pulse * 0.75) + 1; interactionFilters[0].brightness = (pulse * 0.75) + 1;
interactionFilters[1].outerStrength = pulse * 0.5; interactionFilters[1].outerStrength = pulse * 0.5;
const updateEntities = useCallback((payload) => { const updateEntities = useCallback((payload) => {
if (0 === Object.keys(entities.current).length) {
for (let i = 0; i < 1000; ++i) {
pool.current.push(new Entity());
}
}
for (const id in payload) { for (const id in payload) {
if ('1' === id) {
continue;
}
if (!payload[id]) { if (!payload[id]) {
entities.current[id].removeFromContainer(); entities.current[id].removeFromContainer();
pool.current.push(entities.current[id]); pool.current.push(entities.current[id]);
delete entities.current[id]; delete entities.current[id];
continue; continue;
} }
const entity = ecs.get(id);
if (!entity.Position) {
continue;
}
if (!entities.current[id]) { if (!entities.current[id]) {
entities.current[id] = pool.current.length > 0 ? pool.current.pop() : new Entity(); entities.current[id] = pool.current.length > 0
entities.current[id].reset(entity, debug); ? pool.current.pop()
} : new Entity();
if (mainEntity === id) { entities.current[id].reset(ecs.get(id), debug);
entities.current[id].setMainEntity(); if (mainEntity === id) {
entities.current[id].setMainEntity();
}
} }
entities.current[id].update(payload[id]); entities.current[id].update(payload[id]);
entities.current[id].addToContainer(containerRef.current); entities.current[id].addToContainer(containerRef.current);
} }
}, [debug, ecs, mainEntity]) }, [debug, ecs, mainEntity])
useEffect(() => {
if (0 === pool.current.length) {
for (let i = 0; i < 1000; ++i) {
pool.current.push(new Entity());
}
}
}, []);
useEffect(() => { useEffect(() => {
for (const key in entities.current) { for (const key in entities.current) {
entities.current[key].setDebug(debug); entities.current[key].setDebug(debug);