-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.tsx
180 lines (145 loc) · 5.85 KB
/
game.tsx
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
import React, { Suspense, useRef, useCallback, useEffect, useState } from 'react';
import { SafeAreaView, StyleSheet, Text, View,} from 'react-native';
import Chair from './src/components/chair';
import { Canvas } from '@react-three/fiber/native';
import useControls from 'r3f-native-orbitcontrols';
import * as THREE from "three";
import { useVisibleStore } from './src/context/MyZustand';
import { Dimensions } from 'react-native';
import Trigger from './src/components/Trigger';
import Loader from './src/components/Loader';
const Game: React.FC = () => {
const { width, height } = Dimensions.get('window');
const isPortrait = height > width;
const nCubes= useVisibleStore((state) => state.nCubes)
const { updatecurrentletter, interactionStarted, setInteractionStarted } = useVisibleStore();
const [loading, setLoading] = useState<boolean>(false);
const [letters, setLetters] = useState<string[][]>([]);
const handleCameraChange = useCallback((event: any, index: number, letters:string[][]) => {
const { target } = event;
// console.log("letters[index]: ",letters[index])
// console.log("off we go")
if (target) {
// console.log("target, index: ", target.camera.position, index)
const { x, y, z } = target.camera.position;
const cameraTargetPosition = new THREE.Vector3(x, y, z);
const faces = [
new THREE.Vector3(0, 0, -1), // Positive X
new THREE.Vector3(0, 0, 1), // Positive Y
new THREE.Vector3(-1, 0, 0), // Negative X
new THREE.Vector3(1, 0, 0), // Positive Z
new THREE.Vector3(0, -1, 0), // Negative Y
new THREE.Vector3(0, 1, 0), // Negative Z
];
const cameraDirection = cameraTargetPosition
cameraDirection.normalize();
const dotProducts = faces.map((faceDirection) => cameraDirection.dot(faceDirection));
const mostVisibleFaceIndex = dotProducts.indexOf(Math.min(...dotProducts));
// console.log(index)
// console.log("i, c, l, d, Dir: ", index, cameraTargetPosition, letters[index][mostVisibleFaceIndex], dotProducts, cameraDirection)
}
},[]);
// useEffect(()=>{
// // console.log("interaction starte", interactionStarted)
// },[interactionStarted])
// console.log(interactionStarted)
const controlsAndEvents = Array.from({ length: nCubes }, (_, index) => {
const [OrbitControls, events] = useControls();
const orbitControlsRef = useRef<typeof OrbitControls>();
const canvasRef = useRef<any>(null); // Specify the type as View
return { OrbitControls, events, orbitControlsRef, canvasRef, index, interactionStarted, setInteractionStarted };
});
return (
<SafeAreaView style={styles.container}>
<View style={styles.modelsContainer}>
{loading && <Loader />}
<View style={isPortrait ? styles.columnContainer : styles.rowContainer}>
{controlsAndEvents.map(({ OrbitControls, events, orbitControlsRef, canvasRef, index, interactionStarted, setInteractionStarted }) => {
return (
<View
key={index}
style={isPortrait ? styles.modelColumn : styles.modelRow}
{...events}
>
<Canvas key={index} ref={canvasRef} >
<OrbitControls
key={index}
enableZoom={false}
enablePan={false}
dampingFactor={0.75}
enableRotate={true}
rotateSpeed={0.45}
onChange={(event) => handleCameraChange(event, index, letters)}
{...OrbitControls}
/>
<directionalLight position={[-1, 1, 1]} intensity={4} args={["white", 10]} />
<directionalLight position={[1, -1, 1]} intensity={5} args={["blue", 10]} />
<directionalLight position={[-1, -1, 1]} intensity={3} args={["orange", 10]} />
<directionalLight position={[1, 1, -1]} intensity={5} args={["white", 10]} />
<directionalLight position={[1, -1, -1]} intensity={5} args={["yellow", 10]} />
<directionalLight position={[-1, -1, -1]} intensity={7} args={["white", 10]} />
<Suspense fallback={<Trigger setLoading={setLoading} />}>
<Chair />
</Suspense>
</Canvas>
</View>
)
})}
</View>
</View>
<View style={styles.bottomContainer}>
<View style={styles.textContainer}>
<Text style={styles.text}>
</Text>
</View>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'grey', // Add this line to set the background color to grey
},
modelsContainer: {
flex: 0.9,
},
bottomContainer: {
flex: 0.1,
justifyContent: 'flex-end', // Align the content to the bottom
backgroundColor: '#5e2f00',
borderTopLeftRadius: 30,
borderTopRightRadius: 30,
},
textContainer: {
// position: 'absolute',
flex:1,
justifyContent: 'center', // Center the content vertically
alignItems: 'center', // Center the content horizontally
padding: 20,
// backgroundColor: 'rgba(0, 0.5, 0, 0.5)',
},
text: {
color: 'white', // Specify the text color
fontSize: 20, // Adjust the font size as needed
// fontFamily: 'Arial', // Specify the font family
// Add other text styles as needed
},
columnContainer: {
// flexDirection: 'column',
flex: 1,
},
rowContainer: {
// flexDirection: 'row',
flex: 1,
},
modelColumn: {
flex: 1,
justifyContent: 'center',
},
modelRow: {
flex: 1,
justifyContent: 'center',
},
});
export default Game;