MCPcopy
hub / github.com/bberak/react-native-game-engine / GameLoop

Class GameLoop

src/GameLoop.js:6–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import DefaultTouchProcessor from "./DefaultTouchProcessor";
5
6export default class GameLoop extends Component {
7 constructor(props) {
8 super(props);
9 this.timer = props.timer || new DefaultTimer();
10 this.timer.subscribe(this.updateHandler);
11 this.touches = [];
12 this.screen = Dimensions.get("window");
13 this.previousTime = null;
14 this.previousDelta = null;
15 this.touchProcessor = props.touchProcessor(this.touches);
16 this.layout = null;
17 }
18
19 componentDidMount() {
20 if (this.props.running) this.start();
21 }
22
23 componentWillUnmount() {
24 this.stop();
25 this.timer.unsubscribe(this.updateHandler);
26 if (this.touchProcessor.end) this.touchProcessor.end();
27 }
28
29 UNSAFE_componentWillReceiveProps(nextProps) {
30 if (nextProps.running !== this.props.running) {
31 if (nextProps.running) this.start();
32 else this.stop();
33 }
34 }
35
36 start = () => {
37 this.touches.length = 0;
38 this.previousTime = null;
39 this.previousDelta = null;
40 this.timer.start();
41 };
42
43 stop = () => {
44 this.timer.stop();
45 };
46
47 updateHandler = currentTime => {
48 let args = {
49 touches: this.touches,
50 screen: this.screen,
51 layout: this.layout,
52 time: {
53 current: currentTime,
54 previous: this.previousTime,
55 delta: currentTime - (this.previousTime || currentTime),
56 previousDelta: this.previousDelta
57 }
58 };
59
60 if (this.props.onUpdate) this.props.onUpdate(args);
61
62 this.touches.length = 0;
63 this.previousTime = currentTime;

Callers

nothing calls this directly

Calls 2

startMethod · 0.80
stopMethod · 0.80

Tested by

no test coverage detected