-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.js
156 lines (124 loc) · 3.22 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
import React, {
AppRegistry,
PanResponder,
View,
} from 'react-native';
import { connect, Provider } from 'react-redux';
import { createStore } from 'redux';
import Styles from './Styles';
import REPL from './REPL';
REPL.registerEval('main', (c) => eval(c));
// Import from a different module for a different game!
import { sceneReduce, Scene } from './Fluxpy';
/**
* Touch
*
* Event handler that dispatches
* `{ ...gestureState, type: 'TOUCH', pressed: <whether pressed> }`
* on touch events, where `gestureState` is given as in
* https://facebook.github.io/react-native/docs/panresponder.html. Doesn't
* actually render anything.
*/
const Touch = connect()(
({ dispatch, children, ...props }) => {
const panGrant = (_, gestureState) =>
dispatch({ ...gestureState, type: 'TOUCH', pressed: true });
const panRelease = (_, gestureState) =>
dispatch({ ...gestureState, type: 'TOUCH', pressed: false });
const panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderGrant: panGrant,
onPanResponderRelease: panRelease,
onPanResponderTerminate: panRelease,
onShouldBlockNativeResponder: () => false,
});
return (
<View
{...props}
{...panResponder.panHandlers}
style={{ ...props.style, flex: 1 }}>
{children}
</View>
);
}
);
/**
* Clock
*
* Event handler that dispatches
* `{ type: 'TICK', dt: <seconds since last tick> }`
* per animation frame. Doesn't actually render anything.
*/
@connect()
class Clock extends React.Component {
componentDidMount() {
this._requestTick();
}
componentWillUnmount() {
if (this._tickRequestID) {
window.cancelAnimationFrame(this._tickRequestID);
}
}
_requestTick() {
if (!this._lastTickTime) {
this._lastTickTime = Date.now();
}
this._tickRequestID = requestAnimationFrame(this._tick.bind(this));
}
_tick() {
this._tickRequestID = undefined;
const currTime = Date.now();
this.tick(Math.min(0.05, 0.001 * (currTime - this._lastTickTime)));
this._lastTickTime = currTime;
this._requestTick();
}
tick(dt) {
this.props.dispatch({ type: 'TICK', dt });
}
render() {
return null;
}
}
/**
* Game
*
* Brings together event handlers and the Scene.
*/
const Game = () => (
<View style={Styles.container}>
<Clock />
<Scene />
<Touch style={Styles.container}/>
</View>
);
/**
* Main
*
* Initializes a Redux store and provides it to Game.
*/
const dispatchQueue = [];
const queueDispatch = (action) => dispatchQueue.push(action);
const mainReduce = (state, action) => {
if (action.type === 'TICK') {
REPL.flushEvalInQueue();
}
const actions = [action].concat(dispatchQueue);
dispatchQueue.length = 0;
const dispatch = (action) => actions.push(action);
while (actions.length > 0) {
state = sceneReduce(state, actions.shift(), dispatch);
}
return state;
};
const Main = () => {
REPL.connect();
const store = createStore(mainReduce,
mainReduce(undefined, { type: 'START' }));
return (
<Provider store={store}>
<Game />
</Provider>
);
};
AppRegistry.registerComponent('main', () => Main);