animate
This commit is contained in:
24
engine.js
24
engine.js
@@ -97,6 +97,9 @@ export class Engine {
|
||||
this.worldMeshes = new Map();
|
||||
this.renderRequested = false;
|
||||
|
||||
this.observedWorlds = new Set();
|
||||
this.updateRequested = false;
|
||||
|
||||
window.addEventListener('resize', () => {
|
||||
this.resize();
|
||||
this.requestRender();
|
||||
@@ -105,6 +108,19 @@ export class Engine {
|
||||
this.resize();
|
||||
}
|
||||
|
||||
requestUpdate() {
|
||||
if (!this.updateRequested) {
|
||||
this.updateRequested = true;
|
||||
|
||||
// Promise.resolve().then() executes immediately after the current synchronous
|
||||
// call stack finishes, ensuring 10 simultaneous setBlock calls only trigger 1 update.
|
||||
Promise.resolve().then(() => {
|
||||
this.updateRequested = false;
|
||||
this.updateAll();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
requestRender() {
|
||||
if (!this.renderRequested) {
|
||||
this.renderRequested = true;
|
||||
@@ -122,6 +138,14 @@ export class Engine {
|
||||
|
||||
addDiorama(diorama) {
|
||||
this.dioramas.push(diorama);
|
||||
|
||||
// Subscribe to the world if we aren't already watching it
|
||||
if (!this.observedWorlds.has(diorama.world)) {
|
||||
this.observedWorlds.add(diorama.world);
|
||||
|
||||
// Whenever the world changes, queue a single, debounced geometry rebuild
|
||||
diorama.world.subscribe(() => this.requestUpdate());
|
||||
}
|
||||
}
|
||||
|
||||
async updateAll() {
|
||||
|
||||
Reference in New Issue
Block a user