refactor: entity updates

This commit is contained in:
cha0s 2024-07-13 16:33:23 -05:00
parent 70edcc04de
commit a0f6ca9056
2 changed files with 40 additions and 19 deletions

View File

@ -12,22 +12,22 @@ export default function Entities({camera, scale}) {
if (!ecs) {
return;
}
const updatedEntities = {...entities};
const deleting = {};
const updating = {};
for (const id in payload) {
if ('1' === id) {
continue;
}
const update = payload[id];
if (false === update) {
delete updatedEntities[id];
deleting[id] = true;
continue;
}
updatedEntities[id] = ecs.get(id);
updating[id] = ecs.get(id);
const {dialogue} = update.Interlocutor || {};
if (dialogue) {
const {dialogues} = updatedEntities[id].Interlocutor;
const {dialogues} = updating[id].Interlocutor;
for (const key in dialogue) {
updatedEntities[id] = ecs.rebuild(id);
dialogues[key] = dialogue[key];
dialogues[key].letters = parseLetters(dialogues[key].body);
dialogues[key].onClose = () => {
@ -40,8 +40,16 @@ export default function Entities({camera, scale}) {
}
}
}
setEntities(updatedEntities);
}, [ecs, entities]);
setEntities((entities) => {
for (const id in deleting) {
delete entities[id];
}
return {
...entities,
...updating,
};
});
}, [ecs]);
const renderables = [];
for (const id in entities) {
renderables.push(

View File

@ -26,31 +26,44 @@ export default function Entities({filters}) {
if (!ecs) {
return;
}
const updatedEntities = {...entities};
const deleting = {};
const updating = {};
for (const id in payload) {
if ('1' === id) {
continue;
}
const update = payload[id];
if (false === update) {
delete updatedEntities[id];
deleting[id] = true;
continue;
}
else {
updatedEntities[id] = ecs.get(id);
if (update.Emitter?.emit) {
updatedEntities[id].Emitter.emitting = {
...updatedEntities[id].Emitter.emitting,
...update.Emitter.emit,
};
}
updating[id] = ecs.get(id);
if (update.Emitter?.emit) {
updating[id].Emitter.emitting = {
...updating[id].Emitter.emitting,
...update.Emitter.emit,
};
}
}
setEntities(updatedEntities);
setEntities((entities) => {
for (const id in deleting) {
delete entities[id];
}
return {
...entities,
...updating,
};
});
}, [ecs]);
useEcsTick(() => {
if (!ecs) {
return;
}
const main = ecs.get(mainEntity);
if (main) {
setWillInteractWith(main.Interacts.willInteractWith);
}
}, [ecs, entities, mainEntity]);
}, [ecs, mainEntity]);
useEffect(() => {
setRadians(0);
const handle = setInterval(() => {