[*] limit tiles in bounds based on width/height/radius

This commit is contained in:
Gavin McDonald
2018-12-02 21:24:34 -05:00
parent fb60481908
commit 69cae5eb1a
7 changed files with 134 additions and 32 deletions

View File

@@ -28,7 +28,13 @@ export default class Cartographer {
this.settings = Object.assign({}, DEFAULTS, settings); this.settings = Object.assign({}, DEFAULTS, settings);
Object.assign(this, pick(this.settings, ['height', 'width', 'negativeTiles'])); Object.assign(this, pick(this.settings, [
'height',
'negativeTiles',
'radius',
'width',
'wrap',
]));
this.checkScale(this.settings.canvasHeight, this.settings.canvasWidth); this.checkScale(this.settings.canvasHeight, this.settings.canvasWidth);
@@ -102,6 +108,9 @@ export default class Cartographer {
const maxX = this.tileWidth() / 2; const maxX = this.tileWidth() / 2;
const minY = canvasHeight - (this.tileHeight() / 2); const minY = canvasHeight - (this.tileHeight() / 2);
if (this.wrap) {
}
else {
this.originX = this.originX > maxX ? maxX : this.originX; this.originX = this.originX > maxX ? maxX : this.originX;
this.originY = this.originY < minY ? minY : this.originY; this.originY = this.originY < minY ? minY : this.originY;
@@ -119,11 +128,15 @@ export default class Cartographer {
this.originY = this.originY > maxY ? maxY : this.originY; this.originY = this.originY > maxY ? maxY : this.originY;
} }
} }
}
_checkMoveNegativeTiles (event) { _checkMoveNegativeTiles (event) {
const colWidth = this.horizontalDistance(); const colWidth = this.horizontalDistance();
const rowHeight = this.verticalDistance(); const rowHeight = this.verticalDistance();
if (this.wrap) {
}
else {
if (this.width) { if (this.width) {
const canvasWidth = event.width; const canvasWidth = event.width;
const halfBoardWidth = (this.width * colWidth + this.horizontalOverhang()) / 2; const halfBoardWidth = (this.width * colWidth + this.horizontalOverhang()) / 2;
@@ -142,6 +155,7 @@ export default class Cartographer {
this.originY; this.originY;
} }
} }
}
zoom (event) { zoom (event) {
const scaleOrig = this.scale; const scaleOrig = this.scale;

View File

@@ -27,6 +27,8 @@ export default class CartographerFlatXY extends Cartographer {
'tileToPixel', 'tileToPixel',
'pixelToTile', 'pixelToTile',
'inBounds',
'boundingBox', 'boundingBox',
].map(method => this[method] = this[method].bind(this)); ].map(method => this[method] = this[method].bind(this));
} }
@@ -92,6 +94,17 @@ export default class CartographerFlatXY extends Cartographer {
return new Square(x, y); return new Square(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));
}
else {
return (!this.width || (x >= 0 && x < this.width))
&& (!this.height || (y >= 0 && y < this.height));
}
}
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);
@@ -100,6 +113,9 @@ 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 => new Square(x, y))); 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));
} }
} }

View File

@@ -27,6 +27,8 @@ export default class CartographerFlatXYZ extends Cartographer {
'tileToPixel', 'tileToPixel',
'pixelToTile', 'pixelToTile',
'inBounds',
'boundingBox', 'boundingBox',
].map(method => this[method] = this[method].bind(this)); ].map(method => this[method] = this[method].bind(this));
} }
@@ -92,6 +94,27 @@ export default class CartographerFlatXYZ extends Cartographer {
return new Hex(q, r); return new Hex(q, r);
} }
inBounds (q, r, s = -q - r) {
if (this.radius) {
if (this.negativeTiles) {
return Math.max(Math.abs(q), Math.abs(r), Math.abs(s)) <= Math.floor(this.radius);
}
else {
return Math.max(Math.abs(q - this.radius), Math.abs(r + this.radius), Math.abs(s)) <= this.radius;
}
}
else if (this.width || this.height) {
if (this.negativeTiles) {
return (!this.width || (Math.abs(q) < this.width / 2))
&& (!this.height || (Math.abs(-r - Math.floor(q / 2)) < (this.height / 2)));
}
else {
return (!this.width || (q >= 0 && q < this.width))
&& (!this.height || (r <= (Math.floor(q / 2) * -1) && (-r - Math.floor(q / 2)) < this.height));
}
}
}
boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) { boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
const upperLeftTile = this.pixelToTile(upperLeftPoint); const upperLeftTile = this.pixelToTile(upperLeftPoint);
const lowerLeftTile = this.pixelToTile(lowerLeftPoint); const lowerLeftTile = this.pixelToTile(lowerLeftPoint);
@@ -106,7 +129,10 @@ export default class CartographerFlatXYZ extends Cartographer {
const bottom = top + height; const bottom = top + height;
const rows = rangeInclusive(top, bottom + 1); const rows = rangeInclusive(top, bottom + 1);
return rows.map(r => new Hex(q, r)); return rows.map(r => ({q, r}))
.reduce((flat, list) => flat.concat(list), [])
.filter(({q, r}) => this.inBounds(q, r))
.map(({q, r}) => new Hex(q, r));
}); });
} }
} }

