-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.cs
More file actions
189 lines (164 loc) · 6.86 KB
/
InputManager.cs
File metadata and controls
189 lines (164 loc) · 6.86 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
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
using UnityEngine;
using UnityEngine.InputSystem;
namespace MACG.InputManager
{
public static class InputManager
{
/// <summary>
/// MACG: Can be used for disabling input temporarily, like in a pause menu, loading screen transitions...
/// It's great for avoiding bugs when maybe you don't want to read input!
/// You can simply do: InputManager.ReadInput = false;
/// And once your transition/whatever is done, you can do: InputManager.ReadInput = true;
/// To set it back. Cool stuff!
/// </summary>
public static bool ReadInput = true;
#region GetKey
public static bool GetKeyDown(Key keyCode)
{
if (!ReadInput || Keyboard.current == null) return false;
return Keyboard.current[keyCode].wasPressedThisFrame;
}
public static bool GetKeyUp(Key keyCode)
{
if (!ReadInput || Keyboard.current == null) return false;
return Keyboard.current[keyCode].wasReleasedThisFrame;
}
public static bool GetKey(Key keyCode)
{
if (!ReadInput || Keyboard.current == null) return false;
return Keyboard.current[keyCode].isPressed;
}
#endregion
#region AnyKey
public static bool anyKey()
{
if (!ReadInput || Keyboard.current == null) return false;
return Keyboard.current.anyKey.isPressed;
}
public static bool anyKeyDown()
{
if (!ReadInput || Keyboard.current == null) return false;
return Keyboard.current.anyKey.wasPressedThisFrame;
}
public static bool anyKeyUp()
{
if (!ReadInput || Keyboard.current == null) return false;
return Keyboard.current.anyKey.wasReleasedThisFrame;
}
#endregion
#region Mouse
public static bool GetMouseButton(int value)
{
if (!ReadInput || Mouse.current == null) return false;
switch (value)
{
case 0:
return Mouse.current.leftButton.isPressed;
case 1:
return Mouse.current.rightButton.isPressed;
case 2:
return Mouse.current.middleButton.isPressed;
default:
return false;
}
}
public static bool GetMouseButtonDown(int value)
{
if (!ReadInput || Mouse.current == null) return false;
switch (value)
{
case 0:
return Mouse.current.leftButton.wasPressedThisFrame;
case 1:
return Mouse.current.rightButton.wasPressedThisFrame;
case 2:
return Mouse.current.middleButton.wasPressedThisFrame;
default:
return false;
}
}
public static bool GetMouseButtonUp(int value)
{
if (!ReadInput || Mouse.current == null) return false;
switch (value)
{
case 0:
return Mouse.current.leftButton.wasReleasedThisFrame;
case 1:
return Mouse.current.rightButton.wasReleasedThisFrame;
case 2:
return Mouse.current.middleButton.wasReleasedThisFrame;
default:
return false;
}
}
#endregion
#region Axis
static float h, v;
const float gravity = 3f;
const float sensitivity = 3f;
public static float GetAxis(string axisName)
{
if (!ReadInput) return 0f;
switch (axisName)
{
case "Horizontal":
float targetH = GetAxisRaw("Horizontal");
h = Mathf.MoveTowards(h, targetH,
(Mathf.Abs(targetH) > 0f ? sensitivity : gravity) * Time.deltaTime);
return h;
case "Vertical":
float targetV = GetAxisRaw("Vertical");
v = Mathf.MoveTowards(v, targetV,
(Mathf.Abs(targetV) > 0f ? sensitivity : gravity) * Time.deltaTime);
return v;
case "Mouse X":
case "Mouse Y":
case "Jump":
return GetAxisRaw(axisName);
default:
throw new System.NotImplementedException(axisName);
}
}
public static float GetAxisRaw(string axisName)
{
if (!ReadInput) return 0f;
switch (axisName)
{
case "Horizontal":
float gamepadX = 0f;
if (Gamepad.current != null)
gamepadX = Gamepad.current.leftStick.x.ReadValue() + Gamepad.current.dpad.x.ReadValue();
float keyboardX = 0f;
if (Keyboard.current != null)
{
keyboardX += Keyboard.current.aKey.isPressed || Keyboard.current.leftArrowKey.isPressed ? -1f : 0f;
keyboardX += Keyboard.current.dKey.isPressed || Keyboard.current.rightArrowKey.isPressed ? 1f : 0f;
}
return Mathf.Clamp(gamepadX + keyboardX, -1f, 1f);
case "Vertical":
float gamepadY = 0f;
if (Gamepad.current != null)
gamepadY = Gamepad.current.leftStick.y.ReadValue() + Gamepad.current.dpad.y.ReadValue();
float keyboardY = 0f;
if (Keyboard.current != null)
{
keyboardY += Keyboard.current.wKey.isPressed || Keyboard.current.upArrowKey.isPressed ? 1f : 0f;
keyboardY += Keyboard.current.sKey.isPressed || Keyboard.current.downArrowKey.isPressed ? -1f : 0f;
}
return Mathf.Clamp(gamepadY + keyboardY, -1f, 1f);
case "Mouse X":
return Mouse.current != null ? Mouse.current.delta.x.ReadValue() : 0f;
case "Mouse Y":
return Mouse.current != null ? Mouse.current.delta.y.ReadValue() : 0f;
case "Jump":
if (Keyboard.current != null && Keyboard.current.spaceKey.isPressed) return 1f;
if (Gamepad.current != null && Gamepad.current.buttonSouth.isPressed) return 1f;
return 0f;
default:
throw new System.NotImplementedException($"No mapping for axis {axisName}");
}
}
#endregion
}
}