wrapping for pointyXY

This commit is contained in:
Gavin McDonald
2018-12-16 14:11:56 -05:00
parent 9b4f2e92ad
commit e631691289
4 changed files with 67 additions and 32 deletions

View File

@@ -1,10 +1,13 @@
import Cartographer from './cartographer.js';
import * as funky from './funky';
import {rangeInclusive, invSqrt2, sqrt2} from './utils.js';
import Square from './square.js';
import Point from './point.js';
const tilePointToSquare = ({tilePoint, pixelPoint}) => ({tilePoint: new Square(tilePoint), pixelPoint});
export default class CartographerPointyXY extends Cartographer {
constructor(settings) {
super(settings);
@@ -26,9 +29,11 @@ export default class CartographerPointyXY extends Cartographer {
'calculateVerticalScale',
'tileToPixel',
'pixelToTile',
'_pixelToTile',
'teleport',
'inBounds',
'enforceBoundries',
'boundingBox',
].map(method => this[method] = this[method].bind(this));
}
@@ -86,7 +91,7 @@ export default class CartographerPointyXY extends Cartographer {
return new Point(pixelX + this.originX, this.originY - pixelY);
}
pixelToTile(point) {
_pixelToTile (point) {
point = point instanceof Point ? point : new Point(...arguments);
const pixelX = point.getX() - this.originX;
@@ -99,7 +104,17 @@ export default class CartographerPointyXY extends Cartographer {
return new Square(x, y);
}
inBounds (x, y) {
teleport ({x, y}) {
x = x % this.width;
y = y % this.height;
x = x < 0 ? this.width + x : x;
y = y < 0 ? this.height + y : y;
return new Point(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));
@@ -110,11 +125,17 @@ export default class CartographerPointyXY extends Cartographer {
}
}
enforceBoundries ({tilePoint, pixelPoint}) {
return this.wrap ? ({tilePoint: this.teleport(tilePoint), pixelPoint}) :
this.inBounds(tilePoint) ? ({tilePoint, pixelPoint}) :
null;
}
boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
const upperLeftTile = this.pixelToTile(upperLeftPoint);
const lowerRightTile = this.pixelToTile(lowerRightPoint);
const upperRightTile = this.pixelToTile(upperRightPoint);
const lowerLeftTile = this.pixelToTile(lowerLeftPoint);
const upperLeftTile = this._pixelToTile(upperLeftPoint);
const lowerRightTile = this._pixelToTile(lowerRightPoint);
const upperRightTile = this._pixelToTile(upperRightPoint);
const lowerLeftTile = this._pixelToTile(lowerLeftPoint);
const columns = rangeInclusive(lowerLeftTile.getX(), upperRightTile.getX());
@@ -128,7 +149,10 @@ export default class CartographerPointyXY extends Cartographer {
const midway = columns.length % 2 ? columns[aboutHalf] :
(columns[aboutHalf - 1] + columns[aboutHalf]) / 2;
return columns.map(x => {
const makeAPointPair = tilePoint => ({tilePoint, pixelPoint: this.tileToPixel(tilePoint)});
return funky.chain(columns)
.map(x => {
let top = x < midway ? upperLeftIntercept + x : upperRightIntercept - x;
let bottom = x < midway ? lowerRightIntercept - x : lowerLeftIntercept + x;
@@ -138,10 +162,17 @@ export default class CartographerPointyXY extends Cartographer {
// push out by 1 on either end to account for interlocking tiles
const rows = rangeInclusive(bottom - 1, top + 1);
return 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));
});
const makeAPoint = y => ({x, y});
return funky.chain(rows)
.map(makeAPoint)
.map(makeAPointPair)
.map(this.enforceBoundries)
.compact()
.map(tilePointToSquare)
.value();
})
.flatten()
.value();
}
}