chore: tidy

This commit is contained in:
cha0s 2021-03-26 16:12:15 -05:00
parent 69ec257012
commit 8eb0b28bf2

View File

@ -13,17 +13,7 @@ export default class TilesView extends Container {
constructor(tiles, tileset, renderer) {
super();
this.tiles = tiles;
this.tiles.on('update', ([x, y, w, h]) => {
const chunks = this.chunksForUnitExtent([x, y, w, h]);
for (let i = 0; i < chunks.length; ++i) {
const chunk = chunks[i];
const rendered = this.rendered[this.constructor.hashChunk(chunk)];
if (rendered) {
rendered.destroy(true);
}
this.renderChunk(chunk);
}
});
this.tiles.on('update', this.onTilesUpdate, this);
this.tileset = tileset;
this.renderer = renderer;
this.rendered = [];
@ -43,10 +33,7 @@ export default class TilesView extends Container {
chunksForUnitExtent([x, y, w, h]) {
const [fw, fh] = this.tiles.size;
/* eslint-disable no-param-reassign */
if (
x >= fw
|| y >= fh
) {
if (x >= fw || y >= fh) {
return [];
}
if (x < 0) {
@ -57,10 +44,7 @@ export default class TilesView extends Container {
h += y;
y = 0;
}
if (
w <= 0
|| h <= 0
) {
if (w <= 0 || h <= 0) {
return [];
}
if (w > fw - x) {
@ -71,25 +55,23 @@ export default class TilesView extends Container {
}
/* eslint-enable no-param-reassign */
const [cw, ch] = this.constructor.CHUNK_SIZE;
let [cx, cy] = Vector.floor(Vector.div([x, y], [cw, ch]));
let [chunksX, chunksY] = Vector.floor(
Vector.div([w - 1, h - 1], [cw, ch]),
);
let [csx, csy] = Vector.floor(Vector.div([w - 1, h - 1], [cw, ch]));
const [cmx, cmy] = Vector.floor(Vector.mod([x, y], [cw, ch]));
const [cmw, cmh] = Vector.mod([w, h], [cw, ch]);
if (cmx + cmw >= cw) {
chunksX += 1;
csx += 1;
}
if (cmy + cmh >= ch) {
chunksY += 1;
csy += 1;
}
let [cx, cy] = Vector.floor(Vector.div([x, y], [cw, ch]));
const chunks = [];
for (let iy = 0; iy <= chunksY; ++iy) {
for (let ix = 0; ix <= chunksX; ++ix) {
for (let iy = 0; iy <= csy; ++iy) {
for (let ix = 0; ix <= csx; ++ix) {
chunks.push([cx, cy]);
cx += 1;
}
cx -= chunksX + 1;
cx -= csx + 1;
cy += 1;
}
return chunks;
@ -100,20 +82,26 @@ export default class TilesView extends Container {
return (chunk[1] << 16) + chunk[0];
}
renderChunk(chunk) {
if (
chunk[0] < 0
|| chunk[1] < 0
) {
onTilesUpdate([x, y, w, h]) {
const chunks = this.chunksForUnitExtent([x, y, w, h]);
for (let i = 0; i < chunks.length; ++i) {
const chunk = chunks[i];
const rendered = this.rendered[this.constructor.hashChunk(chunk)];
if (rendered) {
rendered.destroy(true);
}
this.renderChunk(chunk);
}
}
renderChunk([cux, cuy]) {
if (cux < 0 || cuy < 0) {
return;
}
const [fw, fh] = this.tiles.size;
const [cw, ch] = this.constructor.CHUNK_SIZE;
const [cx, cy] = Vector.mul(chunk, [cw, ch]);
if (
cx >= fw
|| cy >= fh
) {
const [cx, cy] = Vector.mul([cux, cuy], [cw, ch]);
if (cx >= fw || cy >= fh) {
return;
}
const sw = cx + cw > fw ? (cx + cw) - fw : cw;
@ -163,9 +151,9 @@ export default class TilesView extends Container {
// }
const sprite = new Sprite(canvas.toImage());
sprite.anchor = [0, 0];
sprite.position = [chunk[0] * cw * tw, chunk[1] * ch * th];
sprite.position = [cux * cw * tw, cuy * ch * th];
canvas.destroy();
this.rendered[this.constructor.hashChunk(chunk)] = sprite;
this.rendered[this.constructor.hashChunk([cux, cuy])] = sprite;
this.addChild(sprite);
}