少年修仙传客户端基础资源
dabaoji
2025-06-04 34d28a982a741d63f183884881b0bea73f8c8b47
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Poco;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using TcpServer;
using UnityEngine;
using Debug = UnityEngine.Debug;
 
public class VRSupport
{
    private static Queue<Action> commands = new Queue<Action>();
 
    public VRSupport()
    {
        commands = new Queue<Action>();
    }
 
    public void ClearCommands()
    {
        commands.Clear();
    }
 
    public void PeekCommand()
    {
        if (null != commands && commands.Count > 0)
        {
            Debug.Log("command executed " + commands.Count);
            commands.Peek()();
        }
    }
 
    public object isVRSupported(List<object> param)
    {
#if UNITY_3 || UNITY_4
            return false;
#elif UNITY_5 || UNITY_2017_1
            return UnityEngine.VR.VRSettings.loadedDeviceName.Equals("CARDBOARD");
#else
        return UnityEngine.XR.XRSettings.loadedDeviceName.Equals("CARDBOARD");
#endif
    }
 
    public object IsQueueEmpty(List<object> param)
    {
        Debug.Log("Checking queue");
        if (commands != null && commands.Count > 0)
        {
            return null;
        }
        else
        {
            Thread.Sleep(1000); // we wait a bit and check again just in case we run in between calls
            if (commands != null && commands.Count > 0)
            {
                return null;
            }
        }
 
        return commands.Count;
    }
 
    public object RotateObject(List<object> param)
    {
        var xRotation = Convert.ToSingle(param[0]);
        var yRotation = Convert.ToSingle(param[1]);
        var zRotation = Convert.ToSingle(param[2]);
        float speed = 0f;
        if (param.Count > 5)
            speed = Convert.ToSingle(param[5]);
        Vector3 mousePosition = new Vector3(xRotation, yRotation, zRotation);
        foreach (GameObject cameraContainer in GameObject.FindObjectsOfType<GameObject>())
        {
            if (cameraContainer.name.Equals(param[3]))
            {
                foreach (GameObject cameraFollower in GameObject.FindObjectsOfType<GameObject>())
                {
                    if (cameraFollower.name.Equals(param[4]))
                    {
                        lock (commands)
                        {
                            commands.Enqueue(() => recoverOffset(cameraFollower, cameraContainer, speed));
                        }
 
                        lock (commands)
                        {
                            var currentRotation = cameraContainer.transform.rotation;
                            commands.Enqueue(() => rotate(cameraContainer, currentRotation, mousePosition, speed));
                        }
                        return true;
                    }
                }
 
                return true;
            }
        }
        return false;
    }
 
    public object ObjectLookAt(List<object> param)
    {
        float speed = 0f;
        if (param.Count > 3)
            speed = Convert.ToSingle(param[3]);
        foreach (GameObject toLookAt in GameObject.FindObjectsOfType<GameObject>())
        {
            if (toLookAt.name.Equals(param[0]))
            {
                foreach (GameObject cameraContainer in GameObject.FindObjectsOfType<GameObject>())
                {
                    if (cameraContainer.name.Equals(param[1]))
                    {
                        foreach (GameObject cameraFollower in GameObject.FindObjectsOfType<GameObject>())
                        {
                            if (cameraFollower.name.Equals(param[2]))
                            {
                                lock (commands)
                                {
                                    commands.Enqueue(() => recoverOffset(cameraFollower, cameraContainer, speed));
                                }
 
                                lock (commands)
                                {
                                    commands.Enqueue(() => objectLookAt(cameraContainer, toLookAt, speed));
                                }
 
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
 
    protected void rotate(GameObject go, Quaternion originalRotation, Vector3 mousePosition, float speed)
    {
        Debug.Log("rotating");
        if (!RotateObject(originalRotation, mousePosition, go, speed))
        {
            lock (commands)
            {
                commands.Dequeue();
            }
        }
    }
 
    protected void objectLookAt(GameObject go, GameObject toLookAt, float speed)
    {
        Debug.Log("looking at " + toLookAt.name);
        Debug.Log("from " + go.name);
        if (!ObjectLookAtObject(toLookAt, go, speed))
        {
            lock (commands)
            {
                commands.Dequeue();
            }
        }
    }
 
    protected void recoverOffset(GameObject subcontainter, GameObject cameraContainer, float speed)
    {
        Debug.Log("recovering " + cameraContainer.name);
        if (!ObjectRecoverOffset(subcontainter, cameraContainer, speed))
        {
            lock (commands)
            {
                commands.Dequeue();
            }
        }
    }
 
    protected bool RotateObject(Quaternion originalPosition, Vector3 mousePosition, GameObject cameraContainer, float rotationSpeed = 0.125f)
    {
        if (null == cameraContainer)
        {
            return false;
        }
 
        var expectedx = originalPosition.eulerAngles.x + mousePosition.x;
        var expectedy = originalPosition.eulerAngles.y + mousePosition.y;
        var expectedz = originalPosition.eulerAngles.z + mousePosition.z;
 
        var toRotation = Quaternion.Euler(new Vector3(expectedx, expectedy, expectedz));
        cameraContainer.transform.rotation = Quaternion.Lerp(cameraContainer.transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
        var angle = Quaternion.Angle(cameraContainer.transform.rotation, toRotation);
 
        if (angle == 0)
        {
            return false;
        }
 
        return true;
    }
 
    protected bool ObjectLookAtObject(GameObject go, GameObject cameraContainer, float rotationSpeed = 0.125f)
    {
        if (null == go || null == cameraContainer)
        {
            Debug.Log("exception - item null");
            return false;
        }
 
        var toRotation = Quaternion.LookRotation(go.transform.position - (cameraContainer.transform.localPosition));
        cameraContainer.transform.rotation = Quaternion.Lerp(cameraContainer.transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
        // It should not be needed but sometimes the difference of eurlerAngles might be small and this would ensure it works fine
        if (Quaternion.Angle(cameraContainer.transform.rotation, toRotation) == 0)
        {
 
            return false;
        }
 
        return true;
    }
 
    protected bool ObjectRecoverOffset(GameObject subcontainer, GameObject cameraContainer, float rotationSpeed = 0.125f)
    {
        if (null == cameraContainer)
        {
            Debug.Log("exception - item null");
            return false;
        }
 
        // add offset with the camera
        var cameraRotation = Camera.main.transform.localRotation;
 
        var toRotate = new Quaternion(-cameraRotation.x, -cameraRotation.y, -cameraRotation.z, cameraRotation.w);
        subcontainer.transform.localRotation = Quaternion.Lerp(subcontainer.transform.localRotation, toRotate, rotationSpeed * Time.deltaTime);
 
        // It should not be needed but sometimes the difference of eurlerAngles might be small and this would ensure it works fine
        if (Quaternion.Angle(subcontainer.transform.localRotation, toRotate) == 0)
        {
            return false;
        }
 
        return true;
    }
}