-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cs
162 lines (135 loc) · 5.58 KB
/
Input.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using GBJamGame.Enums;
using GBJamGame.Globals;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
namespace GBJamGame
{
public class Input
{
private readonly IEnumerable<Actions> _actions;
private readonly Dictionary<Actions, List<Buttons>> _buttonMappings;
private readonly Dictionary<Actions, bool> _isPressed;
private readonly Dictionary<Actions, List<Keys>> _keyMappings;
private readonly Dictionary<Actions, float> _pressedTime;
private readonly Dictionary<Actions, bool> _wasPressed;
private bool _isTransitioning;
public Input()
{
_keyMappings = new Dictionary<Actions, List<Keys>>
{
{Actions.A, new List<Keys> {Keys.Z, Keys.P}},
{Actions.B, new List<Keys> {Keys.X, Keys.O}},
{Actions.DPadUp, new List<Keys> {Keys.W, Keys.Up}},
{Actions.DPadDown, new List<Keys> {Keys.S, Keys.Down}},
{Actions.DPadLeft, new List<Keys> {Keys.A, Keys.Left}},
{Actions.DPadRight, new List<Keys> {Keys.D, Keys.Right}},
{Actions.Start, new List<Keys> {Keys.Enter}},
{Actions.Select, new List<Keys> {Keys.Back}},
{Actions.Fullscreen, new List<Keys> {Keys.F1}},
{Actions.Screenshot, new List<Keys> {Keys.F2}},
{Actions.ScreenshotGbRes, new List<Keys> {Keys.F3}}
};
_buttonMappings = new Dictionary<Actions, List<Buttons>>
{
{Actions.A, new List<Buttons> {Buttons.A, Buttons.Y}},
{Actions.B, new List<Buttons> {Buttons.B, Buttons.X}},
{Actions.DPadUp, new List<Buttons> {Buttons.DPadUp, Buttons.LeftThumbstickUp}},
{Actions.DPadDown, new List<Buttons> {Buttons.DPadDown, Buttons.LeftThumbstickDown}},
{Actions.DPadLeft, new List<Buttons> {Buttons.DPadLeft, Buttons.LeftThumbstickLeft}},
{Actions.DPadRight, new List<Buttons> {Buttons.DPadRight, Buttons.LeftThumbstickRight}},
{Actions.Start, new List<Buttons> {Buttons.Start}},
{Actions.Select, new List<Buttons> {Buttons.Back}},
{Actions.Screenshot, new List<Buttons> {Buttons.RightStick}}
};
_isPressed = new Dictionary<Actions, bool>();
_pressedTime = new Dictionary<Actions, float>();
_wasPressed = new Dictionary<Actions, bool>();
_actions = Enum.GetValues(typeof(Actions)).Cast<Actions>();
foreach (var input in _actions)
{
if (!_keyMappings.ContainsKey(input))
_keyMappings.Add(input, new List<Keys>());
if (!_buttonMappings.ContainsKey(input))
_buttonMappings.Add(input, new List<Buttons>());
_isPressed.Add(input, false);
_pressedTime.Add(input, 0);
_wasPressed.Add(input, false);
}
}
private static bool IsButtonDown(GamePadState state, IEnumerable<Buttons> buttons)
{
return state.IsConnected
&& buttons.Any(state.IsButtonDown);
}
private static bool IsKeyDown(KeyboardState state, IEnumerable<Keys> keys)
{
return keys.Any(state.IsKeyDown);
}
public bool Pressed(Actions action)
{
if (_isTransitioning)
return false;
if (!_isPressed.ContainsKey(action) || !_wasPressed.ContainsKey(action))
return false;
return _isPressed[action] && !_wasPressed[action];
}
public bool Released(Actions action)
{
if (_isTransitioning)
return false;
if (!_isPressed.ContainsKey(action) || !_wasPressed.ContainsKey(action))
return false;
return !_isPressed[action] && _wasPressed[action];
}
public bool Down(Actions action)
{
if (_isTransitioning)
return false;
return _isPressed.ContainsKey(action) && _isPressed[action];
}
public bool Repeat(Actions action, float initial)
{
if (_isTransitioning)
return false;
return !(PressedTime(action) < initial) && Down(action);
}
public bool Up(Actions action)
{
if (_isTransitioning)
return false;
return !Down(action);
}
public float PressedTime(Actions action)
{
if (_isTransitioning)
return 0;
return _pressedTime.ContainsKey(action)
? _pressedTime[action]
: 0;
}
public void Update(GameTime gameTime, bool isTransitioning)
{
_isTransitioning = isTransitioning;
var ksState = Keyboard.GetState();
var gpState = GamePad.GetState(PlayerIndex.One);
foreach (var action in _actions)
{
_wasPressed[action] = _isPressed[action];
if (IsKeyDown(ksState, _keyMappings[action]) ||
IsButtonDown(gpState, _buttonMappings[action]))
{
_isPressed[action] = true;
_pressedTime[action] += gameTime.GetElapsedSeconds();
}
else
{
_isPressed[action] = false;
_pressedTime[action] = 0;
}
}
}
}
}