-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenScript.cs
195 lines (178 loc) · 9.26 KB
/
ScreenScript.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
using System;
using System.Collections.Generic;
using GameNetcodeStuff;
using UnityEngine;
using UnityEngine.InputSystem;
using Unity.Netcode;
namespace touchscreen {
[DefaultExecutionOrder(100)] // Guarantees that this LateUpdate is executed after LCVR LateUpdate
public class ScreenScript : MonoBehaviour {
private static PlayerControllerB LOCAL_PLAYER => GameNetworkManager.Instance?.localPlayerController;
private static ManualCameraRenderer MAP_RENDERER => StartOfRound.Instance?.mapScreen;
private const float _isCloseMax = 1.25f;
private LineRenderer _vrRay = null;
private bool _vlookingAtMonitor = false;
private bool _lookingAtMonitor {
get => _vlookingAtMonitor;
set {
if (_vrRay is not null) {
_vrRay.enabled = value;
}
_vlookingAtMonitor = value;
}
}
private Bounds GetBounds() {
return Plugin.CREATE_BOUNDS.Invoke(this.gameObject);
}
private bool IsLookingAtMonitor(out Bounds bound, out Ray viewRay, out Ray camRay) {
PlayerControllerB ply = LOCAL_PLAYER;
if (ply is not null && ply.isInHangarShipRoom) {
Ray lookRay = InputUtil.LOOK_RAY.Invoke(ply);
Bounds bounds = GetBounds();
if (bounds.IntersectRay(lookRay, out float distance) && distance <= ply.grabDistance) {
bound = bounds;
viewRay = lookRay;
camRay = MAP_RENDERER.cam.ViewportPointToRay(GetMonitorCoordinates(bounds, lookRay.GetPoint(distance)));
return true;
}
}
bound = default;
viewRay = default;
camRay = default;
return false;
}
private Vector3 GetMonitorCoordinates(Bounds bounds, Vector3 point) {
return new Vector3(
1f - 1f / Math.Abs(bounds.max.z - bounds.min.z) * (point.z - bounds.min.z),
1f / Math.Abs(bounds.max.y - bounds.min.y) * (point.y - bounds.min.y),
0
);
}
private bool TriggerRadar(RadarBoosterItem rItem, bool isAlt) {
if (rItem != null) {
if (isAlt)
rItem.FlashAndSync();
else
rItem.PlayPingAudioAndSync();
return true;
} else
return false;
}
internal void OnPlayerInteraction(bool isAlt) {
PlayerControllerB ply = LOCAL_PLAYER;
if (ply != null && Plugin.IsActive && IsLookingAtMonitor(out Bounds bounds, out Ray lookRay, out Ray camRay)) {
foreach (Collider x in Physics.OverlapCapsule(camRay.GetPoint(0), camRay.GetPoint(10), _isCloseMax)) {
if (!isAlt && x.GetComponent<TerminalAccessibleObject>() is TerminalAccessibleObject tObject) { // Clicked on BigDoor, Land mine, Turret
tObject.CallFunctionFromTerminal();
return;
} else if (!isAlt && x.gameObject.GetComponentInParent<NetworkObject>() is NetworkObject oSpike && oSpike.name.StartsWith("SpikeRoofTrapHazard")) { // Clicked on Spike trap
oSpike.GetComponentInChildren<TerminalAccessibleObject>()?.CallFunctionFromTerminal();
return;
} else if (x.GetComponent<RadarBoosterItem>() is RadarBoosterItem rItem) { // Clicked on Radar booster
TriggerRadar(rItem, isAlt);
return;
} else if (x.GetComponent<PlayerControllerB>() is PlayerControllerB tgtPlayer) { // Clicked on player or radar the player is holding
if (!TriggerRadar(tgtPlayer.currentlyHeldObjectServer?.GetComponent<RadarBoosterItem>(), isAlt) && !isAlt) {
List<TransformAndName> list = MAP_RENDERER.radarTargets;
for (int i = 0; i < list.Count; i++) {
if (tgtPlayer.transform.Equals(list[i].transform)) {
MAP_RENDERER.SwitchRadarTargetAndSync(i);
return;
}
}
}
} else if (!isAlt && ToilHeadUtil.IsEnabled && ToilHeadUtil.CallFollowTerminalAccessibleObject(x)) { // Clicked on ToilHead TerminalAccessibleObject
return;
}
}
}
}
internal void OnPlayerQuickSwitch(bool isAlt) {
PlayerControllerB ply = LOCAL_PLAYER;
if (ply?.isInHangarShipRoom == true) {
Vector3 vec = this.gameObject.transform.position - ply.transform.position;
float distance = Math.Abs(vec.x) + Math.Abs(vec.y) + Math.Abs(vec.z);
if (distance < 6.85f) {
if (!isAlt) {
if (ConfigUtil.CONFIG_ALT_REVERSE.Value && InputUtil.INPUT_ALT_QUICKSWITCH.IsPressed())
MAP_RENDERER.SwitchRadarTargetBackwards(true);
else
MAP_RENDERER.SwitchRadarTargetForward(true);
} else if (!ConfigUtil.CONFIG_ALT_REVERSE.Value)
MAP_RENDERER.SwitchRadarTargetBackwards(true);
}
}
}
private void OnEnable() {
InputUtil.SCREEN_SCRIPT = this;
PlayerControllerB ply = LOCAL_PLAYER;
if (ply == null) {
Plugin.LOGGER.LogWarning("Unable to activate monitor touchscreen. Reason: Failed to get local player.");
return;
}
// VR Ray
if (InputUtil.inVR && ConfigUtil.CONFIG_VR_SHOW_RAY.Value) {
_vrRay = this.gameObject.AddComponent<LineRenderer>();
_vrRay.enabled = false;
_vrRay.positionCount = 2;
_vrRay.SetPositions(new[] {Vector3.zero, Vector3.zero});
_vrRay.widthMultiplier = 0.0075f;
_vrRay.alignment = LineAlignment.View;
// Get material from LCVR
LineRenderer lcvrLR = LCVRUtil.getVRControllerRay();
if (lcvrLR is not null) {
_vrRay.material = lcvrLR.material;
}
}
}
private void OnDisable() {
InputUtil.SCREEN_SCRIPT = null;
}
private void LateUpdate() { // Moved from Update to LateUpdate due to LCVR
PlayerControllerB ply = LOCAL_PLAYER;
if (Plugin.IsActive && IsLookingAtMonitor(out Bounds bounds, out Ray lookRay, out Ray camRay)) {
if (InputUtil.inVR) {
_vrRay?.SetPositions(new[] {lookRay.origin, lookRay.GetPoint(ply.grabDistance)});
LCVRUtil.UpdateInteractCanvas(lookRay.GetPoint(ply.grabDistance / 2));
if (ConfigUtil.CONFIG_SHOW_TOOLTIP.Value) { // Display Tooltips - Is overridden every update by LCVR https://github.com/DaXcess/LCVR/blob/eb568caab38abb1b9dbacb56eaa543059bcb05c3/Source/Player/VRController.cs#L319
ply.cursorTip.text = String.Format("""
[{0}] Interact
[{1}] Flash (Radar)
{2}
""",
InputUtil.GetButtonDescription(InputUtil.INPUT_PRIMARY),
InputUtil.GetButtonDescription(InputUtil.INPUT_SECONDARY),
String.IsNullOrWhiteSpace(ConfigUtil.CONFIG_QUICK_SWITCH.Value) ?
"" :
"[" + InputUtil.GetButtonDescription(InputUtil.INPUT_QUICKSWITCH) + "] Switch target"
);
}
}
if (!_lookingAtMonitor) {
_lookingAtMonitor = true;
ply.isGrabbingObjectAnimation = true; // Blocks the default code from overwriting it again
if (ConfigUtil.CONFIG_SHOW_POINTER.Value && !InputUtil.inVR) { // Display Pointer
ply.cursorIcon.enabled = true;
ply.cursorIcon.sprite = ConfigUtil.HOVER_ICON;
}
if (ConfigUtil.CONFIG_SHOW_TOOLTIP.Value) { // Display Tooltips
ply.cursorTip.text = String.Format("""
[{0}] Interact
[{1}] Flash (Radar)
{2}
""",
InputUtil.GetButtonDescription(InputUtil.INPUT_PRIMARY),
InputUtil.GetButtonDescription(InputUtil.INPUT_SECONDARY),
String.IsNullOrWhiteSpace(ConfigUtil.CONFIG_QUICK_SWITCH.Value) ?
"" :
"[" + InputUtil.GetButtonDescription(InputUtil.INPUT_QUICKSWITCH) + "] Switch target"
);
}
}
} else if (ply != null && _lookingAtMonitor) {
ply.isGrabbingObjectAnimation = false;
_lookingAtMonitor = false;
}
}
}
}