-
Notifications
You must be signed in to change notification settings - Fork 1
/
Locker_Door_Opener.cs
316 lines (294 loc) · 14.6 KB
/
Locker_Door_Opener.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using HarmonyLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static ErrorMessage;
using UnityEngine;
namespace Tweaks_Fixes
{
internal class Locker_Door_Opener
{
static FMODAsset openSound;
static FMODAsset closeSound;
public class LockerDoorOpener : MonoBehaviour
{
public float startRotation;
public float endRotation;
public float t;
public float duration = 1f;
public float openAngle = 135f;
public float doubleDoorOpenAngle = 90f;
public IEnumerator Rotate(Transform door, bool playCloseSound = false, bool deco = false)
{
while (t < duration)
{
t += Time.deltaTime;
float f = t / duration;
float rotation = Mathf.Lerp(startRotation, endRotation, f);
//Main.Log("rotation " + rotation );
//AddDebug(" rotation " + rotation);
if (deco)
door.localEulerAngles = new Vector3(door.localEulerAngles.x, rotation, door.localEulerAngles.z);
//else if (test)
// door.localEulerAngles = new Vector3(door.localEulerAngles.x, rotation, door.localEulerAngles.z);
else
door.localEulerAngles = new Vector3(door.localEulerAngles.x, door.localEulerAngles.y, rotation);
if (endRotation == 0f)
{
if (playCloseSound && f > .62f && closeSound != null)
{
playCloseSound = false;
Utils.PlayFMODAsset(closeSound, door.transform);
}
else if (f > 1f)
{
ColoredLabel cl = door.GetComponentInChildren<ColoredLabel>();
Transform parent = door.transform.parent.parent.parent;
if (cl && parent)
cl.transform.SetParent(parent);
}
}
yield return null;
}
}
public IEnumerator Rotate(Transform doorLeft, Transform doorRight, bool playCloseSound = false)
{
while (t < duration)
{
t += Time.deltaTime;
float f = t / duration;
float rotation = Mathf.Lerp(startRotation, endRotation, f);
doorLeft.localEulerAngles = new Vector3(doorLeft.localEulerAngles.x, doorLeft.localEulerAngles.y, -rotation);
doorRight.localEulerAngles = new Vector3(doorRight.localEulerAngles.x, doorRight.localEulerAngles.y, rotation);
if (f > .62f && playCloseSound && closeSound != null)
{
playCloseSound = false;
Utils.PlayFMODAsset(closeSound, doorLeft.transform.parent);
}
yield return null;
}
}
}
public class DecoLockerMyController : HandTarget, IHandTarget
{
private StorageContainer _storageContainer = null;
public override void Awake()
{
//base.Awake();
//if (gameObject.transform.parent == null)
// AddDebug("DecoLockerMyController Awake parent null");
//else
HandTarget[] handTargets = transform.GetComponents<HandTarget>();
HandTarget oldController = null;
foreach (HandTarget handTarget in handTargets)
{
if (handTarget != this)
oldController = handTarget;
}
if (oldController)
Destroy(oldController);
_storageContainer = gameObject.transform.GetComponentInChildren<StorageContainer>();
}
public void OnHandClick(GUIHand hand)
{
if (!base.enabled)
return;
if (_storageContainer)
_storageContainer.OnHandClick(hand);
}
public void OnHandHover(GUIHand hand)
{
if (!base.enabled)
return;
//AddDebug("DecoLockerMyController OnHandHover " + transform.name);
//AddDebug("DecoLockerMyController OnHandHover parent " + transform.parent.name);
if (_storageContainer == null)
{
//AddDebug("DecoLockerMyController OnHandHover _storageContainer == null " + transform.name);
_storageContainer = gameObject.transform.GetComponentInChildren<StorageContainer>();
}
else
_storageContainer.OnHandHover(hand);
}
}
[HarmonyPatch(typeof(StorageContainer))]
class StorageContainer_Patch
{
private static void OpenWallLocker(StorageContainer __instance)
{
Transform door = __instance.transform.Find("model/submarine_locker_02/submarine_locker_02_door");
if (door)
{
//AddDebug("SmallLocker Open ");
ColoredLabel cl = __instance.GetComponentInChildren<ColoredLabel>(true);
if (cl)
cl.transform.SetParent(door.transform);
LockerDoorOpener rotater = __instance.gameObject.EnsureComponent<LockerDoorOpener>();
rotater.startRotation = door.transform.localEulerAngles.z;
rotater.endRotation = rotater.startRotation + rotater.openAngle;
rotater.t = 0f;
rotater.StartCoroutine(rotater.Rotate(door));
if (openSound != null)
Utils.PlayFMODAsset(openSound, __instance.transform);
}
}
private static void CloseWallLocker(StorageContainer __instance)
{
Transform door = __instance.transform.Find("model/submarine_locker_02/submarine_locker_02_door");
if (door)
{
//AddDebug("SmallLocker OnClose ");
LockerDoorOpener rotater = __instance.gameObject.EnsureComponent<LockerDoorOpener>();
rotater.startRotation = door.transform.localEulerAngles.z;
rotater.endRotation = 0f;
rotater.t = 0f;
rotater.StartCoroutine(rotater.Rotate(door, true));
}
}
[HarmonyPostfix]
[HarmonyPatch("Awake")]
static void AwakePostfix(StorageContainer __instance)
{
//TechTag techTag = __instance.GetComponent<TechTag>();
//AddDebug("StorageContainer Awake " + __instance.prefabRoot.name);
//if (__instance.transform.parent)
// AddDebug("StorageContainer Awake parent " + __instance.transform.parent.name);
//if (techTag && techTag.type == TechType.SmallLocker)
//{
//}
if (openSound == null)
{
openSound = ScriptableObject.CreateInstance<FMODAsset>();
openSound.path = "event:/sub/cyclops/locker_open";
openSound.id = "{c97d1fdf-ea26-4b19-8358-7f6ea77c3763}";
closeSound = ScriptableObject.CreateInstance<FMODAsset>();
closeSound.path = "event:/sub/cyclops/locker_close";
closeSound.id = "{16eb5589-e341-41cb-9c88-02cb4e3da44a}";
}
}
[HarmonyPostfix]
[HarmonyPatch("Open", new Type[] { typeof(Transform) })]
static void OpenPostfix(StorageContainer __instance, Transform useTransform)
{
TechTag techTag = __instance.GetComponent<TechTag>();
if (techTag == null)
techTag = __instance.transform.parent.GetComponent<TechTag>();
//AddDebug(" Open " + __instance.name);
//AddDebug(" Open useTransform " + useTransform.name);
//if (__instance.transform.parent.name == "upgrade_geoHldr")
//{
// Transform door = __instance.transform.parent.Find("Exosuit_01_storage");
// if (door)
// {
// AddDebug("Exosuit_01_storage " + door.transform.localEulerAngles);
// }
//}
if (techTag == null)
return;
string techType = techTag.type.ToString();
//AddDebug(" Open " + techTag.type + " " + useTransform.name);
if (techTag.type == TechType.SmallLocker || techType == "RadLocker")
{
OpenWallLocker(__instance);
}
else if (techTag.type == TechType.Locker)
{
Transform doorLeft = __instance.transform.Find("model/submarine_Storage_locker_big_01/submarine_Storage_locker_big_01_hinges_L");
Transform doorRight = __instance.transform.Find("model/submarine_Storage_locker_big_01/submarine_Storage_locker_big_01_hinges_R");
if (doorLeft && doorRight)
{
LockerDoorOpener rotater = __instance.gameObject.EnsureComponent<LockerDoorOpener>();
rotater.startRotation = doorLeft.transform.localEulerAngles.z;
rotater.endRotation = rotater.startRotation + rotater.doubleDoorOpenAngle;
rotater.t = 0f;
rotater.StartCoroutine(rotater.Rotate(doorLeft, doorRight));
if (openSound != null)
Utils.PlayFMODAsset(openSound, __instance.transform);
}
}
//AddDebug("StorageContainer open " + name);
else if (techType == "DecorativeLockerClosed")
{
Transform door = __instance.transform.parent.Find("submarine_locker_03_door_01/locker_04_door");
if (door)
{
//AddDebug("DecorativeLockerClosed");
LockerDoorOpener rotater = __instance.gameObject.EnsureComponent<LockerDoorOpener>();
rotater.startRotation = door.transform.localEulerAngles.z;
rotater.endRotation = rotater.startRotation + rotater.openAngle;
rotater.t = 0f;
rotater.StartCoroutine(rotater.Rotate(door, false, true));
if (openSound != null)
Utils.PlayFMODAsset(openSound, __instance.transform);
}
}
return;
//else if (name == "Autosorter")
{
Transform door = __instance.transform.Find("model/submarine_locker_02/submarine_locker_02_door");
if (door)
{
Transform canvas = __instance.transform.Find("Canvas");
FollowTransform ft = canvas.gameObject.EnsureComponent<FollowTransform>();
ft.parent = door;
//ft.keepRotation = true;
//AddDebug("Autosorter Open ");
ColoredLabel cl = __instance.GetComponentInChildren<ColoredLabel>(true);
if (cl)
cl.transform.SetParent(door.transform);
LockerDoorOpener rotater = __instance.gameObject.EnsureComponent<LockerDoorOpener>();
rotater.startRotation = door.transform.localEulerAngles.z;
rotater.endRotation = rotater.startRotation + rotater.openAngle;
rotater.t = 0f;
rotater.StartCoroutine(rotater.Rotate(door));
if (openSound != null)
Utils.PlayFMODAsset(openSound, __instance.transform);
}
}
}
[HarmonyPostfix]
[HarmonyPatch("OnClose")]
static void OnClosePostfix(StorageContainer __instance)
{
TechTag techTag = __instance.GetComponent<TechTag>();
if (techTag == null)
techTag = __instance.transform.parent.GetComponent<TechTag>();
if (techTag == null)
return;
string techType = techTag.type.ToString();
if (techTag.type == TechType.SmallLocker || techType == "RadLocker")
{
CloseWallLocker(__instance);
}
else if (techTag.type == TechType.Locker)
{
Transform doorLeft = __instance.transform.Find("model/submarine_Storage_locker_big_01/submarine_Storage_locker_big_01_hinges_L");
Transform doorRight = __instance.transform.Find("model/submarine_Storage_locker_big_01/submarine_Storage_locker_big_01_hinges_R");
if (doorLeft && doorRight)
{
LockerDoorOpener rotater = __instance.gameObject.EnsureComponent<LockerDoorOpener>();
rotater.startRotation = doorRight.transform.localEulerAngles.z;
rotater.endRotation = 0f;
rotater.t = 0f;
rotater.StartCoroutine(rotater.Rotate(doorLeft, doorRight, true));
}
}
else if (techType == "DecorativeLockerClosed")
{
Transform door = __instance.transform.parent.Find("submarine_locker_03_door_01/locker_04_door");
if (door)
{
//AddDebug("DecorativeLockerClosed");
LockerDoorOpener rotater = __instance.gameObject.EnsureComponent<LockerDoorOpener>();
rotater.startRotation = door.transform.localEulerAngles.y;
rotater.endRotation = 0f;
rotater.t = 0f;
rotater.StartCoroutine(rotater.Rotate(door, true, true));
}
}
}
}
}
}