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

Commit cffd8f0

Browse files
committed
Tilemap grid support
1 parent 9dbaba1 commit cffd8f0

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default class TileMap extends Component {
1212
src: PropTypes.string,
1313
style: PropTypes.object,
1414
tileSize: PropTypes.number,
15+
width: PropTypes.number,
16+
height: PropTypes.number
1517
};
1618

1719
static defaultProps = {
@@ -85,16 +87,26 @@ export default class TileMap extends Component {
8587

8688
getImageStyles(imageIndex) {
8789
const { scale } = this.context;
88-
const { tileSize } = this.props;
90+
const { tileSize, width, height } = this.props;
8991

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

93105
return {
94106
position: 'absolute',
95107
imageRendering: 'pixelated',
96108
display: 'block',
97-
transform: `translate(-${left}px, 0px)`,
109+
transform: `translate(-${left}px, -${top}px)`,
98110
};
99111
}
100112

0 commit comments

Comments
 (0)