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