diff --git a/app/react/components/particle-worker.js b/app/react/components/particle-worker.js index b7e7d33..2091e1f 100644 --- a/app/react/components/particle-worker.js +++ b/app/react/components/particle-worker.js @@ -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);