[*] 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 CartographerFlatXY extends Cartographer {
'tileToPixel',
'pixelToTile',
'inBounds',
'boundingBox',
].map(method => this[method] = this[method].bind(this));
}
@@ -92,6 +94,17 @@ export default class CartographerFlatXY extends Cartographer {
return new Square(x, y);
}
inBounds (x, y) {
if (this.negativeTiles) {
return (!this.width || Math.abs(x) <= Math.floor(this.width / 2))
&& (!this.height || Math.abs(y) <= Math.floor(this.height / 2));
}
else {
return (!this.width || (x >= 0 && x < this.width))
&& (!this.height || (y >= 0 && y < this.height));
}
}
boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
const upperLeftTile = this.pixelToTile(upperLeftPoint);
const lowerRightTile = this.pixelToTile(lowerRightPoint);
@@ -100,6 +113,9 @@ export default class CartographerFlatXY extends Cartographer {
const columns = rangeInclusive(upperLeftTile.getX(), upperRightTile.getX());
const rows = rangeInclusive(lowerRightTile.getY(), upperLeftTile.getY());
return columns.map(x => rows.map(y => new Square(x, y)));
return columns.map(x => rows.map(y => ({x, y})))
.reduce((flat, list) => flat.concat(list), [])
.filter(({x, y}) => this.inBounds(x, y))
.map(({x, y}) => new Square(x, y));
}
}