Skip to content

Commit 1a467a2

Browse files
authored
Merge pull request #2 from IcculusC/hex-engine
v1.0.0
2 parents 4dd5160 + 472871d commit 1a467a2

19 files changed

+342
-834
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ Quote from the original readme
1313
> With inspiration from
1414
[http://www.redblobgames.com/grids/hexagons](http://www.redblobgames.com/grids/hexagons).
1515

16+
# Documentation
17+
18+
Documentation forthcoming alongside stable APIs.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-hex-engine",
33
"description": "Hexagon Map Editor and Game Engine",
4-
"version": "0.2.4",
4+
"version": "1.0.0",
55
"main": "lib/index.js",
66
"author": "IcculusC",
77
"repository": "IcculusC/react-hex-engine",

src/Context.js

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,20 @@
11
import React from "react";
2-
import Orientation from "./models/Orientation";
32

4-
export const LayoutContext = React.createContext({
5-
layout: Orientation.Flat,
6-
points: ""
3+
export const HexEngineContext = React.createContext({
4+
layout: null,
5+
points: null,
6+
viewBox: null
77
});
8-
export const {
9-
Provider: LayoutProvider,
10-
Consumer: LayoutConsumer
11-
} = LayoutContext;
12-
13-
export function withLayout(Component) {
14-
return props => (
15-
<LayoutConsumer>
16-
{layout => <Component layout={layout} {...props} />}
17-
</LayoutConsumer>
18-
);
19-
}
208

21-
export function withExpandedLayout(Component) {
22-
return props => (
23-
<LayoutConsumer>
24-
{({ layout, points }) => (
25-
<Component layout={layout} points={points} {...props} />
26-
)}
27-
</LayoutConsumer>
28-
);
29-
}
30-
31-
export const ViewBoxContext = React.createContext({
32-
x: -50,
33-
y: -50,
34-
width: 100,
35-
height: 100
36-
});
379
export const {
38-
Provider: ViewBoxProvider,
39-
Consumer: ViewBoxConsumer
40-
} = ViewBoxContext;
10+
Consumer: HexEngineConsumer,
11+
Provider: HexEngineProvider
12+
} = HexEngineContext;
4113

42-
export function withViewBox(Component) {
14+
export function withHexEngine(Component) {
4315
return props => (
44-
<ViewBoxConsumer>
45-
{viewBox => <Component viewBox={viewBox} {...props} />}
46-
</ViewBoxConsumer>
16+
<HexEngineConsumer>
17+
{engine => <Component {...engine} {...props} />}
18+
</HexEngineConsumer>
4719
);
4820
}

src/HexEngine.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import React, { Component } from "react";
2+
import PropTypes from "prop-types";
3+
import classNames from "classnames";
4+
import { HexEngineProvider } from "./Context";
5+
import Point from "./models/Point";
6+
import Orientation from "./models/Orientation";
7+
8+
class HexEngine extends Component {
9+
static propTypes = {
10+
children: PropTypes.node.isRequired,
11+
classes: PropTypes.objectOf(PropTypes.string),
12+
flat: PropTypes.bool,
13+
height: PropTypes.oneOfType([
14+
PropTypes.string.isRequired,
15+
PropTypes.number.isRequired
16+
]),
17+
origin: PropTypes.oneOfType([
18+
PropTypes.instanceOf(Point),
19+
PropTypes.shape({ x: PropTypes.number, y: PropTypes.number })
20+
]),
21+
size: PropTypes.oneOfType([
22+
PropTypes.instanceOf(Point),
23+
PropTypes.shape({ x: PropTypes.number, y: PropTypes.number })
24+
]),
25+
spacing: PropTypes.number,
26+
width: PropTypes.oneOfType([
27+
PropTypes.string.isRequired,
28+
PropTypes.number.isRequired
29+
]),
30+
viewBox: PropTypes.objectOf(PropTypes.number)
31+
};
32+
33+
static defaultProps = {
34+
classes: { grid: "", layout: "" },
35+
flat: true,
36+
height: 480,
37+
origin: new Point(0, 0),
38+
size: new Point(10, 10),
39+
spacing: 1.0,
40+
width: 640,
41+
viewBox: {
42+
height: 100,
43+
width: 100,
44+
x: -50,
45+
y: -50
46+
}
47+
};
48+
49+
static getPointOffset(corner, orientation, size) {
50+
let angle = (2.0 * Math.PI * (corner + orientation.startAngle)) / 6;
51+
return new Point(size.x * Math.cos(angle), size.y * Math.sin(angle));
52+
}
53+
54+
static calculateCoordinates(orientation, size) {
55+
const center = new Point(0, 0);
56+
return [...Array(6).keys()].map((_, i) => {
57+
const offset = HexEngine.getPointOffset(i, orientation, size);
58+
return new Point(center.x + offset.x, center.y + offset.y);
59+
});
60+
}
61+
62+
render() {
63+
const {
64+
children,
65+
classes,
66+
flat,
67+
height,
68+
size,
69+
viewBox,
70+
width,
71+
...rest
72+
} = this.props;
73+
74+
const orientation = flat ? Orientation.Flat : Orientation.Pointy;
75+
const points = HexEngine.calculateCoordinates(orientation, size)
76+
.map(point => point.toString())
77+
.join(" ");
78+
const layout = { orientation, size, ...rest };
79+
80+
return (
81+
<svg
82+
className={classNames("grid", classes.grid)}
83+
height={height}
84+
version="1.1"
85+
viewBox={`${viewBox.x} ${viewBox.y} ${viewBox.width} ${viewBox.height}`}
86+
width={width}
87+
xmlns="http://www.w3.org/2000/svg"
88+
>
89+
<HexEngineProvider
90+
value={{
91+
layout,
92+
points,
93+
viewBox
94+
}}
95+
>
96+
<g className={classes.layout}>{children}</g>
97+
</HexEngineProvider>
98+
</svg>
99+
);
100+
}
101+
}
102+
103+
export default HexEngine;

src/HexGrid.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Hexagon.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Hex from "./models/Hex";
55
import HexUtils from "./HexUtils";
66
import Point from "./models/Point";
77
import Text from "./Text";
8-
import { withExpandedLayout } from "./Context";
8+
import { withHexEngine } from "./Context";
99

1010
class Hexagon extends Component {
1111
static propTypes = {
@@ -250,4 +250,4 @@ class Hexagon extends Component {
250250
}
251251
}
252252

253-
export default withExpandedLayout(Hexagon);
253+
export default withHexEngine(Hexagon);

src/Layout.js

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/Path.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from "react";
22
import PropTypes from "prop-types";
33
import HexUtils from "./HexUtils";
4-
import { withExpandedLayout } from "./Context";
4+
import { withHexEngine } from "./Context";
55

66
class Path extends Component {
77
static propTypes = {
@@ -42,4 +42,4 @@ class Path extends Component {
4242
}
4343
}
4444

45-
export default withExpandedLayout(Path);
45+
export default withHexEngine(Path);

src/index.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
export {
2-
LayoutConsumer,
3-
LayoutContext,
4-
LayoutProvider,
5-
ViewBoxConsumer,
6-
ViewBoxContext,
7-
ViewBoxProvider,
8-
withExpandedLayout,
9-
withLayout,
10-
withViewBox
2+
HexEngineConsumer,
3+
HexEngineContext,
4+
HexEngineProvider,
5+
withHexEngine
116
} from "./Context";
127
export { default as GridGenerator } from "./GridGenerator";
138
export { default as Hex } from "./models/Hex";
149
export { default as Hexagon } from "./Hexagon";
15-
export { default as HexGrid } from "./HexGrid";
10+
export { default as HexEngine } from "./HexEngine";
1611
export { default as HexUtils } from "./HexUtils";
17-
export { default as Layout } from "./Layout";
1812
export { default as Orientation } from "./models/Orientation";
1913
export { default as Path } from "./Path";
2014
export { default as Pattern } from "./Pattern";

0 commit comments

Comments
 (0)