fix: memory
This commit is contained in:
parent
38d76791f7
commit
f5d092efaf
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user