[*] limit tiles in bounds based on width/height/radius

This commit is contained in:
Gavin McDonald
2018-12-02 21:24:34 -05:00
parent fb60481908
commit 69cae5eb1a
7 changed files with 134 additions and 32 deletions

View File

@@ -27,6 +27,8 @@ export default class CartographerFlatXYZ extends Cartographer {
'tileToPixel',
'pixelToTile',
'inBounds',
'boundingBox',
].map(method => this[method] = this[method].bind(this));
}
@@ -92,6 +94,27 @@ export default class CartographerFlatXYZ extends Cartographer {
return new Hex(q, r);
}
inBounds (q, r, s = -q - r) {
if (this.radius) {
if (this.negativeTiles) {
return Math.max(Math.abs(q), Math.abs(r), Math.abs(s)) <= Math.floor(this.radius);
}
else {
return Math.max(Math.abs(q - this.radius), Math.abs(r + this.radius), Math.abs(s)) <= this.radius;
}
}
else if (this.width || this.height) {
if (this.negativeTiles) {
return (!this.width || (Math.abs(q) < this.width / 2))
&& (!this.height || (Math.abs(-r - Math.floor(q / 2)) < (this.height / 2)));
}
else {
return (!this.width || (q >= 0 && q < this.width))
&& (!this.height || (r <= (Math.floor(q / 2) * -1) && (-r - Math.floor(q / 2)) < this.height));
}
}
}
boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
const upperLeftTile = this.pixelToTile(upperLeftPoint);
const lowerLeftTile = this.pixelToTile(lowerLeftPoint);
@@ -106,7 +129,10 @@ export default class CartographerFlatXYZ extends Cartographer {
const bottom = top + height;
const rows = rangeInclusive(top, bottom + 1);
return rows.map(r => new Hex(q, r));
return rows.map(r => ({q, r}))
.reduce((flat, list) => flat.concat(list), [])
.filter(({q, r}) => this.inBounds(q, r))
.map(({q, r}) => new Hex(q, r));
});
}
}