[*] wrapping for flatXY
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import Cartographer from './cartographer.js';
|
||||
|
||||
import * as funky from './funky';
|
||||
import {rangeInclusive, invSqrt2} from './utils.js';
|
||||
|
||||
import Square from './square.js';
|
||||
import Point from './point.js';
|
||||
import Square from './square.js';
|
||||
|
||||
const makeASquare = ({x, y}) => new Square(x, y);
|
||||
const tilePointToSquare = ({tilePoint, pixelPoint}) => ({tilePoint: makeASquare(tilePoint), pixelPoint});
|
||||
|
||||
export default class CartographerFlatXY extends Cartographer {
|
||||
constructor(settings) {
|
||||
@@ -28,7 +32,9 @@ export default class CartographerFlatXY extends Cartographer {
|
||||
'tileToPixel',
|
||||
'pixelToTile',
|
||||
|
||||
'teleport',
|
||||
'inBounds',
|
||||
'enforceBoundries',
|
||||
'boundingBox',
|
||||
].map(method => this[method] = this[method].bind(this));
|
||||
}
|
||||
@@ -94,7 +100,22 @@ export default class CartographerFlatXY extends Cartographer {
|
||||
return new Square(x, y);
|
||||
}
|
||||
|
||||
inBounds (x, y) {
|
||||
teleport ({x, y}) {
|
||||
if (this.negativeTiles) {
|
||||
x = x % Math.ceil(this.width / 2);
|
||||
y = y % Math.ceil(this.height / 2);
|
||||
}
|
||||
else {
|
||||
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));
|
||||
@@ -105,6 +126,12 @@ export default class CartographerFlatXY 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);
|
||||
@@ -113,9 +140,16 @@ 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 => ({x, y})))
|
||||
.reduce((flat, list) => flat.concat(list), [])
|
||||
.filter(({x, y}) => this.inBounds(x, y))
|
||||
.map(({x, y}) => new Square(x, y));
|
||||
const makeAPoint = x => rows.map(y => ({x, y}));
|
||||
const makeAPointPair = tilePoint => ({tilePoint, pixelPoint: this.tileToPixel(tilePoint)});
|
||||
|
||||
return funky.chain(columns)
|
||||
.map(makeAPoint)
|
||||
.flatten()
|
||||
.map(makeAPointPair)
|
||||
.map(this.enforceBoundries)
|
||||
.compact()
|
||||
.map(tilePointToSquare)
|
||||
.value();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user