-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerInputController.cs
142 lines (126 loc) · 4.06 KB
/
PlayerInputController.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 System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public enum DirectionKey
{
Down,
Left,
Right,
Up
}
public enum Direction {
East,
Northeast,
North,
Northwest,
West,
Southwest,
South,
Southeast,
None
}
public class PlayerInputController : MonoBehaviour
{
// the most recent direction the player moved in
private Direction _direction;
private void LateUpdate() {
_direction = GetRequestedDirection();
}
public Direction GetRequestedDirection()
{
if (IsPressed(DirectionKey.Right))
{
if (IsPressed(DirectionKey.Up))
{
return Direction.Northeast;
}
else if (IsPressed(DirectionKey.Down))
{
return Direction.Southeast;
}
else
{
return Direction.East;
}
}
else if (IsPressed(DirectionKey.Left))
{
if (IsPressed(DirectionKey.Up))
{
return Direction.Northwest;
}
else if (IsPressed(DirectionKey.Down))
{
return Direction.Southwest;
}
else
{
return Direction.West;
}
}
else if (IsPressed(DirectionKey.Up))
{
return Direction.North;
}
else if (IsPressed(DirectionKey.Down))
{
return Direction.South;
}
return Direction.None;
}
// returns true if the user has changed direction this frame
public bool IsTurned() {
var currentDirection = GetRequestedDirection();
return _direction != currentDirection && _direction != Direction.None && currentDirection != Direction.None;
}
// returns true if the direction has been reversed on either the left/right or up/down axis
public bool IsReversed() {
var currentDirection = GetRequestedDirection();
return IsReversedVertical(_direction, currentDirection) || IsReversedHorizontal(_direction, currentDirection);
}
private bool IsReversedVertical(Direction oldDirection, Direction currentDirection) {
switch (oldDirection)
{
case Direction.Northeast:
case Direction.North:
case Direction.Northwest:
return currentDirection == Direction.Southeast || currentDirection == Direction.South || currentDirection == Direction.Southwest;
case Direction.Southeast:
case Direction.South:
case Direction.Southwest:
return currentDirection == Direction.Northeast || currentDirection == Direction.North || currentDirection == Direction.Northwest;
default:
return false;
}
}
private bool IsReversedHorizontal(Direction oldDirection, Direction currentDirection) {
switch (oldDirection)
{
case Direction.Northeast:
case Direction.East:
case Direction.Southeast:
return currentDirection == Direction.Northwest || currentDirection == Direction.West || currentDirection == Direction.Southwest;
case Direction.Northwest:
case Direction.West:
case Direction.Southwest:
return currentDirection == Direction.Northeast || currentDirection == Direction.East || currentDirection == Direction.Southeast;
default:
return false;
}
}
private bool IsPressed(DirectionKey direction)
{
return direction switch {
DirectionKey.Down => IsPressing(new KeyCode[] { KeyCode.D }),
DirectionKey.Left => IsPressing(new KeyCode[] { KeyCode.S }),
DirectionKey.Right => IsPressing(new KeyCode[] { KeyCode.F }),
DirectionKey.Up => IsPressing(new KeyCode[] { KeyCode.E }),
_ => false
};
}
private bool IsPressing(KeyCode[] keyCodes)
{
return keyCodes.Any(keyCode => Input.GetKey(keyCode));
}
}