-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerStateManager.cs
142 lines (111 loc) · 4.56 KB
/
PlayerStateManager.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
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerStateManager : StateMachineManager
{
[Header("Player State Manager")]
public PlayerMoveData moveData;
private PlayerInput _playerInput;
private InputActionAsset _playerControlsActions;
private PlayerState _currentPlayerState;
public PlayerState PreviousPlayState { get; private set; }
private PlayerIdleState _idleState;
private PlayerWalkingState _walkingState;
private PlayerRollState _rollState;
private PlayerDashState _dashState;
private PlayerSmackState _smackState;
private PlayerFallingState _fallingState;
private new void Awake()
{
_idleState = GetComponent<PlayerIdleState>();
_walkingState = GetComponent<PlayerWalkingState>();
_rollState = GetComponent<PlayerRollState>();
_dashState = GetComponent<PlayerDashState>();
_smackState = GetComponent<PlayerSmackState>();
_fallingState = GetComponent<PlayerFallingState>();
base.Awake();
AddListeners();
}
private new void FixedUpdate()
{
base.FixedUpdate();
if (!currentState.IsValidToSwitch) return;
var moveInput = _playerControlsActions["Move"].ReadValue<Vector2>();
if (moveInput.x != 0)
{
if(_currentPlayerState != PlayerState.Walking) SwitchState(PlayerState.Walking);
moveData.SetMoveDirection(moveInput);
}
else if (_currentPlayerState != PlayerState.Idle && moveData.GroundChecker.IsGrounded) SwitchState(PlayerState.Idle);
}
#region Switch State
/// <summary>
/// Switch the current targetState to a different one.
/// Is it valid to switch targetState?
/// </summary>
/// <param name="targetState">Give target state to switch into.</param>
public void SwitchState(PlayerState targetState)
{
var state = targetState switch
{
PlayerState.Idle => _idleState,
PlayerState.Walking => _walkingState,
PlayerState.Dashing => _dashState,
PlayerState.Rolling => _rollState,
PlayerState.Smacking => _smackState,
PlayerState.Falling => _fallingState,
_ => startingState
};
base.SwitchState(state);
if (currentState != state) return;
PreviousPlayState = _currentPlayerState;
_currentPlayerState = targetState;
}
/// <summary>
/// This function is used for Unity Events, because they can not have an Enum as parameter.
/// </summary>
/// <param name="enumValue">The PlayerState in int variable.</param>
public void SwitchStateEvent(int enumValue) => SwitchState((PlayerState)enumValue);
#endregion
#region Inputs
public Vector2 SetMousePos()
{
var mousePos = _playerControlsActions["MousePosition"].ReadValue<Vector2>();
return Camera.main.ScreenToWorldPoint(new Vector2(mousePos.x, mousePos.y));
}
private void AddListeners()
{
_playerInput = GetComponent<PlayerInput>();
_playerControlsActions = _playerInput.actions;
_playerControlsActions["Roll"].performed += Roll;
_playerControlsActions["Dash"].performed += Dash;
_playerControlsActions["Smack"].performed += Smack;
_playerControlsActions["DisableMovement"].performed += DisableMovement;
_playerControlsActions["Temp-remove-pickup"].performed += Remove;
}
private void OnDestroy()
{
RemoveListeners();
}
public void RemoveListeners()
{
_playerControlsActions["Roll"].performed -= Roll;
_playerControlsActions["Dash"].performed -= Dash;
_playerControlsActions["Smack"].performed -= Smack;
_playerControlsActions["DisableMovement"].performed -= DisableMovement;
_playerControlsActions["Temp-remove-pickup"].performed -= Remove;
}
private void Roll(InputAction.CallbackContext context) => SwitchState(PlayerState.Rolling);
private void Dash(InputAction.CallbackContext context)
{
moveData.MouseWorldPosition = SetMousePos();
SwitchState(PlayerState.Dashing);
}
private void Smack(InputAction.CallbackContext context) => SwitchState(PlayerState.Smacking);
private void DisableMovement(InputAction.CallbackContext context)
{
moveData.ToggleCanMove();
SwitchState(PlayerState.Idle);
}
private void Remove(InputAction.CallbackContext context) => PickupSystem.Instance.RemovePickup(PickupType.Stick);
#endregion
}