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

Commit e205b84

Browse files
authored
Merge branch 'master' into debug-render
2 parents 55f2e52 + e0c5893 commit e205b84

File tree

17 files changed

+81
-41
lines changed

17 files changed

+81
-41
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ node_modules
22
npm-debug.log
33
lib
44
umd
5-
dist
5+
dist
6+
7+
.idea

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"mobx": "^2.5.0",
4747
"mobx-react": "^3.5.5",
4848
"postcss-loader": "^0.10.1",
49+
"prop-types": "^15.5.10",
4950
"raw-loader": "^0.5.1",
5051
"react": "^15.2.1",
5152
"react-dom": "^15.2.1",

src/components/body.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, PropTypes } from 'react';
1+
import { Component } from 'react';
2+
import PropTypes from 'prop-types';
23

34
import Matter, { World, Bodies } from 'matter-js';
45

src/components/loop.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { Component, PropTypes } from 'react';
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
23

34
import GameLoop from '../utils/game-loop';
45

src/components/sprite.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { Component, PropTypes } from 'react';
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
23

34
export default class Sprite extends Component {
45

src/components/stage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { Component, PropTypes } from 'react';
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
23

34
export default class Stage extends Component {
45

src/components/tile-map.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { Component, PropTypes } from 'react';
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
23

34

45
export default class TileMap extends Component {
@@ -12,6 +13,8 @@ export default class TileMap extends Component {
1213
src: PropTypes.string,
1314
style: PropTypes.object,
1415
tileSize: PropTypes.number,
16+
width: PropTypes.number,
17+
height: PropTypes.number
1518
};
1619

1720
static defaultProps = {
@@ -85,17 +88,26 @@ export default class TileMap extends Component {
8588

8689
getImageStyles(imageIndex) {
8790
const { scale } = this.context;
88-
const { tileSize } = this.props;
91+
const { tileSize, width, height } = this.props;
8992

9093
const size = Math.round(scale * tileSize);
91-
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+
}
92105

93106
return {
94107
position: 'absolute',
95108
imageRendering: 'pixelated',
96109
display: 'block',
97-
height: '100%',
98-
transform: `translate(-${left}px, 0px)`,
110+
transform: `translate(-${left}px, -${top}px)`,
99111
};
100112
}
101113

src/components/world.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { Component, PropTypes } from 'react';
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
23

34
import Matter, { Render, Engine, Events } from 'matter-js';
45

56
export default class World extends Component {
6-
77
static propTypes = {
88
children: PropTypes.any,
99
gravity: PropTypes.shape({
@@ -46,8 +46,12 @@ export default class World extends Component {
4646
};
4747

4848
loop = () => {
49-
const currTime = 0.001 * Date.now();
50-
Engine.update(this.engine, 1000 / 60, this.lastTime ? currTime / this.lastTime : 1);
49+
const currTime = 0.001 * performance.now();
50+
Engine.update(
51+
this.engine,
52+
1000 / 60,
53+
this.lastTime ? currTime / this.lastTime : 1,
54+
);
5155
this.lastTime = currTime;
5256
};
5357

@@ -145,15 +149,15 @@ export default class World extends Component {
145149
this.loopID = null;
146150
this.lastTime = null;
147151

148-
const world = Matter.World.create({ gravity: props.gravity });
152+
const world = Matter.World.create({gravity: props.gravity});
149153

150154
this.engine = Engine.create({
151155
world,
152156
});
153157
}
154158

155159
componentWillReceiveProps(nextProps) {
156-
const { gravity } = nextProps;
160+
const {gravity} = nextProps;
157161

158162
if (gravity !== this.props.gravity) {
159163
this.engine.world.gravity = gravity;
@@ -243,5 +247,4 @@ export default class World extends Component {
243247
</div>
244248
);
245249
}
246-
247250
}

src/native/components/body.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, PropTypes } from 'react';
1+
import { Component } from 'react';
2+
import PropTypes from 'prop-types';
23

34
import Matter, { World, Bodies } from 'matter-js';
45

0 commit comments

Comments
 (0)