-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
195 lines (192 loc) · 5.54 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Environment,
VrButton,
VrHeadModel,
asset,
NativeModules
} from 'react-360'
import Entity from 'Entity'
const {AudioModule, Location} = NativeModules
export default class MERNVR extends React.Component {
constructor() {
super()
this.state = {
game: {
name: 'Space Exploration',
world: 'https://s3.amazonaws.com/mernbook/vrGame/milkyway.jpg',
answerObjects: [
{
objUrl: 'https://s3.amazonaws.com/mernbook/vrGame/planet.obj',
mtlUrl: 'https://s3.amazonaws.com/mernbook/vrGame/planet.mtl',
translateX: -50,
translateY: 0,
translateZ: 30,
rotateX: 0,
rotateY: 0,
rotateZ: 0,
scale: 7,
color: 'white'
}
],
wrongObjects: [
{
objUrl: 'https://s3.amazonaws.com/mernbook/vrGame/tardis.obj',
mtlUrl: 'https://s3.amazonaws.com/mernbook/vrGame/tardis.mtl',
translateX: 0,
translateY: 0,
translateZ: 90,
rotateX: 0,
rotateY: 20,
rotateZ: 0,
scale: 1,
color: 'white'
}
]
},
vrObjects: [],
hide: 'none',
collectedNum: 0,
collectedList: [],
hmMatrix: VrHeadModel.getHeadMatrix()
}
this.lastUpdate = Date.now()
}
componentDidMount = () => {
let vrObjects = this.state.game.answerObjects.concat(this.state.game.wrongObjects)
this.setState({vrObjects: vrObjects})
Environment.setBackgroundImage(
{uri: this.state.game.world}
)
}
setModelStyles = (vrObject, index) => {
return {
display: this.state.collectedList[index] ? 'none' : 'flex',
color: vrObject.color,
transform: [
{translateX: vrObject.translateX},
{translateY: vrObject.translateY},
{translateZ: vrObject.translateZ},
{scale: vrObject.scale},
{rotateY: vrObject.rotateY},
{rotateX: vrObject.rotateX},
{rotateZ: vrObject.rotateZ}
]
}
}
collectItem = vrObject => event => {
let match = this.state.game.answerObjects.indexOf(vrObject)
if (match != -1) {
let updateCollectedList = this.state.collectedList
let updateCollectedNum = this.state.collectedNum + 1
updateCollectedList[match] = true
this.checkGameCompleteStatus(updateCollectedNum)
AudioModule.playOneShot({
source: asset('collect.mp3'),
})
this.setState({collectedList: updateCollectedList, collectedNum: updateCollectedNum})
} else {
AudioModule.playOneShot({
source: asset('clog-up.mp3'),
})
}
}
checkGameCompleteStatus = (collectedTotal) => {
if (collectedTotal == this.state.game.answerObjects.length) {
AudioModule.playEnvironmental({
source: asset('happy-bot.mp3'),
loop: true
})
this.setState({hide: 'flex', hmMatrix: VrHeadModel.getHeadMatrix()})
}
}
setGameCompletedStyle = () => {
return {
position: 'absolute',
display: this.state.hide,
layoutOrigin: [0.5, 0.5],
width: 6,
transform: [{translate: [0, 0, 0]}, {matrix: this.state.hmMatrix}]
}
}
exitGame = () => {
Location.replace('/')
}
rotate = index => event => {
const now = Date.now()
const diff = now - this.lastUpdate
const vrObjects = this.state.vrObjects
vrObjects[index].rotateY = vrObjects[index].rotateY + diff / 200
this.lastUpdate = now
this.setState({vrObjects: vrObjects})
this.requestID = requestAnimationFrame(this.rotate(index))
}
stopRotate = () => {
if (this.requestID) {
cancelAnimationFrame(this.requestID)
this.requestID = null
}
}
render() {
return (
<View>
{this.state.vrObjects.map((vrObject, i) => {
return (<VrButton onClick={this.collectItem(vrObject)} key={i}>
<Entity style={this.setModelStyles(vrObject, i)}
source={{
obj: {uri: vrObject.objUrl},
mtl: {uri: vrObject.mtlUrl}
}}
onEnter={this.rotate(i)}
onExit={this.stopRotate}
/>
</VrButton>
)
})}
<View style={this.setGameCompletedStyle()}>
<View style={styles.completeMessage}>
<Text style={styles.congratsText}>Congratulations!</Text>
<Text style={styles.collectedText}>You have collected all items in {this.state.game.name}</Text>
</View>
<VrButton onClick={this.exitGame}>
<View style={styles.button}>
<Text style={styles.buttonText}>Play another game</Text>
</View>
</VrButton>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
completeMessage: {
margin: 0.1,
height: 1.5,
backgroundColor: 'green',
transform: [ {translate: [0, 0, -5] } ]
},
congratsText: {
fontSize: 0.5,
textAlign: 'center',
marginTop: 0.2
},
collectedText: {
fontSize: 0.2,
textAlign: 'center'
},
button: {
margin: 0.1,
height: 0.5,
backgroundColor: 'blue',
transform: [ { translate: [0, 0, -5] } ]
},
buttonText: {
fontSize: 0.3,
textAlign: 'center'
}
})
AppRegistry.registerComponent('MERNVR', () => MERNVR);