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

Commit a0cfef6

Browse files
committed
revert
2 parents 1b22f00 + 876566b commit a0cfef6

File tree

4 files changed

+7
-206
lines changed

4 files changed

+7
-206
lines changed

demo/game/index.js

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import Fade from './fade';
1010

1111
import GameStore from './stores/game-store';
1212

13-
const KEY_D = 68;
14-
const KEY_A = 65;
15-
1613
export default class Game extends Component {
1714
static propTypes = {
1815
onLeave: PropTypes.func,
@@ -34,12 +31,6 @@ export default class Game extends Component {
3431
Matter.World.addBody(engine.world, ground);
3532
Matter.World.addBody(engine.world, leftWall);
3633
Matter.World.addBody(engine.world, rightWall);
37-
38-
Matter.Events.on(engine, 'afterUpdate', this.update);
39-
40-
this.unsubscribeFromUpdate = () => {
41-
Matter.Events.off(engine, 'afterUpdate', this.update);
42-
};
4334
};
4435

4536
handleEnterBuilding = index => {
@@ -51,26 +42,12 @@ export default class Game extends Component {
5142
}, 500);
5243
};
5344

54-
update = () => {
55-
// On first press of "d", enable debug mode
56-
if (this.keyListener.isDown(KEY_D)) {
57-
if (!this.previousDown) {
58-
this.previousDown = true;
59-
this.setState({ debug: !this.state.debug });
60-
}
61-
} else {
62-
this.previousDown = false;
63-
}
64-
};
65-
6645
constructor(props) {
6746
super(props);
6847

6948
this.state = {
7049
fade: true,
71-
debug: false,
7250
};
73-
7451
this.keyListener = new KeyListener();
7552
window.AudioContext = window.AudioContext || window.webkitAudioContext;
7653
window.context = window.context || new AudioContext();
@@ -89,45 +66,25 @@ export default class Game extends Component {
8966
fade: false,
9067
});
9168

92-
this.stageXUIUnsubscribe = GameStore.onStageXChange(stageX => {
93-
this.forceUpdate();
94-
});
95-
9669
this.keyListener.subscribe([
9770
this.keyListener.LEFT,
9871
this.keyListener.RIGHT,
9972
this.keyListener.UP,
10073
this.keyListener.SPACE,
101-
KEY_A,
102-
KEY_D,
74+
65,
10375
]);
10476
}
10577

10678
componentWillUnmount() {
10779
this.stopMusic();
108-
this.unsubscribeFromUpdate();
10980
this.keyListener.unsubscribe();
110-
this.stageXUIUnsubscribe();
11181
}
11282

