split 'world', 'frame'.
This commit is contained in:
159
world.js
159
world.js
@@ -1,3 +1,5 @@
|
||||
// world.js
|
||||
|
||||
export class Block {
|
||||
constructor(id, pos, state) {
|
||||
this.id = id;
|
||||
@@ -8,19 +10,11 @@ export class Block {
|
||||
|
||||
export class World {
|
||||
constructor(script = '') {
|
||||
this.blocks = new Map();
|
||||
this.listeners = new Set();
|
||||
|
||||
// Timeline State
|
||||
// Pure Data State
|
||||
this.events = [];
|
||||
this.labels = new Map();
|
||||
|
||||
this.currentIndex = 0;
|
||||
this.currentTime = -Infinity;
|
||||
|
||||
this.initialState = new Map();
|
||||
this.initialIndex = 0;
|
||||
|
||||
this.eventCount = 0;
|
||||
|
||||
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) {
|
||||
let currentTime = -1;
|
||||
let capturedInitialState = false;
|
||||
@@ -104,7 +44,6 @@ export class World {
|
||||
for (let line of lines) {
|
||||
let mergeNext = false;
|
||||
|
||||
// Check for the continuation operator
|
||||
if (line.endsWith('&')) {
|
||||
mergeNext = true;
|
||||
line = line.slice(0, -1).trim();
|
||||
@@ -118,7 +57,6 @@ export class World {
|
||||
|
||||
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 = [];
|
||||
@@ -173,7 +111,6 @@ export class World {
|
||||
|
||||
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 = [];
|
||||
@@ -188,63 +125,125 @@ export class World {
|
||||
if (!capturedInitialState) captureInitial();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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) {
|
||||
let targetTime = target;
|
||||
|
||||
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}`);
|
||||
}
|
||||
|
||||
while (this.currentIndex < this.events.length && this.events[this.currentIndex].time <= targetTime) {
|
||||
const batch = this.events[this.currentIndex];
|
||||
let changed = false;
|
||||
|
||||
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) {
|
||||
this.#apply(action.next, action.x, action.y, action.z);
|
||||
}
|
||||
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--;
|
||||
const batch = this.events[this.currentIndex];
|
||||
const batch = this.world.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);
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
|
||||
this.currentTime = targetTime;
|
||||
this.notify();
|
||||
if (changed) this.notify();
|
||||
}
|
||||
|
||||
seekIndex(targetIndex) {
|
||||
targetIndex = targetIndex + this.initialIndex;
|
||||
targetIndex = targetIndex + this.world.initialIndex;
|
||||
let changed = false;
|
||||
|
||||
while (this.currentIndex < this.events.length && this.currentIndex < targetIndex) {
|
||||
const batch = this.events[this.currentIndex];
|
||||
while (this.currentIndex < this.world.events.length && this.currentIndex < targetIndex) {
|
||||
const batch = this.world.events[this.currentIndex];
|
||||
for (const action of batch.actions) {
|
||||
this.#apply(action.next, action.x, action.y, action.z);
|
||||
}
|
||||
this.currentIndex++;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
while (this.currentIndex > 0 && this.currentIndex > targetIndex) {
|
||||
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--) {
|
||||
const action = batch.actions[i];
|
||||
this.#apply(action.prev, action.x, action.y, action.z);
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (this.currentIndex < this.events.length) {
|
||||
this.currentTime = this.events[this.currentIndex].time;
|
||||
} else if (this.events.length > 0) {
|
||||
this.currentTime = this.events[this.events.length - 1].time;
|
||||
if (this.currentIndex < this.world.events.length) {
|
||||
this.currentTime = this.world.events[this.currentIndex].time;
|
||||
} else if (this.world.events.length > 0) {
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user