This commit is contained in:
2026-07-05 18:46:47 -04:00
parent 1759e89b3b
commit 5b99030093
3 changed files with 188 additions and 81 deletions

124
world.js
View File

@@ -55,9 +55,35 @@ export class World {
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) {
let currentTime = -1;
let subTick = 0;
let capturedInitialState = false;
const shadowWorld = new Map();
@@ -73,18 +99,32 @@ export class World {
const lines = script.split('\n').map(l => l.trim()).filter(l => l && !l.startsWith('#'));
for (const line of lines) {
let currentBatch = [];
for (let line of lines) {
let mergeNext = false;
// Check for the continuation operator
if (line.endsWith('&')) {
mergeNext = true;
line = line.slice(0, -1).trim();
}
const tokens = line.split(/\s+/);
const cmd = tokens[0];
if (cmd === 't') {
const newTime = parseFloat(tokens[1]);
// If we are crossing the tick 0 threshold, lock in the initial state
if (newTime >= 0 && !capturedInitialState) captureInitial();
// Force flush any pending batch if time suddenly changes
if (currentBatch.length > 0) {
this.events.push({time: currentTime, actions: currentBatch});
currentBatch = [];
}
currentTime = newTime;
subTick = 0;
continue;
}
@@ -131,17 +171,20 @@ export class World {
const nextBlock = shadowWorld.get(key);
const next = nextBlock ? {id: nextBlock.id, state: {...nextBlock.state}} : null;
this.events.push({
time: currentTime,
subTick: subTick++,
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) {
this.events.push({time: currentTime, actions: currentBatch});
currentBatch = [];
}
}
}
// Fallback catch if the script ends entirely before tick 0
if (currentBatch.length > 0) {
this.events.push({time: currentTime, actions: currentBatch});
}
if (!capturedInitialState) captureInitial();
this.eventCount = this.events.length - this.initialIndex;
@@ -149,15 +192,6 @@ export class World {
this.reset();
}
#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) {
let targetTime = target;
@@ -166,37 +200,45 @@ export class World {
if (targetTime === undefined) throw new Error(`Label not found: ${target}`);
}
// Scrub Forward
while (this.currentIndex < this.events.length && this.events[this.currentIndex].time <= targetTime) {
const ev = this.events[this.currentIndex];
this.#apply(ev.next, ev.x, ev.y, ev.z);
const batch = this.events[this.currentIndex];
for (const action of batch.actions) {
this.#apply(action.next, action.x, action.y, action.z);
}
this.currentIndex++;
}
// Scrub Backward
while (this.currentIndex > 0 && this.events[this.currentIndex - 1].time > targetTime) {
this.currentIndex--;
const ev = this.events[this.currentIndex];
this.#apply(ev.prev, ev.x, ev.y, ev.z);
const batch = this.events[this.currentIndex];
for (let i = batch.actions.length - 1; i >= 0; i--) {
const action = batch.actions[i];
this.#apply(action.prev, action.x, action.y, action.z);
}
}
this.currentTime = targetTime;
this.notify(); // Dispatch a single batched update to the Engine
this.notify();
}
seekIndex(targetIndex) {
targetIndex = targetIndex + this.initialIndex;
while (this.currentIndex < this.events.length && this.currentIndex < targetIndex) {
const ev = this.events[this.currentIndex];
this.#apply(ev.next, ev.x, ev.y, ev.z);
const batch = this.events[this.currentIndex];
for (const action of batch.actions) {
this.#apply(action.next, action.x, action.y, action.z);
}
this.currentIndex++;
}
while (this.currentIndex > 0 && this.currentIndex > targetIndex) {
this.currentIndex--;
const ev = this.events[this.currentIndex];
this.#apply(ev.prev, ev.x, ev.y, ev.z);
const batch = this.events[this.currentIndex];
for (let i = batch.actions.length - 1; i >= 0; i--) {
const action = batch.actions[i];
this.#apply(action.prev, action.x, action.y, action.z);
}
}
if (this.currentIndex < this.events.length) {
@@ -207,22 +249,4 @@ export class World {
this.notify();
}
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();
}
}