-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputReader.cs
More file actions
73 lines (59 loc) · 2.06 KB
/
InputReader.cs
File metadata and controls
73 lines (59 loc) · 2.06 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
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
[CreateAssetMenu(fileName = "InputReader", menuName = "Scriptable Objects/InputReader")]
public class InputReader : ScriptableObject, PlayerInputSystem.IGameplayActions
{
public event UnityAction attackEvent = delegate { };
public event UnityAction<Vector2> cameraMoveEvent = delegate { };
public event UnityAction<Vector2> moveEvent = delegate { };
public event UnityAction runStartEvent = delegate { };
public event UnityAction runStopEvent = delegate { };
public event UnityAction jumpStartEvent = delegate { };
public event UnityAction jumpCancelEvent = delegate { };
private PlayerInputSystem playerInput;
private void OnEnable()
{
if (playerInput == null) {
playerInput = new PlayerInputSystem();
playerInput.Gameplay.SetCallbacks(this);
}
}
public void EnableGameplay() {
playerInput.Gameplay.Enable();
}
public void DisableInputSystem() {
playerInput.Gameplay.Disable();
}
public void OnAttack(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed) {
attackEvent.Invoke();
}
}
public void OnCameraMove(InputAction.CallbackContext context)
{
cameraMoveEvent.Invoke(context.ReadValue<Vector2>());
}
public void OnMove(InputAction.CallbackContext context)
{
moveEvent.Invoke(context.ReadValue<Vector2>());
}
public void OnRun(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed) {
runStartEvent.Invoke();
}else if (context.phase == InputActionPhase.Canceled) {
runStopEvent.Invoke();
}
}
public void OnJump(InputAction.CallbackContext context) {
if (context.phase == InputActionPhase.Performed)
{
jumpStartEvent.Invoke();
}
else if (context.phase == InputActionPhase.Canceled) {
jumpCancelEvent.Invoke();
}
}
}