[*] wrapping for flatXY

This commit is contained in:
Gavin McDonald
2018-12-15 11:35:17 -05:00
parent ad4a16b796
commit 9b4f2e92ad
5 changed files with 61 additions and 14 deletions

View File

@@ -1,9 +1,13 @@
import Cartographer from './cartographer.js'; import Cartographer from './cartographer.js';
import * as funky from './funky';
import {rangeInclusive, invSqrt2} from './utils.js'; import {rangeInclusive, invSqrt2} from './utils.js';
import Square from './square.js';
import Point from './point.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 { export default class CartographerFlatXY extends Cartographer {
constructor(settings) { constructor(settings) {
@@ -28,7 +32,9 @@ export default class CartographerFlatXY extends Cartographer {
'tileToPixel', 'tileToPixel',
'pixelToTile', 'pixelToTile',
'teleport',
'inBounds', 'inBounds',
'enforceBoundries',
'boundingBox', 'boundingBox',
].map(method => this[method] = this[method].bind(this)); ].map(method => this[method] = this[method].bind(this));
} }
@@ -94,7 +100,22 @@ export default class CartographerFlatXY extends Cartographer {
return new Square(x, y); 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) { if (this.negativeTiles) {
return (!this.width || Math.abs(x) <= Math.floor(this.width / 2)) return (!this.width || Math.abs(x) <= Math.floor(this.width / 2))
&& (!this.height || Math.abs(y) <= Math.floor(this.height / 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) { boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
const upperLeftTile = this.pixelToTile(upperLeftPoint); const upperLeftTile = this.pixelToTile(upperLeftPoint);
const lowerRightTile = this.pixelToTile(lowerRightPoint); const lowerRightTile = this.pixelToTile(lowerRightPoint);
@@ -113,9 +140,16 @@ export default class CartographerFlatXY extends Cartographer {
const columns = rangeInclusive(upperLeftTile.getX(), upperRightTile.getX()); const columns = rangeInclusive(upperLeftTile.getX(), upperRightTile.getX());
const rows = rangeInclusive(lowerRightTile.getY(), upperLeftTile.getY()); const rows = rangeInclusive(lowerRightTile.getY(), upperLeftTile.getY());
return columns.map(x => rows.map(y => ({x, y}))) const makeAPoint = x => rows.map(y => ({x, y}));
.reduce((flat, list) => flat.concat(list), []) const makeAPointPair = tilePoint => ({tilePoint, pixelPoint: this.tileToPixel(tilePoint)});
.filter(({x, y}) => this.inBounds(x, y))
.map(({x, y}) => new Square(x, y)); return funky.chain(columns)
.map(makeAPoint)
.flatten()
.map(makeAPointPair)
.map(this.enforceBoundries)
.compact()
.map(tilePointToSquare)
.value();
} }
} }

View File

@@ -16,7 +16,7 @@ export function chain (obj) {
} }
export function compact (obj) { export function compact (obj) {
return filter(obj, val => val ? true : false); return filter(obj, val => val != null);
} }
export function contains (obj, value) { export function contains (obj, value) {
@@ -64,9 +64,12 @@ export function find (obj, predicate) {
} }
} }
export function flatten (list) { export function flatten (obj) {
if (Array.isArray(list)) { if (Array.isArray(obj)) {
return list.reduce((memo, element) => memo.concat(Array.isArray(element) ? flatten(element) : element), []); return obj.reduce((memo, element) => memo.concat(Array.isArray(element) ? flatten(element) : element), []);
}
else {
return reduce((flat, prop) => flat.concat(prop), [])
} }
} }

View File

@@ -130,9 +130,15 @@ class Demo {
const key = `${ x },${ z != null ? z : y }`; const key = `${ x },${ z != null ? z : y }`;
const pipMax = this.settings.tile === Tessellate.TILE_STYLES.HEX ? 7 : 9; const pipMax = this.settings.tile === Tessellate.TILE_STYLES.HEX ? 7 : 9;
this.map[key].pips = Tessellate.utils.random(1, pipMax);
console.log({x, y, z}); if (this.map[key]) {
this.map[key].pips = Tessellate.utils.random(1, pipMax);
console.log(key);
}
else {
console.log('ERROR - no tile', key);
}
} }
pressStart(tap) { pressStart(tap) {

View File

@@ -1,6 +1,11 @@
export default class Point { export default class Point {
constructor(x, y) { constructor(x, y) {
if (typeof x === 'object') {
y = x.y;
x = x.x;
}
// add zero to turn -0 into 0 // add zero to turn -0 into 0
this.x = Math.round(x) + 0; this.x = Math.round(x) + 0;
this.y = Math.round(y) + 0; this.y = Math.round(y) + 0;

View File

@@ -231,8 +231,7 @@ export class Tessellate {
lowerRightY: height lowerRightY: height
}; };
const pointGroups = this.getTilePoints(corners) const pointGroups = this.getTilePoints(corners);
.map(tilePoint => ({tilePoint, pixelPoint: this.tileToPixel(tilePoint)}));
this.settings.draw({ this.settings.draw({
context, context,