You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
837 B
43 lines
837 B
import { fabric } from "fabric";
|
|
import TileSet from "./tileset";
|
|
|
|
const image = new fabric.Image();
|
|
|
|
export default class TileMap {
|
|
/**
|
|
* @param {TileSet} tileset
|
|
* @param {{chunkSiz:number}} opts
|
|
*/
|
|
constructor(tileset, opts = {}) {
|
|
this.tileset = tileset;
|
|
|
|
this.opts = {
|
|
chunkSize: 8,
|
|
...opts,
|
|
}
|
|
|
|
this.chunks = [];
|
|
}
|
|
|
|
getChunk(x, y) {
|
|
const cs = this.opts.chunkSize;
|
|
const cx = x > 0 ? Math.floor(x / cs) : Math.ceil(x / cs);
|
|
const cy = y > 0 ? Math.floor(y / cs) : Math.ceil(y / cs);
|
|
|
|
let chunk = this.chunks.find(c => c.x === cx && c.y === cy);
|
|
if (chunk == null) {
|
|
chunk = new Chunk(x, y, cs);
|
|
this.chunks.push(chunk);
|
|
}
|
|
|
|
return chunk;
|
|
}
|
|
}
|
|
|
|
class Chunk {
|
|
constructor(x, y, size) {
|
|
this.x = x;
|
|
this.y = y;
|
|
this.size = size;
|
|
}
|
|
}
|