-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathInputState.cs
118 lines (108 loc) · 5.13 KB
/
InputState.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Hacknet
{
public class InputState
{
public const int MaxInputs = 4;
public static InputState Empty = new InputState();
public readonly bool[] GamePadWasConnected;
public GamePadState[] CurrentGamePadStates;
public KeyboardState[] CurrentKeyboardStates;
public GamePadState[] LastGamePadStates;
public KeyboardState[] LastKeyboardStates;
public InputState()
{
CurrentKeyboardStates = new KeyboardState[4];
CurrentGamePadStates = new GamePadState[4];
LastKeyboardStates = new KeyboardState[4];
LastGamePadStates = new GamePadState[4];
GamePadWasConnected = new bool[4];
}
public void Update()
{
for (var index = 0; index < 4; ++index)
{
LastKeyboardStates[index] = CurrentKeyboardStates[index];
LastGamePadStates[index] = CurrentGamePadStates[index];
CurrentKeyboardStates[index] = Keyboard.GetState((PlayerIndex) index);
CurrentGamePadStates[index] = GamePad.GetState((PlayerIndex) index);
if (CurrentGamePadStates[index].IsConnected)
GamePadWasConnected[index] = true;
}
}
public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
playerIndex = controllingPlayer.Value;
var index = (int) playerIndex;
if (CurrentKeyboardStates[index].IsKeyDown(key))
return LastKeyboardStates[index].IsKeyUp(key);
return false;
}
if (!IsNewKeyPress(key, PlayerIndex.One, out playerIndex) &&
!IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) &&
!IsNewKeyPress(key, PlayerIndex.Three, out playerIndex))
return IsNewKeyPress(key, PlayerIndex.Four, out playerIndex);
return true;
}
public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
playerIndex = controllingPlayer.Value;
var index = (int) playerIndex;
if (CurrentGamePadStates[index].IsButtonDown(button))
return LastGamePadStates[index].IsButtonUp(button);
return false;
}
if (!IsNewButtonPress(button, PlayerIndex.One, out playerIndex) &&
!IsNewButtonPress(button, PlayerIndex.Two, out playerIndex) &&
!IsNewButtonPress(button, PlayerIndex.Three, out playerIndex))
return IsNewButtonPress(button, PlayerIndex.Four, out playerIndex);
return true;
}
public bool IsMenuSelect(PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (!IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) &&
!IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex) &&
!IsNewButtonPress(Buttons.A, controllingPlayer, out playerIndex))
return IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
return true;
}
public bool IsMenuCancel(PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (!IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) &&
!IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex))
return IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex);
return true;
}
public bool IsMenuUp(PlayerIndex? controllingPlayer)
{
PlayerIndex playerIndex;
if (!IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex) &&
!IsNewKeyPress(Keys.W, controllingPlayer, out playerIndex) &&
!IsNewButtonPress(Buttons.DPadUp, controllingPlayer, out playerIndex))
return IsNewButtonPress(Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex);
return true;
}
public bool IsMenuDown(PlayerIndex? controllingPlayer)
{
PlayerIndex playerIndex;
if (!IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex) &&
!IsNewKeyPress(Keys.D, controllingPlayer, out playerIndex) &&
!IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex))
return IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
return true;
}
public bool IsPauseGame(PlayerIndex? controllingPlayer)
{
PlayerIndex playerIndex;
if (!IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) &&
!IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex))
return IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
return true;
}
}
}