fix: synchronized updates

This commit is contained in:
cha0s 2024-08-01 13:23:10 -05:00
parent 0e5fcc3ea3
commit 5d352bb367
2 changed files with 10 additions and 4 deletions

View File

@ -15,6 +15,7 @@ export default function Entities({monopolizers, particleWorker}) {
const [debug] = useDebug();
const [ecs] = useEcs();
const containerRef = useRef();
const latestTick = useRef();
const entities = useRef({});
const pool = useRef([]);
const [mainEntity] = useMainEntity();
@ -75,8 +76,10 @@ export default function Entities({monopolizers, particleWorker}) {
return;
}
async function onMessage(diff) {
latestTick.current = Promise.resolve(latestTick.current).then(async () => {
await ecs.apply(diff.data);
updateEntities(diff.data);
});
}
particleWorker.addEventListener('message', onMessage);
return () => {

View File

@ -35,6 +35,7 @@ function Ui({disconnected}) {
// Key input.
const client = useClient();
const chatInputRef = useRef();
const latestTick = useRef();
const gameRef = useRef();
const [mainEntity, setMainEntity] = useMainEntity();
const [debug, setDebug] = useDebug();
@ -320,8 +321,10 @@ function Ui({disconnected}) {
if (0 === Object.keys(payload.ecs).length) {
return;
}
latestTick.current = Promise.resolve(latestTick.current).then(async () => {
await ecs.apply(payload.ecs);
client.emitter.invoke(':Ecs', payload.ecs);
});
}, [ecs]);
usePacket('Tick', onTickPacket);
const onEcsTick = useCallback((payload, ecs) => {