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

Commit 876566b

Browse files
authored
Merge pull request #40 from FormidableLabs/revert-23-debug-render
Revert "Debug render"
2 parents a4434c5 + b74ebbe commit 876566b

File tree

4 files changed

+5
-198
lines changed

4 files changed

+5
-198
lines changed

demo/game/index.js

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import Fade from './fade';
1515

1616
import GameStore from './stores/game-store';
1717

18-
const KEY_D = 68;
19-
const KEY_A = 65;
20-
2118
export default class Game extends Component {
2219

2320
static propTypes = {
@@ -52,12 +49,6 @@ export default class Game extends Component {
5249
Matter.World.addBody(engine.world, ground);
5350
Matter.World.addBody(engine.world, leftWall);
5451
Matter.World.addBody(engine.world, rightWall);
55-
56-
Matter.Events.on(engine, 'afterUpdate', this.update);
57-
58-
const unsubscribeFromUpdate = () => {
59-
Matter.Events.off(engine, 'afterUpdate', this.update);
60-
}
6152
}
6253

6354
handleEnterBuilding = (index) => {
@@ -69,27 +60,12 @@ export default class Game extends Component {
6960
}, 500);
7061
}
7162

72-
update = () => {
73-
74-
// On first press of "d", enable debug mode
75-
if (this.keyListener.isDown(KEY_D)) {
76-
if (!this.previousDown) {
77-
this.previousDown = true;
78-
this.setState({debug: !this.state.debug});
79-
}
80-
} else {
81-
this.previousDown = false;
82-
}
83-
}
84-
8563
constructor(props) {
8664
super(props);
8765

8866
this.state = {
8967
fade: true,
90-
debug: false,
9168
};
92-
9369
this.keyListener = new KeyListener();
9470
window.AudioContext = window.AudioContext || window.webkitAudioContext;
9571
window.context = window.context || new AudioContext();
@@ -104,25 +80,18 @@ export default class Game extends Component {
10480
fade: false,
10581
});
10682

107-
this.stageXUIUnsubscribe = GameStore.onStageXChange((stageX) => {
108-
this.forceUpdate();
109-
});
110-
11183
this.keyListener.subscribe([
11284
this.keyListener.LEFT,
11385
this.keyListener.RIGHT,
11486
this.keyListener.UP,
11587
this.keyListener.SPACE,
116-
KEY_A,
117-
KEY_D,
88+
65,
11889
]);
11990
}
12091

12192
componentWillUnmount() {
12293
this.stopMusic();
123-
this.unsubscribeFromUpdate();
12494
this.keyListener.unsubscribe();
125-
this.stageXUIUnsubscribe();
12695
}
12796

12897
render() {
@@ -131,13 +100,6 @@ export default class Game extends Component {
131100
<Stage style={{ background: '#3a9bdc' }}>
132101
<World
133102
onInit={this.physicsInit}
134-
debug={this.state.debug && {
135-
offset: {
136-
x: -GameStore.stageX,
137-
y: 0,
138-
},
139-
background: 'rgba(0, 0, 0, 0.5)',
140-
}}
141103
>
142104
<Level
143105
store={GameStore}

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: 2 additions & 148 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,94 +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-
96-
if (!this._debugRenderElement) {
97-
return;
98-
}
99-
100-
const { renderWidth, renderHeight, scale } = this.context;
101-
const { offset, background } = this.getDebugProps();
102-
103-
const width = renderWidth / scale;
104-
const height = renderHeight / scale;
105-
106-
this._render = Render.create({
107-
canvas: this._debugRenderElement,
108-
// Auto-zoom the canvas to the correct game area
109-
bounds: {
110-
min: {
111-
x: offset.x,
112-
y: offset.y,
113-
},
114-
max: {
115-
x: offset.x + width,
116-
y: offset.y + height,
117-
},
118-
},
119-
options: {
120-
wireframeBackground: background,
121-
width: renderWidth,
122-
height: renderHeight,
123-
},
124-
});
125-
126-
// Setting this as part of `.create` crashes Chrome during a deep clone. :/
127-
// My guess: a circular reference
128-
this._render.engine = this.engine;
129-
130-
Render.run(this._render);
131-
};
132-
133-
getCanvasRef = (element) => {
134-
135-
this._debugRenderElement = element;
136-
137-
if (element) {
138-
if (!this._render) {
139-
this.setupDebugRenderer();
140-
}
141-
} else {
142-
this.stopDebugRendering();
143-
}
144-
};
145-
14649
constructor(props) {
14750
super(props);
14851

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

19870
componentDidMount() {
19971
this.loopID = this.context.loop.subscribe(this.loop);
200-
this.onInit(this.engine);
72+
this.props.onInit(this.engine);
20173
Events.on(this.engine, 'afterUpdate', this.props.onUpdate);
20274
Events.on(this.engine, 'collisionStart', this.props.onCollision);
20375
}
@@ -206,7 +78,6 @@ export default class World extends Component {
20678
this.context.loop.unsubscribe(this.loopID);
20779
Events.off(this.engine, 'afterUpdate', this.props.onUpdate);
20880
Events.off(this.engine, 'collisionStart', this.props.onCollision);
209-
this.stopDebugRendering();
21081
}
21182

21283
getChildContext() {
@@ -224,26 +95,9 @@ export default class World extends Component {
22495
width: '100%',
22596
};
22697

227-
const { renderWidth, renderHeight, scale } = this.context;
228-
229-
let debugRenderTarget = false;
230-
231-
if (this.props.debug) {
232-
debugRenderTarget = (
233-
<canvas
234-
key="debug-render-target"
235-
style={{position: 'relative'}}
236-
width={renderWidth}
237-
height={renderHeight}
238-
ref={this.getCanvasRef}
239-
/>
240-
);
241-
}
242-
24398
return (
24499
<div style={defaultStyles}>
245100
{this.props.children}
246-
{debugRenderTarget}
247101
</div>
248102
);
249103
}

0 commit comments

Comments
 (0)