View File

@@ -27,6 +27,8 @@ export default class CartographerPointyXY extends Cartographer {
'tileToPixel', 'tileToPixel',
'pixelToTile', 'pixelToTile',
'inBounds',
'boundingBox', 'boundingBox',
].map(method => this[method] = this[method].bind(this)); ].map(method => this[method] = this[method].bind(this));
} }
@@ -97,6 +99,17 @@ export default class CartographerPointyXY extends Cartographer {
return new Square(x, y); return new Square(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));
}
else {
return (!this.width || (x >= 0 && x < this.width))
&& (!this.height || (y >= 0 && y < this.height));
}
}
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);
@@ -125,7 +138,10 @@ export default class CartographerPointyXY extends Cartographer {
// push out by 1 on either end to account for interlocking tiles // push out by 1 on either end to account for interlocking tiles
const rows = rangeInclusive(bottom - 1, top + 1); const rows = rangeInclusive(bottom - 1, top + 1);
return rows.map(y => new Square(x, y)); return 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));
}); });
} }
} }

View File

@@ -27,6 +27,8 @@ export default class CartographerPointyXYZ extends Cartographer {
'tileToPixel', 'tileToPixel',
'pixelToTile', 'pixelToTile',
'inBounds',
'boundingBox', 'boundingBox',
].map(method => this[method] = this[method].bind(this)); ].map(method => this[method] = this[method].bind(this));
} }
@@ -92,6 +94,27 @@ export default class CartographerPointyXYZ extends Cartographer {
return new Hex(q, r); return new Hex(q, r);
} }
inBounds (q, r, s = -q - r) {
if (this.radius) {
if (this.negativeTiles) {
return Math.max(Math.abs(q), Math.abs(r), Math.abs(s)) <= Math.floor(this.radius);
}
else {
return Math.max(Math.abs(q - this.radius), Math.abs(r + this.radius), Math.abs(s)) <= this.radius;
}
}
else if (this.width || this.height) {
if (this.negativeTiles) {
return (!this.height || (Math.abs(r) < this.height / 2))
&& (!this.width || (Math.abs(-q - Math.floor(r / 2)) < (this.width / 2)));
}
else {
return (!this.height || (r >= 0 && r < this.height))
&& (!this.width || (q <= (Math.floor(r / 2)) && (q - Math.floor(r / 2)) < this.width));
}
}
}
boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) { boundingBox(upperLeftPoint, upperRightPoint, lowerLeftPoint, lowerRightPoint) {
const upperLeftTile = this.pixelToTile(upperLeftPoint); const upperLeftTile = this.pixelToTile(upperLeftPoint);
const lowerLeftTile = this.pixelToTile(lowerLeftPoint); const lowerLeftTile = this.pixelToTile(lowerLeftPoint);
@@ -106,7 +129,10 @@ export default class CartographerPointyXYZ extends Cartographer {
const right = left + width; const right = left + width;
const columns = rangeInclusive(left, right + 1); const columns = rangeInclusive(left, right + 1);
return columns.map(q => new Hex(q, r)); return columns.map(q => ({q, r}))
.reduce((flat, list) => flat.concat(list), [])
.filter(({q, r}) => this.inBounds(q, r))
.map(({q, r}) => new Hex(q, r));
}); });
} }
} }

View File

@@ -131,6 +131,8 @@ class Demo {
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); this.map[key].pips = Tessellate.utils.random(1, pipMax);
console.log({x, y, z});
} }
pressStart(tap) { pressStart(tap) {

View File

@@ -116,9 +116,11 @@ export class Tessellate {
'centerX', 'centerX',
'centerY', 'centerY',
'height', 'height',
'width',
'scale',
'negativeTiles', 'negativeTiles',
'radius',
'scale',
'width',
'wrap',
]))); ])));
} }