animate
This commit is contained in:
19
world.js
19
world.js
@@ -1,16 +1,30 @@
|
||||
// world.js
|
||||
export class World {
|
||||
constructor() {
|
||||
this.blocks = new Map(); // Keyed by "x,y,z"
|
||||
this.blocks = new Map();
|
||||
this.listeners = new Set();
|
||||
}
|
||||
|
||||
// Subscribe to changes (returns an unsubscribe function)
|
||||
subscribe(callback) {
|
||||
this.listeners.add(callback);
|
||||
return () => this.listeners.delete(callback);
|
||||
}
|
||||
|
||||
notify() {
|
||||
for (const listener of this.listeners) listener();
|
||||
}
|
||||
|
||||
setBlock(id, x, y, z, props = {}) {
|
||||
const key = `${x},${y},${z}`;
|
||||
this.blocks.set(key, { id, x, y, z, props });
|
||||
this.notify();
|
||||
}
|
||||
|
||||
removeBlock(x, y, z) {
|
||||
this.blocks.delete(`${x},${y},${z}`);
|
||||
if (this.blocks.delete(`${x},${y},${z}`)) {
|
||||
this.notify();
|
||||
}
|
||||
}
|
||||
|
||||
updateBlock(x, y, z, newProps) {
|
||||
@@ -18,6 +32,7 @@ export class World {
|
||||
const block = this.blocks.get(key);
|
||||
if (block) {
|
||||
block.props = { ...block.props, ...newProps };
|
||||
this.notify();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user