-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Face.cs
135 lines (105 loc) · 3.78 KB
/
Face.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
using GameNetcodeStuff;
using HarmonyLib;
using LCVR.Assets;
using LCVR.Patches;
using LCVR.Player;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LCVR.Physics.Interactions;
public class Face : MonoBehaviour, VRInteractable
{
private readonly List<string> ALLOWED_ITEMS = [
"Walkie-talkie",
"TZP-Inhalant",
"Comedy",
"Tragedy",
];
private GrabbableObject heldItem;
private bool isInteracting;
private Coroutine stopInteractingCoroutine;
public InteractableFlags Flags => InteractableFlags.RightHand;
public bool IsInteracting => isInteracting;
private bool CanInteract => VRSession.Instance.LocalPlayer.PlayerController.CanUseItem();
private void Update()
{
if (!isInteracting)
return;
var item = GetItem();
if (!item || heldItem == item ||
VRSession.Instance.LocalPlayer.PlayerController.timeSinceSwitchingSlots < 0.075f)
return;
heldItem = item;
heldItem.UseItemOnClient();
}
public void OnColliderEnter(VRInteractor _)
{
if (stopInteractingCoroutine != null)
{
StopCoroutine(stopInteractingCoroutine);
stopInteractingCoroutine = null;
return;
}
var item = GetItem();
if (item == null || !CanInteract)
return;
isInteracting = true;
heldItem = item;
heldItem.UseItemOnClient();
}
public void OnColliderExit(VRInteractor _)
{
stopInteractingCoroutine = StartCoroutine(DelayedStopUsing());
}
private IEnumerator DelayedStopUsing()
{
yield return new WaitForSeconds(0.25f);
isInteracting = false;
heldItem = null;
var item = GetItem();
if (CanInteract)
item?.UseItemOnClient(false);
stopInteractingCoroutine = null;
}
public bool OnButtonPress(VRInteractor _) { return false; }
public void OnButtonRelease(VRInteractor _) { }
private GrabbableObject GetItem()
{
var item = VRSession.Instance.LocalPlayer.PlayerController.currentlyHeldObjectServer;
if (!item || !item.itemProperties || !ALLOWED_ITEMS.Contains(item.itemProperties.itemName))
return null;
return item;
}
public static Face Create()
{
if (Plugin.Config.DisableFaceInteractions.Value)
return null;
var interactableObject = Instantiate(AssetManager.Interactable, VRSession.Instance.MainCamera.transform);
interactableObject.transform.localPosition = new Vector3(0, -0.1f, 0.1f);
interactableObject.transform.localScale = new Vector3(0.225f, 0.2f, 0.225f);
return interactableObject.AddComponent<Face>();
}
}
[LCVRPatch]
[HarmonyPatch]
internal static class FaceItemInteractionPatches
{
/// <summary>
/// Prevent the item activation keybind from doing anything if we're currently holding an active item up to our face
/// </summary>
[HarmonyPatch(typeof(PlayerControllerB), nameof(PlayerControllerB.ActivateItem_performed))]
[HarmonyPrefix]
private static bool CanInteractUsingController()
{
return VRSession.Instance?.Face is not { IsInteracting: true };
}
/// <summary>
/// Prevent the item activation keybind from doing anything if we're currently holding an active item up to our face
/// </summary>
[HarmonyPatch(typeof(PlayerControllerB), nameof(PlayerControllerB.ActivateItem_canceled))]
[HarmonyPrefix]
private static bool CanStopInteractUsingController()
{
return VRSession.Instance?.Face is not { IsInteracting: true };
}
}