perf: damage clumping

This commit is contained in:
cha0s 2024-07-30 17:06:04 -05:00
parent 2becc0afb9
commit 4b46b2f0ab
2 changed files with 26 additions and 7 deletions

View File

@ -31,7 +31,11 @@ export default class Vulnerable extends Component {
}
}
}
Component.markChange(this.entity, 'damage', {[this.id++]: specification});
Component.markChange(
this.entity,
'damage',
{[`${this.entity}-${this.id++}`]: specification},
);
}
};
}

View File

@ -16,6 +16,7 @@ export default function Entities({
const [ecs] = useEcs();
const [entities, setEntities] = useState({});
const [damages, setDamages] = useState({});
const [pendingDamage] = useState({accumulated: [], handle: undefined});
usePacket('EcsChange', async () => {
setEntities({});
}, [setEntities]);
@ -93,17 +94,31 @@ export default function Entities({
const {damage} = update.Vulnerable || {};
if (damage) {
for (const key in damage) {
const composite = [id, key].join('-');
damage[key].onClose = () => {
setDamages((damages) => {
const {[composite]: _, ...rest} = damages; // eslint-disable-line no-unused-vars
const {[key]: _, ...rest} = damages; // eslint-disable-line no-unused-vars
return rest;
})
};
setDamages((damages) => ({
...damages,
[composite]: damage[key],
}));
}
pendingDamage.accumulated.push(damage);
if (!pendingDamage.handle) {
pendingDamage.handle = setTimeout(() => {
const update = {};
for (const damage of pendingDamage.accumulated) {
for (const key in damage) {
update[key] = damage[key];
}
}
pendingDamage.accumulated.length = 0;
setDamages((damages) => {
return {
...damages,
...update,
};
});
pendingDamage.handle = undefined;
}, 50);
}
}
}