wrapping for flat hex tiles almost working

This commit is contained in:
Gavin McDonald
2018-12-28 16:31:55 -05:00
parent e631691289
commit c77f179ad3
5 changed files with 132 additions and 45 deletions

View File

@@ -35,11 +35,26 @@ function roundOff(hex) {
}
export default class Hex extends Point {
static offsetToCube (col, row) {
const x = col;
const z = row - (col + (col & 1)) / 2;
const y = -x - z;
return new Hex(x, y, z);
}
constructor() {
super();
if (arguments.length === 2) { // hex = Hex(q, r);
if (arguments.length === 1) {
const {q, r, s = -q - r} = arguments[0];
const {x, y, z = -x - y} = arguments[0];
this.x = !isNaN(q) ? q : x;
this.y = !isNaN(s) ? s : y;
this.z = !isNaN(r) ? r : z;
}
else if (arguments.length === 2) { // hex = Hex(q, r);
this.x = arguments[0];
this.z = arguments[1];
this.y = computeY(this.x, this.z);
@@ -57,65 +72,76 @@ export default class Hex extends Point {
getY() {return this.y;}
getZ() {return this.z;}
setX(newX) {this.x = newX; return this;}
setY(newY) {this.y = newY; return this;}
setZ(newZ) {this.z = newZ; return this;}
setX (newX) {this.x = newX; return this;}
setY (newY) {this.y = newY; return this;}
setZ (newZ) {this.z = newZ; return this;}
moveX(byX) {this.x += byX; return this;}
moveY(byY) {this.y += byY; return this;}
moveZ(byZ) {this.z += byZ; return this;}
moveX (byX) {this.x += byX; return this;}
moveY (byY) {this.y += byY; return this;}
moveZ (byZ) {this.z += byZ; return this;}
getQ() {return this.x;}
getR() {return this.z;}
getQ () {return this.x;}
getR () {return this.z;}
setQ(newQ) {
setQ (newQ) {
this.x = newQ;
this.y = computeY(this.x, this.z);
return this;
}
setR(newR) {
setR (newR) {
this.z = newR;
this.y = computeY(this.x, this.z);
return this;
}
moveQ(byQ) {
moveQ (byQ) {
this.x += byQ;
this.y = computeY(this.x, this.z);
return this;
}
moveR(byR) {
moveR (byR) {
this.z += byR;
this.y = computeY(this.x, this.z);
return this;
}
getPoint() { return {x: this.x, y: this.y, z: this.z}; }
getPoint () { return {x: this.x, y: this.y, z: this.z}; }
setHex(newHex) {
setHex (newHex) {
this.x = newHex.x;
this.y = newHex.y;
this.z = newHex.z;
return this;
}
moveHex(byHex) {
moveHex (byHex) {
this.x += byHex.x;
this.y += byHex.y;
this.z += byHex.z;
return this;
}
getAxial() {return {q: this.x, r: this.z};}
setAxial(newAxial) {
getAxial () {return {q: this.x, r: this.z};}
setAxial (newAxial) {
this.x = newAxial.q;
this.z = newAxial.r;
this.y = computeY(this.x, this.y);
return this;
}
moveAxial(byAxial) {
moveAxial (byAxial) {
this.x += byAxial.q;
this.z += byAxial.r;
this.y = computeY(this.x, this.z);
return this;
}
getOffsetHex () {
const col = this.x;
const row = this.z + (this.x + (this.x & 1)) / 2;
return {col, row};
}
}