Skip to content

Commit

Permalink
v0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
IcculusC committed Oct 18, 2018
1 parent 7d69077 commit ba7a90a
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 169 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.1",
"version": "0.2.2",
"main": "lib/index.js",
"author": "IcculusC",
"repository": "IcculusC/react-hex-engine",
Expand Down
13 changes: 9 additions & 4 deletions src/Hexagon.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Hexagon extends Component {
children: PropTypes.node,
classes: PropTypes.objectOf(PropTypes.any),
data: PropTypes.object,
hoverable: PropTypes.bool,
highlighted: PropTypes.bool,
layout: PropTypes.objectOf(PropTypes.any).isRequired,
onClick: PropTypes.func,
Expand All @@ -26,14 +27,14 @@ class Hexagon extends Component {
q: PropTypes.number.isRequired,
r: PropTypes.number.isRequired,
s: PropTypes.number.isRequired,
selectable: PropTypes.bool,
selected: PropTypes.bool,
showCoordinates: PropTypes.bool,
text: PropTypes.string,
TextProps: PropTypes.objectOf(PropTypes.any)
};

static defaultProps = {
className: "",
classes: {
group: "",
hexagon: "",
Expand All @@ -46,6 +47,8 @@ class Hexagon extends Component {
selected: "",
text: ""
},
hoverable: true,
selectable: true,
showCoordinates: false,
text: "",
TextProps: {}
Expand Down Expand Up @@ -157,11 +160,13 @@ class Hexagon extends Component {
const {
classes,
highlighted,
hoverable,
layout,
points,
q,
r,
s,
selectable,
selected,
showCoordinates,
text,
Expand Down Expand Up @@ -199,15 +204,15 @@ class Hexagon extends Component {
>
<g
className={classNames("hexagon", classes.hexagon, {
[classes.selected]: selected,
[classes.selected]: selectable && selected,
[classes.highlighted]: highlighted,
[classes.hovered]: hovered
[classes.hovered]: hoverable && hovered
})}
>
<polygon className={classes.polygon} points={points} />
{this.props.children}
{text ? (
<Text className={classes.text} {...TextProps}>
<Text classes={{ text: classes.text }} {...TextProps}>
{text}
</Text>
) : null}
Expand Down
14 changes: 2 additions & 12 deletions src/Layout.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Component } from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import memoize from "memoize-one";
import Hex from "./models/Hex";
import HexUtils from "./HexUtils";
Expand All @@ -11,7 +10,6 @@ import { LayoutProvider, withViewBox } from "./Context";
export class Layout extends Component {
static propTypes = {
children: PropTypes.node.isRequired,
className: PropTypes.string,
classes: PropTypes.objectOf(PropTypes.string),
flat: PropTypes.bool,
origin: PropTypes.object,
Expand Down Expand Up @@ -84,15 +82,7 @@ export class Layout extends Component {
});

render() {
const {
flat,
children,
classes,
className,
size,
viewBox,
...rest
} = this.props;
const { flat, children, classes, size, viewBox, ...rest } = this.props;
const orientation = flat ? Orientation.Flat : Orientation.Pointy;
const points = Layout.calculateCoordinates(orientation, size)
.map(point => point.toString())
Expand All @@ -101,7 +91,7 @@ export class Layout extends Component {
const inBounds = this.filterChildren(children, layout, viewBox);
return (
<LayoutProvider value={{ layout, points }}>
<g className={classNames(className, classes.layout)}>{inBounds}</g>
<g className={classes.layout}>{inBounds}</g>
</LayoutProvider>
);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Text.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React, { Component } from "react";
import PropTypes from "prop-types";
import classNames from "classnames";

class Text extends Component {
static propTypes = {
anchor: PropTypes.string,
children: PropTypes.string,
className: PropTypes.string,
classes: PropTypes.objectOf(PropTypes.string),
x: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
y: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
Expand All @@ -18,10 +16,10 @@ class Text extends Component {
};

render() {
const { anchor, children, className, classes, x, y } = this.props;
const { anchor, children, classes, x, y } = this.props;
return (
<text
className={classNames(className, classes.text)}
className={classes.text}
textAnchor={anchor}
x={x || 0}
y={y ? y : "0.3em"}
Expand Down
11 changes: 6 additions & 5 deletions src/models/Hex.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Hex {
constructor(q, r, s) {
constructor(q, r, s, meta = {}) {
this.q = q;
this.r = r;
this.s = s;
this.meta = {};
}

toString() {
Expand All @@ -11,13 +12,13 @@ class Hex {
}

toJSON() {
const { q, r, s } = this;
return { q, r, s };
const { meta, q, r, s } = this;
return { meta, q, r, s };
}

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

Expand Down
114 changes: 52 additions & 62 deletions test/src/GridGenerator.test.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,66 @@
import GridGenerator from '../../src/GridGenerator';
import GridGenerator from "../../src/GridGenerator";

test('getGenerator should work when the request exists', () => {
expect(GridGenerator.getGenerator('rectangle')).toBe(GridGenerator.rectangle);
test("getGenerator should work when the request exists", () => {
expect(GridGenerator.getGenerator("rectangle")).toBe(GridGenerator.rectangle);
});

test('getGenerator should work when the request does not exist', () => {
expect(GridGenerator.getGenerator('bogus')).toBeNull();
test("getGenerator should work when the request does not exist", () => {
expect(GridGenerator.getGenerator("bogus")).toBeNull();
});

test('parallelogram should work', () => {
expect(GridGenerator.parallelogram(0, 1, 0, 1)).toEqual(
[
{ q: 0, r: 0, s: -0 },
{ q: 0, r: 1, s: -1 },
{ q: 1, r: 0, s: -1 },
{ q: 1, r: 1, s: -2 },
],
);
test("parallelogram should work", () => {
expect(GridGenerator.parallelogram(0, 1, 0, 1)).toEqual([
{ meta: {}, q: 0, r: 0, s: -0 },
{ meta: {}, q: 0, r: 1, s: -1 },
{ meta: {}, q: 1, r: 0, s: -1 },
{ meta: {}, q: 1, r: 1, s: -2 }
]);
});

test('triangle should work', () => {
expect(GridGenerator.triangle(1)).toEqual(
[
{ q: 0, r: 0, s: -0 },
{ q: 0, r: 1, s: -1 },
{ q: 1, r: 0, s: -1 },
],
);
test("triangle should work", () => {
expect(GridGenerator.triangle(1)).toEqual([
{ meta: {}, q: 0, r: 0, s: -0 },
{ meta: {}, q: 0, r: 1, s: -1 },
{ meta: {}, q: 1, r: 0, s: -1 }
]);
});

test('hexagon should work', () => {
expect(GridGenerator.hexagon(1)).toEqual(
[
{ q: -1, r: 0, s: 1 },
{ q: -1, r: 1, s: 0 },
{ q: 0, r: -1, s: 1 },
{ q: 0, r: 0, s: -0 },
{ q: 0, r: 1, s: -1 },
{ q: 1, r: -1, s: 0 },
{ q: 1, r: 0, s: -1 },
],
);
test("hexagon should work", () => {
expect(GridGenerator.hexagon(1)).toEqual([
{ meta: {}, q: -1, r: 0, s: 1 },
{ meta: {}, q: -1, r: 1, s: 0 },
{ meta: {}, q: 0, r: -1, s: 1 },
{ meta: {}, q: 0, r: 0, s: -0 },
{ meta: {}, q: 0, r: 1, s: -1 },
{ meta: {}, q: 1, r: -1, s: 0 },
{ meta: {}, q: 1, r: 0, s: -1 }
]);
});

test('rectangle should work', () => {
expect(GridGenerator.rectangle(3, 3)).toEqual(
[
{ q: -0, r: 0, s: 0 },
{ q: 1, r: 0, s: -1 },
{ q: 2, r: 0, s: -2 },
{ q: -0, r: 1, s: -1 },
{ q: 1, r: 1, s: -2 },
{ q: 2, r: 1, s: -3 },
{ q: -1, r: 2, s: -1 },
{ q: 0, r: 2, s: -2 },
{ q: 1, r: 2, s: -3 },
],
);
test("rectangle should work", () => {
expect(GridGenerator.rectangle(3, 3)).toEqual([
{ meta: {}, q: -0, r: 0, s: 0 },
{ meta: {}, q: 1, r: 0, s: -1 },
{ meta: {}, q: 2, r: 0, s: -2 },
{ meta: {}, q: -0, r: 1, s: -1 },
{ meta: {}, q: 1, r: 1, s: -2 },
{ meta: {}, q: 2, r: 1, s: -3 },
{ meta: {}, q: -1, r: 2, s: -1 },
{ meta: {}, q: 0, r: 2, s: -2 },
{ meta: {}, q: 1, r: 2, s: -3 }
]);
});

test('orientedRectangle should work', () => {
expect(GridGenerator.orientedRectangle(3, 3)).toEqual(
[
{ q: 0, r: -0, s: 0 },
{ q: 0, r: 1, s: -1 },
{ q: 0, r: 2, s: -2 },
{ q: 1, r: -0, s: -1 },
{ q: 1, r: 1, s: -2 },
{ q: 1, r: 2, s: -3 },
{ q: 2, r: -1, s: -1 },
{ q: 2, r: 0, s: -2 },
{ q: 2, r: 1, s: -3 },
],
);
test("orientedRectangle should work", () => {
expect(GridGenerator.orientedRectangle(3, 3)).toEqual([
{ meta: {}, q: 0, r: -0, s: 0 },
{ meta: {}, q: 0, r: 1, s: -1 },
{ meta: {}, q: 0, r: 2, s: -2 },
{ meta: {}, q: 1, r: -0, s: -1 },
{ meta: {}, q: 1, r: 1, s: -2 },
{ meta: {}, q: 1, r: 2, s: -3 },
{ meta: {}, q: 2, r: -1, s: -1 },
{ meta: {}, q: 2, r: 0, s: -2 },
{ meta: {}, q: 2, r: 1, s: -3 }
]);
});
Loading

0 comments on commit ba7a90a

Please sign in to comment.