-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputManager.cs
121 lines (101 loc) · 4.2 KB
/
InputManager.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Swing.Engine;
using System;
using System.Collections.Generic;
using System.Text;
namespace Swing
{
static class InputManager
{
public static Vector2 Direction { get; private set; }
public static Vector2 SecondaryDirection { get; private set; }
public static bool MenuButton { get; private set; }
/// <summary>
/// The screenspace position of the mouse cursor
/// </summary>
public static Vector2 MouseLocation { get; private set; }
public static bool MousePressed { get; private set; }
public static bool MouseReleased { get; private set; }
public static bool MouseHeld { get; private set; }
public static bool Jump { get; private set; }
public static bool JumpHeld { get; private set; }
private static KeyboardState currentKeyboardState;
private static KeyboardState previousKeyboardState;
private static GamePadState currentGamePadState;
private static GamePadState previousGamePadState;
private static MouseState currentMouseState;
private static MouseState previousMouseState;
public static void Update()
{
previousKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
previousGamePadState = currentGamePadState;
currentGamePadState = GamePad.GetState(0);
previousMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
#region Direction
Direction = currentGamePadState.ThumbSticks.Right;
if (currentKeyboardState.IsKeyDown(Keys.A))
{
Direction += new Vector2(-1, 0);
}
if (currentKeyboardState.IsKeyDown(Keys.D))
{
Direction += new Vector2(1, 0);
}
if (currentKeyboardState.IsKeyDown(Keys.W))
{
Direction += new Vector2(0, 1);
}
if (currentKeyboardState.IsKeyDown(Keys.S))
{
Direction += new Vector2(0, -1);
}
#endregion
#region SecondaryDirection
SecondaryDirection = currentGamePadState.ThumbSticks.Left;
if (currentKeyboardState.IsKeyDown(Keys.Left))
{
SecondaryDirection += new Vector2(-1, 0);
}
if (currentKeyboardState.IsKeyDown(Keys.Right))
{
SecondaryDirection += new Vector2(1, 0);
}
if (currentKeyboardState.IsKeyDown(Keys.Up))
{
SecondaryDirection += new Vector2(0, 1);
}
if (currentKeyboardState.IsKeyDown(Keys.Down))
{
SecondaryDirection += new Vector2(0, -1);
}
#endregion
#region MenuButton
MenuButton = (currentGamePadState.Buttons.Start == ButtonState.Pressed && previousGamePadState.Buttons.Start == ButtonState.Released) ||
(currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape));
#endregion
#region MouseLocation
MouseLocation = currentMouseState.Position.ToVector2();
#endregion
#region MousePressed
MousePressed = currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released;
#endregion
#region MouseReleased
MouseReleased = currentMouseState.LeftButton == ButtonState.Released && previousMouseState.LeftButton == ButtonState.Pressed;
#endregion
#region MouseHeld
MouseHeld = currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Pressed;
#endregion
#region Jump
bool lastJump = Jump;
Jump = (currentGamePadState.Buttons.A == ButtonState.Pressed) ||
(currentKeyboardState.IsKeyDown(Keys.Space));
#endregion
#region JumpHeld
JumpHeld = lastJump && Jump;
#endregion
}
}
}