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

Commit 79678a1

Browse files
committed
Native
1 parent 4376556 commit 79678a1

File tree

11 files changed

+789
-9
lines changed

11 files changed

+789
-9
lines changed

demo/game/character.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,8 @@ export default class Character extends Component {
201201

202202
return (
203203
<div style={this.getWrapperStyles()}>
204-
<Body args={[
205-
x, 384, 64, 64, { inertia: Infinity, restitution: 0, friction: 1, frictionStatic: 0 }]
206-
}
204+
<Body
205+
args={[x, 384, 64, 64]}
207206
ref={(b) => { this.body = b; }}
208207
>
209208
<Sprite

src/components/body.js

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,70 @@
11
import { Component, PropTypes } from 'react';
22

3-
import { World, Bodies } from 'matter-js';
3+
import Matter, { World, Bodies } from 'matter-js';
44

55
export default class Body extends Component {
66

77
static propTypes = {
8+
angle: PropTypes.number,
9+
area: PropTypes.string,
810
args: PropTypes.array,
11+
axes: PropTypes.shape({
12+
x: PropTypes.number,
13+
y: PropTypes.number,
14+
}),
15+
bounds: PropTypes.shape({
16+
min: PropTypes.shape({
17+
x: PropTypes.number,
18+
y: PropTypes.number,
19+
}),
20+
max: PropTypes.shape({
21+
x: PropTypes.number,
22+
y: PropTypes.number,
23+
}),
24+
}),
925
children: PropTypes.any,
26+
collisionFilter: PropTypes.shape({
27+
category: PropTypes.number,
28+
group: PropTypes.number,
29+
mask: PropTypes.number,
30+
}),
31+
density: PropTypes.number,
32+
force: PropTypes.shape({
33+
x: PropTypes.number,
34+
y: PropTypes.number,
35+
}),
36+
friction: PropTypes.number,
37+
frictionAir: PropTypes.number,
38+
frictionStatic: PropTypes.number,
39+
id: PropTypes.number,
40+
inertia: PropTypes.number,
41+
inverseInertia: PropTypes.number,
42+
inverseMass: PropTypes.number,
43+
isSensor: PropTypes.bool,
44+
isSleeping: PropTypes.bool,
45+
isStatic: PropTypes.bool,
46+
label: PropTypes.string,
47+
mass: PropTypes.number,
48+
position: PropTypes.shape({
49+
x: PropTypes.number,
50+
y: PropTypes.number,
51+
}),
52+
restitution: PropTypes.number,
1053
shape: PropTypes.string,
54+
sleepThreshold: PropTypes.number,
55+
slop: PropTypes.number,
56+
slope: PropTypes.number,
57+
timeScale: PropTypes.number,
58+
torque: PropTypes.number,
59+
vertices: PropTypes.array,
1160
};
1261

1362
static defaultProps = {
1463
args: [0, 0, 100, 100],
64+
inertia: Infinity,
65+
restitution: 0,
66+
friction: 1,
67+
frictionStatic: 0,
1568
shape: 'rectangle',
1669
};
1770

@@ -26,10 +79,22 @@ export default class Body extends Component {
2679
constructor(props, context) {
2780
super(props);
2881

29-
this.body = Bodies[props.shape](...props.args);
82+
const { args, children, shape, ...options } = props;
83+
84+
this.body = Bodies[shape](...args, options);
3085
World.addBody(context.engine.world, this.body);
3186
}
3287

88+
componentWillReceiveProps(nextProps) {
89+
const { args, children, shape, ...options } = nextProps;
90+
91+
Object.keys(options).forEach((option) => {
92+
if (option in this.body && this.props[option] !== nextProps[option]) {
93+
Matter.Body.set(this.body, option, options[option]);
94+
}
95+
});
96+
}
97+
3398
componentWillUnmount() {
3499
World.remove(this.context.engine.world, this.body);
35100
}

src/components/world.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import React, { Component, PropTypes } from 'react';
22

3-
import { Engine, Events } from 'matter-js';
3+
import Matter, { Engine, Events } from 'matter-js';
44

55
export default class World extends Component {
66

77
static propTypes = {
88
children: PropTypes.any,
9-
gravity: PropTypes.array,
9+
gravity: PropTypes.shape({
10+
x: PropTypes.number,
11+
y: PropTypes.number,
12+
scale: PropTypes.number,
13+
}),
1014
onCollision: PropTypes.func,
1115
onInit: PropTypes.func,
1216
onUpdate: PropTypes.func,
1317
};
1418

1519
static defaultProps = {
16-
gravity: [0, -25],
20+
gravity: {
21+
x: 0,
22+
y: 1,
23+
scale: 0.001,
24+
},
1725
onCollision: () => {},
1826
onInit: () => {},
1927
onUpdate: () => {},
@@ -40,7 +48,19 @@ export default class World extends Component {
4048
this.loopID = null;
4149
this.lastTime = null;
4250

43-
this.engine = Engine.create();
51+
const world = Matter.World.create({ gravity: props.gravity });
52+
53+
this.engine = Engine.create({
54+
world,
55+
});
56+
}
57+
58+
componentWillReceiveProps(nextProps) {
59+
const { gravity } = nextProps;
60+
61+
if (gravity !== this.props.gravity) {
62+
this.engine.world.gravity = gravity;
63+
}
4464
}
4565

4666
componentDidMount() {

src/native/components/body.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { Component, PropTypes } from 'react';
2+
3+
import Matter, { World, Bodies } from 'matter-js';
4+
5+
export default class Body extends Component {
6+
7+
static propTypes = {
8+
angle: PropTypes.number,
9+
area: PropTypes.string,
10+
args: PropTypes.array,
11+
axes: PropTypes.shape({
12+
x: PropTypes.number,
13+
y: PropTypes.number,
14+
}),
15+
bounds: PropTypes.shape({
16+
min: PropTypes.shape({
17+
x: PropTypes.number,
18+
y: PropTypes.number,
19+
}),
20+
max: PropTypes.shape({
21+
x: PropTypes.number,
22+
y: PropTypes.number,
23+
}),
24+
}),
25+
children: PropTypes.any,
26+
collisionFilter: PropTypes.shape({
27+
category: PropTypes.number,
28+
group: PropTypes.number,
29+
mask: PropTypes.number,
30+
}),
31+
density: PropTypes.number,
32+
force: PropTypes.shape({
33+
x: PropTypes.number,
34+
y: PropTypes.number,
35+
}),
36+
friction: PropTypes.number,
37+
frictionAir: PropTypes.number,
38+
frictionStatic: PropTypes.number,
39+
id: PropTypes.number,
40+
inertia: PropTypes.number,
41+
inverseInertia: PropTypes.number,
42+
inverseMass: PropTypes.number,
43+
isSensor: PropTypes.bool,
44+
isSleeping: PropTypes.bool,
45+
isStatic: PropTypes.bool,
46+
label: PropTypes.string,
47+
mass: PropTypes.number,
48+
position: PropTypes.shape({
49+
x: PropTypes.number,
50+
y: PropTypes.number,
51+
}),
52+
restitution: PropTypes.number,
53+
shape: PropTypes.string,
54+
sleepThreshold: PropTypes.number,
55+
slop: PropTypes.number,
56+
slope: PropTypes.number,
57+
timeScale: PropTypes.number,
58+
torque: PropTypes.number,
59+
vertices: PropTypes.array,
60+
};
61+
62+
static defaultProps = {
63+
args: [0, 0, 100, 100],
64+
inertia: Infinity,
65+
restitution: 0,
66+
friction: 1,
67+
frictionStatic: 0,
68+
shape: 'rectangle',
69+
};
70+
71+
static contextTypes = {
72+
engine: PropTypes.object,
73+
};
74+
75+
static childContextTypes = {
76+
body: PropTypes.object,
77+
};
78+
79+
constructor(props, context) {
80+
super(props);
81+
82+
const { args, children, shape, ...options } = props;
83+
84+
this.body = Bodies[shape](...args, options);
85+
World.addBody(context.engine.world, this.body);
86+
}
87+
88+
componentWillReceiveProps(nextProps) {
89+
const { args, children, shape, ...options } = nextProps;
90+
91+
Object.keys(options).forEach((option) => {
92+
if (option in this.body && this.props[option] !== nextProps[option]) {
93+
Matter.Body.set(this.body, option, options[option]);
94+
}
95+
});
96+
}
97+
98+
componentWillUnmount() {
99+
World.remove(this.context.engine.world, this.body);
100+
}
101+
102+
getChildContext() {
103+
return {
104+
body: this.body,
105+
};
106+
}
107+
108+
render() {
109+
return this.props.children;
110+
}
111+
112+
}

src/native/components/loop.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React, { Component, PropTypes } from 'react';
2+
3+
import {
4+
View,
5+
} from 'react-native';
6+
7+
import GameLoop from '../utils/game-loop';
8+
9+
export default class Loop extends Component {
10+
11+
static propTypes = {
12+
children: PropTypes.any,
13+
style: PropTypes.object,
14+
};
15+
16+
static childContextTypes = {
17+
loop: PropTypes.object,
18+
};
19+
20+
constructor(props) {
21+
super(props);
22+
23+
this.loop = new GameLoop();
24+
}
25+
26+
componentDidMount() {
27+
this.loop.start();
28+
}
29+
30+
componentWillUnmount() {
31+
this.loop.stop();
32+
}
33+
34+
getChildContext() {
35+
return {
36+
loop: this.loop,
37+
};
38+
}
39+
40+
render() {
41+
const defaultStyles = {
42+
flex: 1
43+
};
44+
const styles = { ...defaultStyles, ...this.props.style };
45+
return (
46+
<View style={styles}>
47+
{this.props.children}
48+
</View>
49+
);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)