106 lines
2.1 KiB
JavaScript
106 lines
2.1 KiB
JavaScript
import Cartographer from './cartographer.js';
|
|
|
|
import {rangeInclusive, sqrt2} from './utils.js';
|
|
|
|
import Square from './square.js';
|
|
import Point from './point.js';
|
|
|
|
export default class CartographerFlatXY extends Cartographer {
|
|
constructor(settings) {
|
|
super(settings);
|
|
|
|
[
|
|
'tileHeight',
|
|
'tileWidth',
|
|
|
|
'maxWidth',
|
|
'minWidth',
|
|
|
|
'horizontalOverhang',
|
|
'verticalOverhang',
|
|
|
|
'horizontalDistance',
|
|
'verticalDistance',
|
|
|
|
'calculateHorizontalScale',
|
|
'calculateVerticalScale',
|
|
|
|
'tileToPixel',
|
|
'pixelToTile',
|
|
'boundingBox',
|
|
].map(method => this[method] = this[method].bind(this));
|
|
}
|
|
|
|
tileHeight() {
|
|
return this.minWidth();
|
|
}
|
|
|
|
tileWidth() {
|
|
return this.minWidth();
|
|
}
|
|
|
|
maxWidth() {
|
|
return this.minWidth() * sqrt2;
|
|
}
|
|
|
|
minWidth() {
|
|
return this.scale * 2;
|
|
}
|
|
|
|
horizontalOverhang() {
|
|
return 0;
|
|
}
|
|
|
|
verticalOverhang() {
|
|
return 0;
|
|
}
|
|
|
|
horizontalDistance() {
|
|
return this.minWidth();
|
|
}
|
|
|
|
verticalDistance() {
|
|
return this.minWidth();
|
|
}
|
|
|
|
calculateHorizontalScale(pixels, tiles) {
|
|
return pixels / tiles / 2;
|
|
}
|
|
|
|
calculateVerticalScale(pixels, tiles) {
|
|
return pixels / tiles / 2;
|
|
}
|
|
|
|
tileToPixel(square) {
|
|
square = square instanceof Square ? square : new Square(...arguments);
|
|
|
|
const x = square.getX() * this.minWidth();
|
|
const y = square.getY() * this.minWidth();
|
|
|
|
return new Point(x + this.originX, this.originY - y);
|
|
}
|
|
|
|
pixelToTile(point) {
|
|
point = point instanceof Point ? point : new Point(...arguments);
|
|
|
|
const pixelX = point.getX() - this.originX;
|
|
const pixelY = this.originY - point.getY();
|
|
|
|
const x = pixelX / this.minWidth();
|
|
const y = pixelY / this.minWidth();
|
|
|
|
return new Square(x, y);
|
|
}
|
|
|
|
boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
|
|
const upperLeftTile = this.pixelToTile(upperLeftPoint);
|
|
const lowerRightTile = this.pixelToTile(lowerRightPoint);
|
|
const upperRightTile = this.pixelToTile(upperRightPoint);
|
|
|
|
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)));
|
|
}
|
|
}
|