From 9b4f2e92ad20076c93873c0649cc71e9c3c66fcb Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Sat, 15 Dec 2018 11:35:17 -0500 Subject: [PATCH] [*] wrapping for flatXY --- src/cartographerFlatXY.js | 46 ++++++++++++++++++++++++++++++++++----- src/funky.js | 11 ++++++---- src/main.js | 10 +++++++-- src/point.js | 5 +++++ src/tessellate.js | 3 +-- 5 files changed, 61 insertions(+), 14 deletions(-) diff --git a/src/cartographerFlatXY.js b/src/cartographerFlatXY.js index 3ed4eb3..bbe40b7 100644 --- a/src/cartographerFlatXY.js +++ b/src/cartographerFlatXY.js @@ -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(); } } diff --git a/src/funky.js b/src/funky.js index 0f6c337..5f6a683 100644 --- a/src/funky.js +++ b/src/funky.js @@ -16,7 +16,7 @@ export function chain (obj) { } export function compact (obj) { - return filter(obj, val => val ? true : false); + return filter(obj, val => val != null); } export function contains (obj, value) { @@ -64,9 +64,12 @@ export function find (obj, predicate) { } } -export function flatten (list) { - if (Array.isArray(list)) { - return list.reduce((memo, element) => memo.concat(Array.isArray(element) ? flatten(element) : element), []); +export function flatten (obj) { + if (Array.isArray(obj)) { + return obj.reduce((memo, element) => memo.concat(Array.isArray(element) ? flatten(element) : element), []); + } + else { + return reduce((flat, prop) => flat.concat(prop), []) } } diff --git a/src/main.js b/src/main.js index e840376..16b960b 100644 --- a/src/main.js +++ b/src/main.js @@ -130,9 +130,15 @@ class Demo { const key = `${ x },${ z != null ? z : y }`; 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) { diff --git a/src/point.js b/src/point.js index 8d96b1f..50d54b9 100644 --- a/src/point.js +++ b/src/point.js @@ -1,6 +1,11 @@ export default class Point { constructor(x, y) { + if (typeof x === 'object') { + y = x.y; + x = x.x; + } + // add zero to turn -0 into 0 this.x = Math.round(x) + 0; this.y = Math.round(y) + 0; diff --git a/src/tessellate.js b/src/tessellate.js index 063666a..6f4893f 100644 --- a/src/tessellate.js +++ b/src/tessellate.js @@ -231,8 +231,7 @@ export class Tessellate { lowerRightY: height }; - const pointGroups = this.getTilePoints(corners) - .map(tilePoint => ({tilePoint, pixelPoint: this.tileToPixel(tilePoint)})); + const pointGroups = this.getTilePoints(corners); this.settings.draw({ context,