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

View File

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