wrapping for radial, hexagonal, maps

This commit is contained in:
Gavin McDonald
2019-01-06 15:34:56 -05:00
parent 28f9647c3a
commit 7c3ee5b475
3 changed files with 102 additions and 154 deletions

View File

@@ -8,6 +8,8 @@ import Point from './point.js';
const tilePointToHex = ({tilePoint, pixelPoint}) => ({tilePoint: new Hex(tilePoint), pixelPoint});
const zeroZeroZero = new Hex({x: 0, y: 0, z: 0});
export default class CartographerPointyXYZ extends Cartographer {
constructor (settings) {
super(settings);
@@ -36,6 +38,41 @@ export default class CartographerPointyXYZ extends Cartographer {
'enforceBoundries',
'boundingBox',
].map(method => this[method] = this[method].bind(this));
if (this.radius) {
this.mirrors = [
new Hex({ // East
x: 2 * this.radius + 1,
y: -this.radius - 1,
z: -this.radius,
}),
new Hex({ // North East
x: this.radius + 1,
y: this.radius,
z: -2 * this.radius - 1,
}),
new Hex({ // North West
x: -this.radius,
y: 2 * this.radius + 1,
z: -this.radius - 1,
}),
new Hex ({ // West
x: -2 * this.radius - 1,
y: this.radius + 1,
z: this.radius,
}),
new Hex ({ // South West
x: -this.radius - 1,
y: -this.radius,
z: 2 * this.radius + 1,
}),
new Hex ({ // South East
x: this.radius,
y: -2 * this.radius - 1,
z: this.radius + 1,
}),
];
}
}
tileHeight () {
@@ -101,11 +138,18 @@ export default class CartographerPointyXYZ extends Cartographer {
teleport (hex) {
hex = hex instanceof Hex ? hex : new Hex(hex);
let {col, row} = Hex.cubeToEvenR(hex);
if (this.radius) {
if (hex.distance(zeroZeroZero) <= this.radius) return hex;
const distances = this.mirrors.map(mirror => hex.distance(mirror));
const mirror = this.mirrors[distances.indexOf(Math.min(...distances))];
return this.teleport(hex.subtractHex(mirror));
}
else {
let {col, row} = Hex.cubeToEvenR(hex);
// ensure odd-width maps wrap properly
if (this.height % 2) {
const offset = Math.floor(row / this.height);