fix: memory

This commit is contained in:
cha0s 2024-08-01 14:31:54 -05:00
parent 38d76791f7
commit f5d092efaf

View File

@ -17,19 +17,41 @@ addEventListener('message', (particle) => {
.onEnd(() => {});
});
const memory = new Set();
let last = performance.now();
function tick(now) {
const elapsed = (now - last) / 1000;
last = now;
if (ecs.get(1)) {
ecs.tick(elapsed);
emitter.tick(elapsed);
if ('1' in ecs.diff) {
delete ecs.diff['1'];
}
postMessage(ecs.diff);
ecs.setClean();
}
requestAnimationFrame(tick);
if (!ecs.get(1)) {
return;
}
ecs.tick(elapsed);
emitter.tick(elapsed);
const update = {};
const lastNearby = new Set(memory.values());
for (const id in ecs.$$entities) {
const entity = ecs.$$entities[id];
lastNearby.delete(id);
if (!memory.has(id)) {
update[id] = entity.toJSON();
}
else if (ecs.diff[id]) {
update[id] = ecs.diff[id];
}
memory.add(id);
}
for (const id of lastNearby) {
memory.delete(id);
update[id] = false;
}
if ('1' in update) {
delete update['1'];
}
if (Object.keys(update).length > 0) {
postMessage(update);
}
ecs.setClean();
}
requestAnimationFrame(tick);