Skip to content

Commit

Permalink
v0.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
IcculusC committed Oct 18, 2018
1 parent ba7a90a commit 4cf9ad3
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-hex-engine",
"description": "Hexagon Map Editor and Game Engine",
"version": "0.2.2",
"version": "0.2.3",
"main": "lib/index.js",
"author": "IcculusC",
"repository": "IcculusC/react-hex-engine",
Expand Down
12 changes: 6 additions & 6 deletions src/HexUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import Point from "./models/Point";

class HexUtils {
static DIRECTIONS = [
new Hex(1, 0, -1),
new Hex(1, -1, 0),
new Hex(0, -1, 1),
new Hex(-1, 0, 1),
new Hex(-1, 1, 0),
new Hex(0, 1, -1)
new Hex(1, 0),
new Hex(1, -1),
new Hex(0, -1),
new Hex(-1, 0),
new Hex(-1, 1),
new Hex(0, 1)
];

static equals(a, b) {
Expand Down
6 changes: 3 additions & 3 deletions src/Hexagon.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class Hexagon extends Component {

static getDerivedStateFromProps(nextProps, prevState) {
if (prevState.points !== nextProps.points) {
const { layout, points, q, r, s } = nextProps;
const hex = new Hex(q, r, s);
const { layout, points, q, r } = nextProps;
const hex = new Hex(q, r);
const pixel = HexUtils.hexToPixel(hex, nextProps.layout);
return { hex, pixel, layout, points };
}
Expand All @@ -87,7 +87,7 @@ class Hexagon extends Component {

constructor(props) {
super(props);
const hex = new Hex(props.q, props.r, props.s);
const hex = new Hex(props.q, props.r);
const pixel = HexUtils.hexToPixel(hex, props.layout);
this.state = {
...this.state,
Expand Down
18 changes: 14 additions & 4 deletions src/models/Hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ class Hex {
constructor(q, r, s, meta = {}) {
this.q = q;
this.r = r;
this.s = s;
this.meta = {};
if (s !== undefined) {
if (typeof s === "number") {
this.s = s;
this.meta = meta;
} else if (typeof s === "object") {
this.s = -q - r;
this.meta = s;
}
} else {
this.s = -q - r;
this.meta = {};
}
}

toString() {
Expand All @@ -17,8 +27,8 @@ class Hex {
}

static fromJSON(obj) {
const { meta, q, r, s } = obj;
return new Hex(q, r, s, meta);
const { meta, q, r } = obj;
return new Hex(q, r, meta);
}
}

Expand Down
246 changes: 246 additions & 0 deletions test/src/HexUtils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import HexUtils from "../../src/HexUtils";
import Hex from "../../src/models/Hex";
import Orientation from "../../src/models/Orientation";

const hex1 = { q: 0, r: 0, s: 0 };
const hex2 = { q: 0, r: -2, s: 2 };
Expand Down Expand Up @@ -26,6 +28,250 @@ const layout = {
}
};

const map = {
"-2,0,2": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: -2,
r: 0,
s: 2,
tile: "empty"
},
"-2,1,1": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: true
},
q: -2,
r: 1,
s: 1,
tile: "empty"
},
"-2,2,0": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: -2,
r: 2,
s: 0,
tile: "empty"
},
"-1,-1,2": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: -1,
r: -1,
s: 2,
tile: "empty"
},
"-1,0,1": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: -1,
r: 0,
s: 1,
tile: "empty"
},
"-1,1,0": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: -1,
r: 1,
s: 0,
tile: "empty"
},
"-1,2,-1": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: -1,
r: 2,
s: -1,
tile: "empty"
},
"0,-2,2": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 0,
r: -2,
s: 2,
tile: "empty"
},
"0,-1,1": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 0,
r: -1,
s: 1,
tile: "empty"
},
"0,0,0": {
meta: {
hoverable: false,
replaceable: false,
selectable: false,
selected: false
},
q: 0,
r: 0,
s: 0,
tile: "colony"
},
"0,1,-1": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 0,
r: 1,
s: -1,
tile: "empty"
},
"0,2,-2": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 0,
r: 2,
s: -2,
tile: "empty"
},
"1,-2,1": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 1,
r: -2,
s: 1,
tile: "empty"
},
"1,-1,0": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 1,
r: -1,
s: 0,
tile: "empty"
},
"1,0,-1": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 1,
r: 0,
s: -1,
tile: "empty"
},
"1,1,-2": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 1,
r: 1,
s: -2,
tile: "empty"
},
"2,-2,0": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 2,
r: -2,
s: 0,
tile: "empty"
},
"2,-1,-1": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 2,
r: -1,
s: -1,
tile: "empty"
},
"2,0,-2": {
meta: {
hoverable: true,
replaceable: true,
selectable: true,
selected: false
},
q: 2,
r: 0,
s: -2,
tile: "empty"
}
};

const checkLayout = {
spacing: 1.05,
orientation: Orientation.Pointy,
size: {
x: 10,
y: 10
},
origin: {
x: 0,
y: 0
}
};

test("HexUtils.equals should work", () => {
expect(HexUtils.equals(hex1, hex1)).toBe(true);
expect(HexUtils.equals(hex1, hex2)).toBe(false);
Expand Down

0 comments on commit 4cf9ad3

Please sign in to comment.