-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputHandler.cs
More file actions
121 lines (98 loc) · 4.13 KB
/
InputHandler.cs
File metadata and controls
121 lines (98 loc) · 4.13 KB
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
#if STEAMVR
using Valve.VR;
#endif
using UnityEngine;
using UnityEngine.InputSystem;
using WalkSimulator.Animators;
using WalkSimulator.Menus;
using WalkSimulator.Rigging;
using CommonUsages = UnityEngine.XR.CommonUsages;
using InputDevice = UnityEngine.XR.InputDevice;
namespace WalkSimulator;
public class InputHandler : MonoBehaviour
{
public static Vector3 inputDirection;
public static Vector3 inputDirectionNoY;
public static InputHandler Instance { get; private set; }
private bool Jump => Keyboard.current.spaceKey.wasPressedThisFrame;
private void Awake()
{
Instance = this;
}
private void Update()
{
if (!Plugin.Instance.Enabled || ComputerGUI.Instance.IsInUse)
return;
GetInputDirection();
if (Keyboard.current.escapeKey.wasPressedThisFrame)
HeadDriver.Instance.LockCursor = !HeadDriver.Instance.LockCursor;
if (Keyboard.current.cKey.wasPressedThisFrame)
HeadDriver.Instance.ToggleCam();
RadialMenu radialMenu = Plugin.Instance.radialMenu;
bool tabPressed = Keyboard.current.tabKey.isPressed;
radialMenu.enabled = tabPressed;
radialMenu.gameObject.SetActive(tabPressed);
if (Keyboard.current.digit1Key.wasPressedThisFrame) EnableEmote(EmoteAnimator.Emote.Wave);
if (Keyboard.current.digit2Key.wasPressedThisFrame) EnableEmote(EmoteAnimator.Emote.Point);
if (Keyboard.current.digit3Key.wasPressedThisFrame) EnableEmote(EmoteAnimator.Emote.ThumbsUp);
if (Keyboard.current.digit4Key.wasPressedThisFrame) EnableEmote(EmoteAnimator.Emote.ThumbsDown);
if (Keyboard.current.digit5Key.wasPressedThisFrame) EnableEmote(EmoteAnimator.Emote.Shrug);
if (Keyboard.current.digit6Key.wasPressedThisFrame) EnableEmote(EmoteAnimator.Emote.Dance);
if (Keyboard.current.digit7Key.wasPressedThisFrame) EnableEmote(EmoteAnimator.Emote.Dance2);
if (Keyboard.current.digit8Key.wasPressedThisFrame) EnableEmote(EmoteAnimator.Emote.Goofy);
if (Keyboard.current.digit9Key.wasPressedThisFrame) EnableEmote(EmoteAnimator.Emote.UltraGoofy);
}
private void EnableEmote(EmoteAnimator.Emote emote)
{
if (Plugin.Instance.emoteAnimator is EmoteAnimator emoteAnimator)
{
Rig.Instance.Animator = emoteAnimator;
emoteAnimator.emote = emote;
}
}
private void GetInputDirection()
{
Vector3 keyboardDir = KeyboardInput();
if (keyboardDir.magnitude > 0f)
{
inputDirection = keyboardDir.normalized;
inputDirectionNoY = new Vector3(keyboardDir.x, 0f, keyboardDir.z).normalized;
return;
}
float x = 0f, y = 0f, z = 0f;
if (Plugin.IsSteam)
{
#if STEAMVR
Vector2 leftAxis = SteamVR_Actions.gorillaTag_LeftJoystick2DAxis.axis;
Vector2 rightAxis = SteamVR_Actions.gorillaTag_RightJoystick2DAxis.axis;
x = leftAxis.x;
y = rightAxis.y;
z = leftAxis.y;
#endif
}
else
{
InputDevice leftDevice = ControllerInputPoller.instance.leftControllerDevice;
InputDevice rightDevice = ControllerInputPoller.instance.rightControllerDevice;
leftDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out Vector2 leftAxis);
rightDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out Vector2 rightAxis);
x = leftAxis.x;
y = rightAxis.y;
z = leftAxis.y;
}
Vector3 dir = new(x, y, z);
inputDirection = dir.normalized;
inputDirectionNoY = new Vector3(x, 0f, z).normalized;
}
private Vector3 KeyboardInput()
{
float x = 0f, y = 0f, z = 0f;
if (Keyboard.current.aKey.isPressed) x -= 1f;
if (Keyboard.current.dKey.isPressed) x += 1f;
if (Keyboard.current.sKey.isPressed) z -= 1f;
if (Keyboard.current.wKey.isPressed) z += 1f;
if (Keyboard.current.ctrlKey.isPressed) y -= 1f;
if (Keyboard.current.spaceKey.isPressed) y += 1f;
return new Vector3(x, y, z);
}
}