-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
HangarLever.cs
141 lines (107 loc) · 4 KB
/
HangarLever.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
using HarmonyLib;
using LCVR.Assets;
using LCVR.Patches;
using LCVR.Player;
using UnityEngine;
using Object = UnityEngine.Object;
namespace LCVR.Physics.Interactions;
public class HangarLever : MonoBehaviour, VRInteractable
{
private InteractTrigger trigger;
private AnimatedObjectTrigger animTrigger;
private bool interacting;
private Transform lookAtTransform;
private float lockedRotation;
private float lockedRotationTime;
internal bool inverse;
public InteractableFlags Flags => InteractableFlags.BothHands;
private void Awake()
{
trigger = GetComponentInParent<InteractTrigger>();
animTrigger = GetComponentInParent<AnimatedObjectTrigger>();
transform.parent.GetComponentInParent<BoxCollider>().enabled = false;
}
public void OnColliderEnter(VRInteractor interactor)
{
}
public void OnColliderExit(VRInteractor interactor)
{
}
public bool OnButtonPress(VRInteractor interactor)
{
if (!trigger.interactable)
return false;
interacting = true;
lookAtTransform = interactor.transform;
interactor.FingerCurler.ForceFist(true);
return true;
}
public void OnButtonRelease(VRInteractor interactor)
{
interactor.FingerCurler.ForceFist(false);
if (!interacting)
return;
interacting = false;
var rot = GetLookRotation();
switch ((!inverse && animTrigger.boolValue) || (inverse && !animTrigger.boolValue))
{
case true when rot is > 160 and < 200:
trigger.Interact(VRSession.Instance.LocalPlayer.transform);
lockedRotation = 180;
lockedRotationTime = Time.realtimeSinceStartup + 0.5f;
break;
case false when rot < 20:
trigger.Interact(VRSession.Instance.LocalPlayer.transform);
lockedRotation = 0;
lockedRotationTime = Time.realtimeSinceStartup + 0.5f;
break;
}
}
private void LateUpdate()
{
if (lockedRotationTime > Time.realtimeSinceStartup)
{
transform.parent.localEulerAngles = new Vector3(0, lockedRotation, 0);
return;
}
if (!interacting)
return;
transform.parent.localEulerAngles = new Vector3(0, GetLookRotation(), 0);
}
private float GetLookRotation()
{
// I know, I'm supposed to calculate this myself but idk how and internet is not helping
var tf = transform.parent;
var rotation = tf.rotation;
tf.LookAt(lookAtTransform.position);
var lookRotation = tf.localEulerAngles.y;
tf.rotation = rotation;
// Prevent specific issue with the lever jumping down
if (lookRotation > 270)
return 0;
return Mathf.Clamp(lookRotation, 0, 180);
}
}
[LCVRPatch]
[HarmonyPatch]
internal static class LeverSwitchPatches
{
[HarmonyPatch(typeof(InteractTrigger), nameof(InteractTrigger.Start))]
[HarmonyPostfix]
private static void OnInteractTriggerStart(InteractTrigger __instance)
{
if (Plugin.Config.DisableHangarLeverInteraction.Value)
return;
var go = __instance.gameObject;
if (go.name is not "LeverSwitchHandle" and not "MagnetLever")
return;
var isMagnet = go.name is "MagnetLever";
go.name = "LeverSwitchInteractable";
var interactableObject = Object.Instantiate(AssetManager.Interactable,
isMagnet ? __instance.transform.Find("LeverSwitchHandle") : __instance.transform);
interactableObject.transform.localPosition = new Vector3(0.0044f, -0.0513f, 0.2529f);
interactableObject.transform.localScale = new Vector3(0.0553f, 0.1696f, 0.0342f);
var lever = interactableObject.AddComponent<HangarLever>();
lever.inverse = isMagnet;
}
}