Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 544a0c7

Browse files
authored
Merge pull request #35 from ssg-luke/tilemap-grid
Tilemap grid support
2 parents 7886e58 + cffd8f0 commit 544a0c7

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Once this general structure is established, what follows usually depends on what
9393
Using this library with React Native is a simple as importing from the native directory:
9494

9595
```js
96-
import { Loop, Stage, ...etc } from 'react-game-kit/native';
96+
import { Loop, Stage, ...etc } from 'react-game-kit/native';
9797
```
9898

9999
> Note: AudioPlayer and KeyListener are not implemented on the React Native version.
@@ -111,15 +111,15 @@ class ChildComponent extends React.Component {
111111
static contextTypes = {
112112
loop: PropTypes.object,
113113
};
114-
114+
115115
update = () => {
116116
// tick logic
117117
};
118-
118+
119119
componentDidMount() {
120120
this.context.loop.subscribe(this.update);
121121
}
122-
122+
123123
componentWillUnmount() {
124124
this.context.loop.unsubscribe(this.update);
125125
}
@@ -135,7 +135,7 @@ class ChildComponent extends React.Component {
135135

136136
**width** (_number_) : Base game width. Defaults to `1024`.
137137

138-
The `Stage` component also leverages `context` much like `Loop`, except it passes game scale as `this.context.scale`. You can use this value to appropriately scale positioning and dimension values within your game. Again, you would have to specify `scale: PropTypes.number` in your component's `contextTypes` to receive this value.
138+
The `Stage` component also leverages `context` much like `Loop`, except it passes game scale as `this.context.scale`. You can use this value to appropriately scale positioning and dimension values within your game. Again, you would have to specify `scale: PropTypes.number` in your component's `contextTypes` to receive this value.
139139

140140
--
141141

@@ -218,6 +218,10 @@ The `Sprite` component lets you define sprite animations using sprite sheets. Wh
218218

219219
**tileSize** (number) : Tilemap tile size.
220220

221+
**width** (number) : Tilemap width.
222+
223+
**height** (number) : Tilemap height.
224+
221225
The `TileMap` component lets you define tile maps from a tile atlas. Your tilemap is made of up rows and columns. Each layer is then drawn using those numbers as reference. So for example, if you had 4 rows and 4 columns, with 1 layer, your `layers` prop would look like:
222226

223227
```js

src/components/tile-map.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export default class TileMap extends Component {
1313
src: PropTypes.string,
1414
style: PropTypes.object,
1515
tileSize: PropTypes.number,
16+
width: PropTypes.number,
17+
height: PropTypes.number
1618
};
1719

1820
static defaultProps = {
@@ -86,17 +88,26 @@ export default class TileMap extends Component {
8688

8789
getImageStyles(imageIndex) {
8890
const { scale } = this.context;
89-
const { tileSize } = this.props;
91+
const { tileSize, width, height } = this.props;
9092

9193
const size = Math.round(scale * tileSize);
92-
const left = (imageIndex - 1) * size;
94+
95+
var left, top;
96+
if (!width || !height) {
97+
left = (imageIndex - 1) * size;
98+
top = 0;
99+
} else {
100+
const cols = width / tileSize;
101+
const rows = height / tileSize;
102+
left = ((imageIndex - 1) % cols) * Math.round((scale * width) / cols);
103+
top = Math.floor((imageIndex - 1) / cols) * Math.round((scale * height) / rows);
104+
}
93105

94106
return {
95107
position: 'absolute',
96108
imageRendering: 'pixelated',
97109
display: 'block',
98-
height: '100%',
99-
transform: `translate(-${left}px, 0px)`,
110+
transform: `translate(-${left}px, -${top}px)`,
100111
};
101112
}
102113

0 commit comments

Comments
 (0)