perf: damage clumping
This commit is contained in:
parent
2becc0afb9
commit
4b46b2f0ab
|
@ -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},
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user