split 'world', 'frame'.

This commit is contained in:
2026-07-05 20:55:54 -04:00
parent 5b99030093
commit 6f8ca350ca
4 changed files with 162 additions and 195 deletions

View File

@@ -1,17 +1,17 @@
import {mat4Ortho, mat4LookAt, mat4Multiply} from './math.js'; import {mat4Ortho, mat4LookAt, mat4Multiply} from './math.js';
export class Diorama { export class Diorama {
constructor(elementId, world, requestRenderCallback) { constructor(elementId, frame, requestRenderCallback) {
this.element = document.getElementById(elementId); this.element = document.getElementById(elementId);
this.canvas = document.createElement('canvas') this.canvas = document.createElement('canvas')
this.element.appendChild(this.canvas) this.element.appendChild(this.canvas)
this.ctx2d = this.canvas.getContext('2d'); this.ctx2d = this.canvas.getContext('2d');
this.world = world; this.frame = frame;
this.requestRender = requestRenderCallback; this.requestRender = requestRenderCallback;
this.target = [0.5, 0, 0.5]; this.target = [0.5, 0, 0.5];
this.radius = 4; this.radius = 4;
this.theta = 45; this.theta = 135;
this.phi = 30; this.phi = 30;
this.projMatrix = new Float32Array(16); this.projMatrix = new Float32Array(16);
@@ -73,7 +73,7 @@ export class Diorama {
} }
centerView() { centerView() {
if (this.world.blocks.size === 0) { if (this.frame.blocks.size === 0) {
this.target = [0.5, 0.5, 0.5]; this.target = [0.5, 0.5, 0.5];
return; return;
} }
@@ -81,7 +81,7 @@ export class Diorama {
let minX = Infinity, minY = Infinity, minZ = Infinity; let minX = Infinity, minY = Infinity, minZ = Infinity;
let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity; let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
for (const block of this.world.blocks.values()) { for (const block of this.frame.blocks.values()) {
minX = Math.min(minX, block.pos[0]); minX = Math.min(minX, block.pos[0]);
minY = Math.min(minY, block.pos[1]); minY = Math.min(minY, block.pos[1]);
minZ = Math.min(minZ, block.pos[2]); minZ = Math.min(minZ, block.pos[2]);

View File

@@ -104,9 +104,9 @@ export class Engine {
this.cache.register('textures', this.atlas); this.cache.register('textures', this.atlas);
this.dioramas = []; this.dioramas = [];
this.worldMeshes = new Map(); this.frameMeshes = new Map();
this.renderRequested = false; this.renderRequested = false;
this.observedWorlds = new Set(); this.observedFrames = new Set();
this.updateRequested = false; this.updateRequested = false;
window.addEventListener('resize', () => this.requestRender()); window.addEventListener('resize', () => this.requestRender());
@@ -137,43 +137,43 @@ export class Engine {
addDiorama(diorama) { addDiorama(diorama) {
this.dioramas.push(diorama); this.dioramas.push(diorama);
if (!this.observedWorlds.has(diorama.world)) { if (!this.observedFrames.has(diorama.frame)) {
this.observedWorlds.add(diorama.world); this.observedFrames.add(diorama.frame);
diorama.world.subscribe(() => this.requestUpdate()); diorama.frame.subscribe(() => this.requestUpdate());
} }
} }
async updateAll() { async updateAll() {
const uniqueWorlds = new Set(this.dioramas.map(d => d.world)); const uniqueFrames = new Set(this.dioramas.map(d => d.frame));
if (uniqueWorlds.size === 0) return; if (uniqueFrames.size === 0) return;
const uniqueBlockIds = new Set(); const uniqueBlockIds = new Set();
for (const world of uniqueWorlds) { for (const frame of uniqueFrames) {
for (const block of world.blocks.values()) uniqueBlockIds.add(block.id); for (const block of frame.blocks.values()) uniqueBlockIds.add(block.id);
} }
await Promise.all(Array.from(uniqueBlockIds).map(id => this.cache.get(id, 'blockstates'))); await Promise.all(Array.from(uniqueBlockIds).map(id => this.cache.get(id, 'blockstates')));
const uniqueModelIds = new Set(); const uniqueModelIds = new Set();
const parsedWorlds = new Map(); const parsedFrames = new Map();
for (const world of uniqueWorlds) { for (const frame of uniqueFrames) {
const parsedBlocks = []; const parsedBlocks = [];
for (const block of world.blocks.values()) { for (const block of frame.blocks.values()) {
const state = await this.cache.get(block.id, 'blockstates'); const state = await this.cache.get(block.id, 'blockstates');
const parts = state.resolveParts(block.state); const parts = state.resolveParts(block.state);
parsedBlocks.push({block, parts}); parsedBlocks.push({block, parts});
for (const p of parts) uniqueModelIds.add(p.model); for (const p of parts) uniqueModelIds.add(p.model);
} }
parsedWorlds.set(world, parsedBlocks); parsedFrames.set(frame, parsedBlocks);
} }
await Promise.all(Array.from(uniqueModelIds).map(id => this.cache.get(id, 'models'))); await Promise.all(Array.from(uniqueModelIds).map(id => this.cache.get(id, 'models')));
const textureTasks = []; const textureTasks = [];
const worldPools = new Map(); const framePools = new Map();
for (const world of uniqueWorlds) { for (const frame of uniqueFrames) {
const instancePool = new Map(); const instancePool = new Map();
for (const {block, parts} of parsedWorlds.get(world)) { for (const {block, parts} of parsedFrames.get(frame)) {
for (const part of parts) { for (const part of parts) {
const hash = Blockstate.getVariantHash(part); const hash = Blockstate.getVariantHash(part);
if (!instancePool.has(hash)) { if (!instancePool.has(hash)) {
@@ -205,15 +205,15 @@ export class Engine {
} }
} }
} }
worldPools.set(world, instancePool); framePools.set(frame, instancePool);
} }
await Promise.all(textureTasks); await Promise.all(textureTasks);
this.updateAtlasTexture(); this.updateAtlasTexture();
for (const world of uniqueWorlds) { for (const frame of uniqueFrames) {
const newMeshes = []; const newMeshes = [];
for (const pool of worldPools.get(world).values()) { for (const pool of framePools.get(frame).values()) {
const blockModel = await this.cache.get(pool.partDef.model, 'models'); const blockModel = await this.cache.get(pool.partDef.model, 'models');
const geometry = blockModel.buildGeometry(this.atlas.uvmap, pool.partDef); const geometry = blockModel.buildGeometry(this.atlas.uvmap, pool.partDef);
@@ -223,7 +223,7 @@ export class Engine {
newMeshes.push(this.createInstancedMesh(geometry, instanceCount, matrixArray, colorArray)); newMeshes.push(this.createInstancedMesh(geometry, instanceCount, matrixArray, colorArray));
} }
this.worldMeshes.set(world, newMeshes); this.frameMeshes.set(frame, newMeshes);
} }
this.requestRender(); this.requestRender();
@@ -317,11 +317,11 @@ export class Engine {
const viewProj = diorama.updateMatrices(aspect); const viewProj = diorama.updateMatrices(aspect);
gl.uniformMatrix4fv(this.uniforms.viewProj, false, viewProj); gl.uniformMatrix4fv(this.uniforms.viewProj, false, viewProj);
gl.uniform3f(this.uniforms.lightDir, 1.0, 3.0, -2.0); gl.uniform3f(this.uniforms.lightDir, 1.0, 3.0, 2.0);
gl.uniform3f(this.uniforms.viewDir, diorama.viewDir[0], diorama.viewDir[1], diorama.viewDir[2]); gl.uniform3f(this.uniforms.viewDir, diorama.viewDir[0], diorama.viewDir[1], diorama.viewDir[2]);
gl.uniform3f(this.uniforms.upDir, diorama.upDir[0], diorama.upDir[1], diorama.upDir[2]); gl.uniform3f(this.uniforms.upDir, diorama.upDir[0], diorama.upDir[1], diorama.upDir[2]);
const meshes = this.worldMeshes.get(diorama.world) || []; const meshes = this.frameMeshes.get(diorama.frame) || [];
for (const mesh of meshes) { for (const mesh of meshes) {
gl.bindVertexArray(mesh.vao); gl.bindVertexArray(mesh.vao);
gl.drawElementsInstanced(gl.TRIANGLES, mesh.indexCount, gl.UNSIGNED_SHORT, 0, mesh.instanceCount); gl.drawElementsInstanced(gl.TRIANGLES, mesh.indexCount, gl.UNSIGNED_SHORT, 0, mesh.instanceCount);

View File

@@ -52,77 +52,76 @@
<script type="module"> <script type="module">
import {Engine} from './engine.js'; import {Engine} from './engine.js';
import {Diorama} from './diorama.js'; import {Diorama} from './diorama.js';
import {World} from './world.js'; import {World, WorldFrame} from './world.js';
async function initDocument() { async function initDocument() {
try { try {
const engine = new Engine(); const engine = new Engine();
document.getElementById('atlas-container')?.appendChild(engine.atlas.canvas); document.getElementById('atlas-container')?.appendChild(engine.atlas.canvas);
const world1 = new World(` const w_static = new World(`
p -1 0 0 redstone_wire east=side west=none north=none south=none power=15 p -1 0 0 redstone_wire east=side west=none north=none south=none power=15
p 0 0 0 repeater facing=east delay=1 locked=false powered=true p 0 0 0 repeater facing=east delay=1 locked=false powered=true
p 2 0 0 oak_trapdoor facing=east half=bottom open=true p 2 0 0 oak_trapdoor facing=east half=bottom open=true
p 0 -1 0 lodestone p 0 -1 0 lodestone
p 0 -2 0 piston facing=north extended=false p 0 -2 0 piston facing=north extended=false
`) `)
const f_static = new WorldFrame(w_static);
const d_static_top = new Diorama('demo-2', f_static, () => engine.requestRender());
d_static_top.radius = 5;
d_static_top.theta = 180;
d_static_top.phi = 90;
d_static_top.centerView();
d_static_top.saveState();
engine.addDiorama(d_static_top)
const d_static_iso = new Diorama('demo-3', f_static, () => engine.requestRender());
d_static_iso.radius = 5;
d_static_iso.centerView();
d_static_iso.saveState();
engine.addDiorama(d_static_iso);
const pulses = ` const w_pulses = new World(`
p 0 0 0 observer facing=north powered=false p 0 0 0 observer facing=south powered=false
p 0 0 1 piston facing=south extended=false p 0 0 1 observer facing=east powered=false
p 1 0 1 observer facing=north powered=false
p 0 -1 1 observer facing=up powered=false p 1 0 0 observer facing=west powered=false
p 0 -1 2 observer facing=up powered=false t 0
p 0 0 1 powered=false
t 2
p 0 0 0 powered=true p 0 0 0 powered=true
p 0 0 1 extended=true t 2
t 3
p 0 0 1.5 piston_head facing=south short=true type=normal
t 4
p 0 0 0 powered=false p 0 0 0 powered=false
p 1 0 0 powered=true
p 0 -1 1 powered=true t 4
p 1 0 0 powered=false
p 0 -1 2 powered=true p 1 0 1 powered=true
t 6 t 6
p 0 -1 1 powered=false p 1 0 1 powered=false
p 0 -1 2 powered=false p 0 0 1 powered=true
`; `);
const f_pulses = new WorldFrame(w_pulses);
const d_pulses = new Diorama('demo-pulses', f_pulses, () => engine.requestRender());
d_pulses.radius = 4;
d_pulses.centerView()
d_pulses.saveState()
engine.addDiorama(d_pulses);
const world_pulses = new World(pulses); const w_piston = new World(`
const demo_pulses = new Diorama('demo-pulses', world_pulses, () => engine.requestRender())
demo_pulses.radius = 4;
demo_pulses.centerView()
demo_pulses.saveState()
engine.addDiorama(demo_pulses);
const piston = `
p 0 0 0 redstone_wire power=0 east=side west=none north=none south=none p 0 0 0 redstone_wire power=0 east=side west=none north=none south=none
p 1 0 0 repeater facing=west powered=false locked=false delay=2 p 1 0 0 repeater facing=west powered=false locked=false delay=2
p 2 0 0 piston facing=east extended=false p 2 0 0 piston facing=east extended=false
p 3 0 0 slime_block p 3 0 0 slime_block
p 4 0 0 slime_block p 4 0 0 slime_block
t 2 t 2
p 0 0 0 power=15 p 0 0 0 power=15
t 6 t 6
p 1 0 0 powered=true p 1 0 0 powered=true
t 7 t 7
p 2 0 0 extended=true p 2 0 0 extended=true
p 4.0 0 0 air & p 4.0 0 0 air &
p 4.5 0 0 slime_block p 4.5 0 0 slime_block
p 3.0 0 0 air & p 3.0 0 0 air &
p 3.5 0 0 slime_block p 3.5 0 0 slime_block
p 2.5 0 0 piston_head facing=east short=true type=normal p 2.5 0 0 piston_head facing=east short=true type=normal
t 8 t 8
p 4.5 0 0 air & p 4.5 0 0 air &
p 5.0 0 0 slime_block p 5.0 0 0 slime_block
@@ -130,13 +129,10 @@
p 4.0 0 0 slime_block p 4.0 0 0 slime_block
p 2.5 0 0 air & p 2.5 0 0 air &
p 3.0 0 0 piston_head facing=east short=false type=normal p 3.0 0 0 piston_head facing=east short=false type=normal
t 12 t 12
p 0 0 0 power=0 p 0 0 0 power=0
t 16 t 16
p 1 0 0 powered=false p 1 0 0 powered=false
t 17 t 17
p 3 0 0 air & p 3 0 0 air &
p 2.5 0 0 piston_head facing=east short=true type=normal p 2.5 0 0 piston_head facing=east short=true type=normal
@@ -144,7 +140,6 @@
p 3.5 0 0 slime_block p 3.5 0 0 slime_block
p 5.0 0 0 air & p 5.0 0 0 air &
p 4.5 0 0 slime_block p 4.5 0 0 slime_block
t 18 t 18
p 2.5 0 0 air p 2.5 0 0 air
p 3.5 0 0 air & p 3.5 0 0 air &
@@ -152,36 +147,19 @@
p 4.5 0 0 air & p 4.5 0 0 air &
p 4.0 0 0 slime_block p 4.0 0 0 slime_block
p 2 0 0 extended=false p 2 0 0 extended=false
`; `);
const f_piston_t = new WorldFrame(w_piston);
const piston_time = new World(piston); const d_piston_t = new Diorama('demo-time', f_piston_t, () => engine.requestRender());
const piston_state = new World(piston); d_piston_t.radius = 4;
d_piston_t.centerView();
const demo_time = new Diorama('demo-time', piston_time, () => engine.requestRender()) d_piston_t.saveState();
demo_time.radius = 4; engine.addDiorama(d_piston_t);
demo_time.centerView() const f_piston_s = new WorldFrame(w_piston);
demo_time.saveState() const d_piston_s = new Diorama('demo-state', f_piston_s, () => engine.requestRender());
engine.addDiorama(demo_time); d_piston_s.radius = 4;
d_piston_s.centerView();
const demo_state = new Diorama('demo-state', piston_state, () => engine.requestRender()) d_piston_s.saveState();
demo_state.radius = 4; engine.addDiorama(d_piston_s);
demo_state.centerView()
demo_state.saveState()
engine.addDiorama(demo_state);
const demo2 = new Diorama('demo-2', world1, () => engine.requestRender())
demo2.radius = 5;
demo2.theta = 180;
demo2.phi = 90;
demo2.centerView()
demo2.saveState()
engine.addDiorama(demo2);
const demo3 = new Diorama('demo-3', world1, () => engine.requestRender())
demo3.radius = 5;
demo3.centerView()
demo3.saveState()
engine.addDiorama(demo3);
// Fetch and build everything concurrently! // Fetch and build everything concurrently!
await engine.updateAll(); await engine.updateAll();
@@ -189,38 +167,28 @@
const factor = 2.5; const factor = 2.5;
const update_time = () => { const update_time = () => {
for (let i = 0; i < 20; i++) { for (let i = 0; i < 20; i++) {
setTimeout(() => piston_time.seek(i), i * factor * 50) setTimeout(() => f_piston_t.seek(i), i * factor * 50)
} }
}; };
setInterval(update_time, factor * 1000) setInterval(update_time, factor * 1000)
update_time(); update_time();
let i = 0; let i = 0;
let n = piston_state.eventCount; let n = w_piston.eventCount;
const update_state = () => { const update_state = () => {
piston_state.seekIndex(i); f_piston_s.seekIndex(i);
i = (i + 1) % n; i = (i + 1) % n;
}; };
setInterval(update_state, 250); setInterval(update_state, 250);
update_state() update_state()
const update_pulses = () => { const update_pulses = () => {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 8; i++) {
setTimeout(() => world_pulses.seek(i), i * factor * 50) setTimeout(() => f_pulses.seek(i), i * factor * 50)
} }
}; };
setInterval(update_pulses, factor * 500) setInterval(update_pulses, factor * 8 * 50)
update_pulses(); update_pulses();
// let j = 0;
// let m = world_pulses.eventCount;
// const update_pulses = () => {
// world_pulses.seekIndex(j);
// j = (j + 1) % m;
// };
// setInterval(update_pulses, 250);
// update_state()
} catch (err) { } catch (err) {
console.error("Renderer Initialization Failed:", err); console.error("Renderer Initialization Failed:", err);
} }

159
world.js
View File

@@ -1,3 +1,5 @@
// world.js
export class Block { export class Block {
constructor(id, pos, state) { constructor(id, pos, state) {
this.id = id; this.id = id;
@@ -8,19 +10,11 @@ export class Block {
export class World { export class World {
constructor(script = '') { constructor(script = '') {
this.blocks = new Map(); // Pure Data State
this.listeners = new Set();
// Timeline State
this.events = []; this.events = [];
this.labels = new Map(); this.labels = new Map();
this.currentIndex = 0;
this.currentTime = -Infinity;
this.initialState = new Map(); this.initialState = new Map();
this.initialIndex = 0; this.initialIndex = 0;
this.eventCount = 0; this.eventCount = 0;
if (script) { if (script) {
@@ -28,60 +22,6 @@ export class World {
} }
} }
subscribe(callback) {
this.listeners.add(callback);
return () => this.listeners.delete(callback);
}
notify() {
for (const listener of this.listeners) listener();
}
set(id, x, y, z, state = {}) {
const key = `${x},${y},${z}`;
const isAir = id === 'air' || id === 'minecraft:air';
if (isAir) {
if (this.blocks.delete(key)) {
this.notify();
}
} else {
this.blocks.set(key, new Block(id, [x, y, z], state));
this.notify();
}
}
get(x, y, z) {
return this.blocks.get(`${x},${y},${z}`);
}
#apply(data, x, y, z) {
const key = `${x},${y},${z}`;
if (data) {
this.blocks.set(key, new Block(data.id, [x, y, z], {...data.state}));
} else {
this.blocks.delete(key);
}
}
reset() {
this.blocks.clear();
for (const [key, block] of this.initialState.entries()) {
this.blocks.set(key, new Block(block.id, [...block.pos], {...block.state}));
}
this.currentIndex = this.initialIndex;
if (this.initialIndex > 0 && this.events.length > 0) {
this.currentTime = this.events[this.initialIndex - 1].time;
} else {
this.currentTime = -Infinity;
}
this.notify();
}
#compile(script) { #compile(script) {
let currentTime = -1; let currentTime = -1;
let capturedInitialState = false; let capturedInitialState = false;
@@ -104,7 +44,6 @@ export class World {
for (let line of lines) { for (let line of lines) {
let mergeNext = false; let mergeNext = false;
// Check for the continuation operator
if (line.endsWith('&')) { if (line.endsWith('&')) {
mergeNext = true; mergeNext = true;
line = line.slice(0, -1).trim(); line = line.slice(0, -1).trim();
@@ -118,7 +57,6 @@ export class World {
if (newTime >= 0 && !capturedInitialState) captureInitial(); if (newTime >= 0 && !capturedInitialState) captureInitial();
// Force flush any pending batch if time suddenly changes
if (currentBatch.length > 0) { if (currentBatch.length > 0) {
this.events.push({time: currentTime, actions: currentBatch}); this.events.push({time: currentTime, actions: currentBatch});
currentBatch = []; currentBatch = [];
@@ -173,7 +111,6 @@ export class World {
currentBatch.push({x, y, z, prev, next}); currentBatch.push({x, y, z, prev, next});
// Commit the batch to the timeline if it's not waiting for a continuation
if (!mergeNext) { if (!mergeNext) {
this.events.push({time: currentTime, actions: currentBatch}); this.events.push({time: currentTime, actions: currentBatch});
currentBatch = []; currentBatch = [];
@@ -188,63 +125,125 @@ export class World {
if (!capturedInitialState) captureInitial(); if (!capturedInitialState) captureInitial();
this.eventCount = this.events.length - this.initialIndex; this.eventCount = this.events.length - this.initialIndex;
}
}
export class WorldFrame {
constructor(world) {
this.world = world;
// Playback State
this.blocks = new Map();
this.listeners = new Set();
this.currentIndex = 0;
this.currentTime = -Infinity;
// Immediately sync frame to the bedrock state
this.reset(); this.reset();
} }
subscribe(callback) {
this.listeners.add(callback);
return () => this.listeners.delete(callback);
}
notify() {
for (const listener of this.listeners) listener();
}
get(x, y, z) {
return this.blocks.get(`${x},${y},${z}`);
}
#apply(data, x, y, z) {
const key = `${x},${y},${z}`;
if (data) {
this.blocks.set(key, new Block(data.id, [x, y, z], {...data.state}));
} else {
this.blocks.delete(key);
}
}
seek(target) { seek(target) {
let targetTime = target; let targetTime = target;
if (typeof target === 'string') { if (typeof target === 'string') {
targetTime = this.labels.get(target); targetTime = this.world.labels.get(target);
if (targetTime === undefined) throw new Error(`Label not found: ${target}`); if (targetTime === undefined) throw new Error(`Label not found: ${target}`);
} }
while (this.currentIndex < this.events.length && this.events[this.currentIndex].time <= targetTime) { let changed = false;
const batch = this.events[this.currentIndex];
while (this.currentIndex < this.world.events.length && this.world.events[this.currentIndex].time <= targetTime) {
const batch = this.world.events[this.currentIndex];
for (const action of batch.actions) { for (const action of batch.actions) {
this.#apply(action.next, action.x, action.y, action.z); this.#apply(action.next, action.x, action.y, action.z);
} }
this.currentIndex++; this.currentIndex++;
changed = true;
} }
while (this.currentIndex > 0 && this.events[this.currentIndex - 1].time > targetTime) { while (this.currentIndex > 0 && this.world.events[this.currentIndex - 1].time > targetTime) {
this.currentIndex--; this.currentIndex--;
const batch = this.events[this.currentIndex]; const batch = this.world.events[this.currentIndex];
for (let i = batch.actions.length - 1; i >= 0; i--) { for (let i = batch.actions.length - 1; i >= 0; i--) {
const action = batch.actions[i]; const action = batch.actions[i];
this.#apply(action.prev, action.x, action.y, action.z); this.#apply(action.prev, action.x, action.y, action.z);
} }
changed = true;
} }
this.currentTime = targetTime; this.currentTime = targetTime;
this.notify(); if (changed) this.notify();
} }
seekIndex(targetIndex) { seekIndex(targetIndex) {
targetIndex = targetIndex + this.initialIndex; targetIndex = targetIndex + this.world.initialIndex;
let changed = false;
while (this.currentIndex < this.events.length && this.currentIndex < targetIndex) { while (this.currentIndex < this.world.events.length && this.currentIndex < targetIndex) {
const batch = this.events[this.currentIndex]; const batch = this.world.events[this.currentIndex];
for (const action of batch.actions) { for (const action of batch.actions) {
this.#apply(action.next, action.x, action.y, action.z); this.#apply(action.next, action.x, action.y, action.z);
} }
this.currentIndex++; this.currentIndex++;
changed = true;
} }
while (this.currentIndex > 0 && this.currentIndex > targetIndex) { while (this.currentIndex > 0 && this.currentIndex > targetIndex) {
this.currentIndex--; this.currentIndex--;
const batch = this.events[this.currentIndex]; const batch = this.world.events[this.currentIndex];
for (let i = batch.actions.length - 1; i >= 0; i--) { for (let i = batch.actions.length - 1; i >= 0; i--) {
const action = batch.actions[i]; const action = batch.actions[i];
this.#apply(action.prev, action.x, action.y, action.z); this.#apply(action.prev, action.x, action.y, action.z);
} }
changed = true;
} }
if (this.currentIndex < this.events.length) { if (this.currentIndex < this.world.events.length) {
this.currentTime = this.events[this.currentIndex].time; this.currentTime = this.world.events[this.currentIndex].time;
} else if (this.events.length > 0) { } else if (this.world.events.length > 0) {
this.currentTime = this.events[this.events.length - 1].time; this.currentTime = this.world.events[this.world.events.length - 1].time;
} else {
this.currentTime = -Infinity;
}
if (changed) this.notify();
}
reset() {
this.blocks.clear();
for (const [key, block] of this.world.initialState.entries()) {
this.blocks.set(key, new Block(block.id, [...block.pos], {...block.state}));
}
this.currentIndex = this.world.initialIndex;
if (this.world.initialIndex > 0 && this.world.events.length > 0) {
this.currentTime = this.world.events[this.world.initialIndex - 1].time;
} else {
this.currentTime = -Infinity;
} }
this.notify(); this.notify();