-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathController.cs
217 lines (178 loc) · 5.69 KB
/
Controller.cs
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
using AIChara;
using UnityEngine;
namespace HS2_PovX
{
public static partial class Controller
{
// Angle offsets are used for situations where the character can't move.
// The offsets are added to the neck's current rotation.
// This means that the values can be negative.
public static float cameraAngleOffsetX = 0f;
public static float cameraAngleOffsetY = 0f;
public static float cameraFoV = 0f;
public static Vector3 cameraPosition = Vector3.zero;
public static Quaternion cameraRotation = Quaternion.identity;
public static bool cameraDidSet = false;
public static float cameraSmoothness = 0f;
// 0 = Player; 1 = 1st Partner; 2 = 2nd Partner; 3 = ...
public static int focus = 0;
public static int focusLockOn = -1;
public static ChaControl[] chaCtrls = new ChaControl[0];
public static ChaControl chaCtrl = null;
public static Vector3 prevPosition = Vector3.zero;
public static Vector3 eyeOffset = Vector3.zero;
public static float backupFoV = 0f;
public static Vector3 backupPosition = Vector3.zero;
public static Quaternion backupRotation = Quaternion.identity;
public static bool didHideHead = false;
public static bool Toggled
{
get => chaCtrl != null;
set
{
if (Toggled == value)
return;
if (value)
{
focus = 0;
chaCtrls = GetChaControls();
if (chaCtrls.Length == 0)
return;
SetChaControl(GetChaControl());
}
else
{
SetChaControl(null);
focusLockOn = -1;
}
}
}
public static void Update()
{
if (cameraDidSet)
cameraDidSet = false;
if (HS2_PovX.PoVKey.Value.IsDown())
Toggled = !Toggled;
if (!Toggled)
return;
if (chaCtrl == null || !chaCtrl.visibleAll)
{
SetChaControl(GetChaControl());
if (chaCtrl == null)
{
Toggled = false;
return;
}
}
if (HS2_PovX.CharaCycleKey.Value.IsDown())
{
int prev = focus;
focus = (focus + 1) % chaCtrls.Length;
// Swap lock-on.
if (focusLockOn == focus)
{
focusLockOn = prev;
cameraAngleOffsetX = cameraAngleOffsetY = 0f;
}
SetChaControl(GetChaControl());
return;
}
if (HS2_PovX.LockOnKey.Value.IsDown())
{
focusLockOn = (focusLockOn + 2) % (chaCtrls.Length + 1) - 1;
cameraAngleOffsetX = cameraAngleOffsetY = 0f;
return;
}
float sensitivity = HS2_PovX.Sensitivity.Value;
bool didZoom = HS2_PovX.ZoomKey.Value.IsPressed();
if (didZoom)
sensitivity *= HS2_PovX.ZoomFoV.Value / HS2_PovX.FoV.Value;
float x = Input.GetAxis("Mouse Y") * sensitivity;
float y = Input.GetAxis("Mouse X") * sensitivity;
if (Cursor.lockState != CursorLockMode.None || HS2_PovX.CameraDragKey.Value.IsPressed())
{
float max = HS2_PovX.CameraMaxX.Value;
float min = HS2_PovX.CameraMinX.Value;
float span = HS2_PovX.CameraSpanY.Value;
cameraAngleOffsetX = Mathf.Clamp(cameraAngleOffsetX - x, -max, min);
cameraAngleOffsetY = Mathf.Clamp(cameraAngleOffsetY + y, -span, span);
}
if (HS2_PovX.ToggleCursorKey.Value.IsDown())
{
Cursor.visible = !Cursor.visible;
Cursor.lockState = Cursor.visible ? CursorLockMode.None : CursorLockMode.Locked;
}
else if (!didZoom && !Cursor.visible && Input.anyKeyDown)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
}
public static Vector3 GetDesiredPosition(ChaControl chaCtrl)
{
Transform head = chaCtrl.objHeadBone.transform;
EyeObject[] eyes = chaCtrl.eyeLookCtrl.eyeLookScript.eyeObjs;
Vector3 pos = Vector3.Lerp(
eyes[0].eyeTransform.position,
eyes[1].eyeTransform.position,
0.5f
);
return pos +
HS2_PovX.OffsetX.Value * head.right +
HS2_PovX.OffsetY.Value * head.up +
HS2_PovX.OffsetZ.Value * head.forward;
}
public static void SetPosition()
{
if (cameraPosition != Camera.main.transform.position)
backupPosition = Camera.main.transform.position;
Vector3 next = GetDesiredPosition(chaCtrl);
if (cameraSmoothness > 0f)
next = prevPosition = Vector3.Lerp(next, prevPosition, cameraSmoothness);
Camera.main.transform.position = cameraPosition = next;
}
public static void SetRotation()
{
Transform head = chaCtrl.objHeadBone.transform;
Transform camTransform = Camera.main.transform;
ChaControl lockOn = GetChaControlLockOn();
if (cameraRotation != Camera.main.transform.rotation)
backupRotation = Camera.main.transform.rotation;
if (lockOn != null)
{
Vector3 position = camTransform.position;
camTransform.position = GetDesiredPosition(chaCtrl);
{
camTransform.LookAt(GetDesiredPosition(lockOn), Vector3.up);
}
camTransform.position = position;
}
else if (HS2_PovX.CameraNormalize.Value)
camTransform.rotation = Quaternion.Euler(head.rotation.eulerAngles.x, head.rotation.eulerAngles.y, 0f);
else
camTransform.rotation = head.rotation;
Camera.main.transform.Rotate(cameraAngleOffsetX, cameraAngleOffsetY, 0f);
if (HS2_PovX.CameraHeadRotate.Value)
{
NeckObjectVer2[] bones = chaCtrl.neckLookCtrl.neckLookScript.aBones;
bones[0].neckBone.rotation = Camera.main.transform.rotation;
}
cameraRotation = Camera.main.transform.rotation;
}
public static void SetFoV()
{
if (cameraFoV != Camera.main.fieldOfView)
backupFoV = Camera.main.fieldOfView;
Camera.main.fieldOfView = cameraFoV =
HS2_PovX.ZoomKey.Value.IsPressed() ?
HS2_PovX.ZoomFoV.Value :
HS2_PovX.FoV.Value;
}
public static void ScenePoV()
{
SetRotation();
SetPosition();
SetFoV();
}
}
}