39 lines
818 B
JavaScript
39 lines
818 B
JavaScript
export class Block {
|
|
constructor(id, pos, state) {
|
|
this.id = id;
|
|
this.pos = pos;
|
|
this.state = state;
|
|
}
|
|
}
|
|
|
|
export class World {
|
|
constructor() {
|
|
this.blocks = new Map();
|
|
this.listeners = new Set();
|
|
}
|
|
|
|
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}`;
|
|
this.blocks.set(key, new Block(id, [x, y, z], state));
|
|
this.notify();
|
|
}
|
|
|
|
del(x, y, z) {
|
|
if (this.blocks.delete(`${x},${y},${z}`)) {
|
|
this.notify();
|
|
}
|
|
}
|
|
|
|
get(x, y, z) {
|
|
return this.blocks.get(`${x},${y},${z}`);
|
|
}
|
|
} |