11383
render() {
11484
return (
11585
<Loop>
11686
<Stage style={{ background: '#3a9bdc' }}>
117-
<World
118-
onInit={this.physicsInit}
119-
debug={
120-
this.state.debug ? (
121-
{
122-
offset: {
123-
x: -GameStore.stageX,
124-
y: 0,
125-
},
126-
background: 'rgba(0, 0, 0, 0.5)',
127-
}
128-
) : null
129-
}
130-
>
87+
<World onInit={this.physicsInit}>
13188
<Level store={GameStore} />
13289
<Character
13390
onEnterBuilding={this.handleEnterBuilding}

demo/game/stores/game-store.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { observable, autorun } from 'mobx';
1+
import { observable } from 'mobx';
22

33
class GameStore {
44
@observable characterPosition = { x: 0, y: 0 };
@@ -9,10 +9,6 @@ class GameStore {
99
this.characterPosition = position;
1010
}
1111

12-
onStageXChange(callback) {
13-
autorun(() => callback(this.stageX));
14-
}
15-
1612
setStageX(x) {
1713
if (x > 0) {
1814
this.stageX = 0;

src/components/stage.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export default class Stage extends Component {
2222
static childContextTypes = {
2323
loop: PropTypes.object,
2424
scale: PropTypes.number,
25-
renderWidth: PropTypes.number,
26-
renderHeight: PropTypes.number,
2725
};
2826

2927
setDimensions = () => {
@@ -55,11 +53,8 @@ export default class Stage extends Component {
5553
}
5654

5755
getChildContext() {
58-
const { scale, width, height } = this.getScale();
5956
return {
60-
scale: scale,
61-
renderWidth: width,
62-
renderHeight: height,
57+
scale: this.getScale().scale,
6358
loop: this.context.loop,
6459
};
6560
}

src/components/world.js

Lines changed: 3 additions & 150 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

4-
import Matter, { Render, Engine, Events } from 'matter-js';
4+
import Matter, { Engine, Events } from 'matter-js';
55

66
export default class World extends Component {
77
static propTypes = {
@@ -11,13 +11,6 @@ export default class World extends Component {
1111
y: PropTypes.number,
1212
scale: PropTypes.number,
1313
}),
14-
debug: PropTypes.shape({
15-
offset: PropTypes.shape({
16-
x: PropTypes.number,
17-
y: PropTypes.number,
18-
}),
19-
background: PropTypes.string,
20-
}),
2114
onCollision: PropTypes.func,
2215
onInit: PropTypes.func,
2316
onUpdate: PropTypes.func,
@@ -36,8 +29,6 @@ export default class World extends Component {
3629

3730
static contextTypes = {
3831
scale: PropTypes.number,
39-
renderWidth: PropTypes.number,
40-
renderHeight: PropTypes.number,
4132
loop: PropTypes.object,
4233
};
4334

@@ -55,92 +46,6 @@ export default class World extends Component {
5546
this.lastTime = currTime;
5647
};
5748

58-
onInit = (...args) => {
59-
if (this.props.debug) {
60-
this.setupDebugRenderer();
61-
}
62-
63-
this.props.onInit(...args);
64-
};
65-
66-
stopDebugRendering = () => {
67-
if (this._render) {
68-
Render.stop(this._render);
69-
delete this._render;
70-
}
71-
};
72-
73-
getDebugProps = () => {
74-
const debugProps = Object.assign(
75-
{
76-
offset: {},
77-
// transparent background to see sprites, etc, in the world
78-
background: 'rgba(0, 0, 0, 0)',
79-
},
80-
this.props.debug || {}
81-
);
82-
83-
debugProps.offset = Object.assign(
84-
{
85-
x: 0,
86-
y: 0,
87-
},
88-
debugProps.offset
89-
);
90-
91-
return debugProps;
92-
};
93-
94-
setupDebugRenderer = () => {
95-
if (!this._debugRenderElement) {
96-
return;
97-
}
98-
99-
const { renderWidth, renderHeight, scale } = this.context;
100-
const { offset, background } = this.getDebugProps();
101-
102-
const width = renderWidth / scale;
103-
const height = renderHeight / scale;
104-
105-
this._render = Render.create({
106-
canvas: this._debugRenderElement,
107-
// Auto-zoom the canvas to the correct game area
108-
bounds: {
109-
min: {
110-
x: offset.x,
111-
y: offset.y,
112-
},
113-
max: {
114-
x: offset.x + width,
115-
y: offset.y + height,
116-
},
117-
},
118-
options: {
119-
wireframeBackground: background,
120-
width: renderWidth,
121-
height: renderHeight,
122-
},
123-
});
124-
125-
// Setting this as part of `.create` crashes Chrome during a deep clone. :/
126-
// My guess: a circular reference
127-
this._render.engine = this.engine;
128-
129-
Render.run(this._render);
130-
};
131-
132-
getCanvasRef = element => {
133-
this._debugRenderElement = element;
134-
135-
if (element) {
136-
if (!this._render) {
137-
this.setupDebugRenderer();
138-
}
139-
} else {
140-
this.stopDebugRendering();
141-
}
142-
};
143-
14449
constructor(props) {
14550
super(props);
14651

@@ -160,41 +65,11 @@ export default class World extends Component {
16065
if (gravity !== this.props.gravity) {
16166
this.engine.world.gravity = gravity;
16267
}
163-
164-
if (!nextProps.debug) {
165-
this.stopDebugRendering();
166-
}
167-
}
168-
169-
componentDidUpdate() {
170-
if (this.props.debug && this._render) {
171-
const { renderWidth, renderHeight, scale } = this.context;
172-
173-
const { offset } = this.getDebugProps();
174-
175-
// When context changes (eg; `scale` due to a window resize),
176-
// re-calculate the world stage
177-
this._render.options.width = renderWidth;
178-
this._render.options.height = renderHeight;
179-
180-
this._render.bounds = {
181-
min: {
182-
x: offset.x,
183-
y: offset.y,
184-
},
185-
max: {
186-
x: offset.x + renderWidth / scale,
187-
y: offset.y + renderHeight / scale,
188-
},
189-
};
190-
191-
Render.world(this._render);
192-
}
19368
}
19469

19570
componentDidMount() {
19671
this.loopID = this.context.loop.subscribe(this.loop);
197-
this.onInit(this.engine);
72+
this.props.onInit(this.engine);
19873
Events.on(this.engine, 'afterUpdate', this.props.onUpdate);
19974
Events.on(this.engine, 'collisionStart', this.props.onCollision);
20075
}
@@ -203,7 +78,6 @@ export default class World extends Component {
20378
this.context.loop.unsubscribe(this.loopID);
20479
Events.off(this.engine, 'afterUpdate', this.props.onUpdate);
20580
Events.off(this.engine, 'collisionStart', this.props.onCollision);
206-
this.stopDebugRendering();
20781
}
20882

20983
getChildContext() {
@@ -221,27 +95,6 @@ export default class World extends Component {
22195
width: '100%',
22296
};
22397

224-
const { renderWidth, renderHeight, scale } = this.context;
225-
226-
let debugRenderTarget = false;
227-
228-
if (this.props.debug) {
229-
debugRenderTarget = (
230-
<canvas
231-
key="debug-render-target"
232-
style={{ position: 'relative' }}
233-
width={renderWidth}
234-
height={renderHeight}
235-
ref={this.getCanvasRef}
236-
/>
237-
);
238-
}
239-
240-
return (
241-
<div style={defaultStyles}>
242-
{this.props.children}
243-
{debugRenderTarget}
244-
</div>
245-
);
98+
return <div style={defaultStyles}>{this.props.children}</div>;
24699
}
247100
}

0 commit comments

Comments
 (0)