Finite boards (#1)

This commit is contained in:
gavin
2018-07-15 02:54:26 +00:00
committed by Gitea
parent 68f83bfc4b
commit 0545c0a5d9
14 changed files with 497 additions and 356 deletions

View File

@@ -14,17 +14,21 @@ export {DrawCircle, DrawHexagon, DrawSquare};
import Cell from './cell.js';
export {Cell};
import CartographerXY from './cartographerXY.js';
import CartographerXYZ from './cartographerXYZ.js';
import CartographerFlatXY from './cartographerFlatXY.js';
import CartographerPointyXY from './cartographerPointyXY.js';
import CartographerFlatXYZ from './cartographerFlatXYZ.js';
import CartographerPointyXYZ from './cartographerPointyXYZ.js';
const HEX = 'hex';
const CIRCLE = 'circle';
const SQUARE = 'square';
const TILE_STYLES = {HEX, CIRCLE, SQUARE};
import {
HEX, CIRCLE, SQUARE,
TILE_STYLES,
BOARD_STYLES,
FLAT, POINTY,
TILE_ORIENTATIONS,
} from './consts.js';
const TILES = {
[HEX]: new DrawHexagon(),
[HEX]: new DrawHexagon(),
[CIRCLE]: new DrawCircle(),
[SQUARE]: new DrawSquare(),
};
@@ -34,18 +38,34 @@ const DEFAULTS = {
board: HEX,
tap: utils.noop,
draw: utils.noop,
pointyTop: false,
orientation: FLAT,
};
function selectCartographer(board, orientation) {
switch (board) {
case HEX:
switch (orientation) {
case FLAT: return CartographerFlatXYZ;
case POINTY: return CartographerPointyXYZ;
}
case SQUARE:
switch (orientation) {
case FLAT: return CartographerFlatXY;
case POINTY: return CartographerPointyXY;
}
}
}
export default class Tessellate {
static get TILES () {return TILES}
static get TILE_STYLES () {return TILE_STYLES}
static get BOARD_STYLES () {return {HEX, SQUARE}}
static get TILES() {return TILES}
static get TILE_STYLES() {return TILE_STYLES}
static get BOARD_STYLES() {return BOARD_STYLES}
static get TILE_ORIENTATIONS() {return TILE_ORIENTATIONS}
constructor(settings) {
['seedTiles', 'tap', 'draw', 'drawMap', 'move', 'zoom'].map(method => {this[method] = this[method].bind(this)});
this.settings = utils.extend(DEFAULTS, settings);
this.settings = Object.assign(DEFAULTS, settings);
this.settings.element = this.settings.element instanceof HTMLElement ? this.settings.element :
document.querySelector(this.settings.element);
@@ -64,61 +84,33 @@ export default class Tessellate {
this.map = [];
this.seedTiles();
const boardSettings = {
pointyTop: this.settings.pointyTop,
originX: this.sketch.getContext().canvas.width / 2,
originY: this.sketch.getContext().canvas.height / 2
};
const cartographer = selectCartographer(this.settings.board, this.settings.orientation);
this.cartographer = new cartographer({
height: this.settings.height,
width: this.settings.width,
scale: this.settings.scale,
this.cartographer = this.settings.board === HEX ? new CartographerXYZ(boardSettings) : new CartographerXY(boardSettings);
canvasWidth: this.sketch.getContext().canvas.width,
canvasHeight: this.sketch.getContext().canvas.height,
originX: this.settings.originX || this.sketch.getContext().canvas.width / 2,
originY: this.settings.originY || this.sketch.getContext().canvas.height / 2,
});
}
seedTiles() {
this.map[0] = [];
// this.map[1] = [];
// this.map[-1] = [];
// this.map[-5] = [];
this.map[0][0] = new Cell({
x: 0,
y: 0,
pointyTop: this.settings.pointyTop,
orientation: this.settings.orientation,
red: 0,
green: 0,
blue: 0,
alpha: 75/100,
scale: 9/10,
});
// this.map[1][1] = new Cell({
// x: 1,
// y: 1,
// pointyTop: this.settings.pointyTop,
// red: 0,
// green: 0,
// blue: 255,
// alpha: 90/100,
// scale: 9/10,
// });
// this.map[-1][-1] = new Cell({
// x: -1,
// y: -1,
// pointyTop: this.settings.pointyTop,
// red: 255,
// green: 0,
// blue: 0,
// alpha: 90/100,
// scale: 9/10,
// });
// this.map[-5][5] = new Cell({
// x: -5,
// y: 5,
// pointyTop: this.settings.pointyTop,
// red: 0,
// green: 255,
// blue: 0,
// alpha: 90/100,
// scale: 9/10,
// });
}
tap(event) {
@@ -161,7 +153,7 @@ export default class Tessellate {
this.map[tile.getX()][tile.getY()] = new Cell({
x: tile.getX(),
y: tile.getY(),
pointyTop: this.settings.pointyTop,
orientation: this.settings.orientation,
red: utils.random(64, 192),
green: utils.random(64, 192),
blue: utils.random(64, 192),