-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputHandler.cs
386 lines (308 loc) · 11.7 KB
/
InputHandler.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using JustCombat.Collision;
using JustCombat.Common;
using JustCombat.Entities;
using JustCombat.UI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
using System;
namespace JustCombat
{
public class InputHandler
{
// Implementation reference:
// https://community.monogame.net/t/one-shot-key-press/11669
private static KeyboardState _currentKeyState;
private static KeyboardState _previousKeyState;
private static MouseState _currentMouseState;
private static MouseState _previousMouseState;
public static void UpdateKeyboardState()
{
_previousKeyState = _currentKeyState;
_currentKeyState = Keyboard.GetState();
}
public static void UpdateMouseState()
{
_previousMouseState = _currentMouseState;
_currentMouseState = Mouse.GetState();
}
public static bool IsKeyPressed(Keys key)
{
return _currentKeyState.IsKeyDown(key) && !(_previousKeyState.IsKeyDown(key));
}
public static bool RightButtonPressed()
{
return (_currentMouseState.RightButton == ButtonState.Pressed) &&
!(_previousMouseState.RightButton == ButtonState.Pressed);
}
private static void TranslateMovement(float dx, float dy, bool isRunning)
{
Player player = Player.Instance();
OrthographicCamera camera = JustCombat.WorldCamera;
Vector2 screenCoords;
int[] newPositions;
int[] testPositions;
bool translateCamera = false;
newPositions = Util.GetNewPosition(dx, dy, isRunning);
screenCoords = camera.WorldToScreen(newPositions[0], newPositions[1]);
testPositions = new int[] { (int)(screenCoords.X), (int)(screenCoords.Y) };
translateCamera = Util.BeyondMapScrollTransformBounds(testPositions, (int)(player.GetWidth()), (int)(player.GetHeight()));
if (translateCamera)
{
int oldX = (int)(player.GetX());
int oldY = (int)(player.GetY());
int newX = newPositions[0];
int newY = newPositions[1];
int diffX = (oldX - newX) * (-1);
int diffY = (oldY - newY) * (-1);
camera.Move(new Vector2(diffX, diffY));
// Update the TransformMatrix, because the camera has moved.
JustCombat.TransformMatrix = camera.GetViewMatrix();
}
player.Move(newPositions, isRunning);
}
public static void HandleInput()
{
UpdateKeyboardState();
UpdateMouseState();
KeyboardState keyboardState = Keyboard.GetState();
MouseState mouseState = Mouse.GetState();
Player player = Player.Instance();
bool isRunning = Keyboard.GetState().IsKeyDown(Keys.LeftShift);
if (IsValidMovementKey(keyboardState))
{
int dx = 0;
int dy = 0;
// North
if (Keyboard.GetState().IsKeyDown(Keys.W))
{
dy = -1;
}
// East
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
dx = 1;
}
// South
if (Keyboard.GetState().IsKeyDown(Keys.S))
{
dy = 1;
}
// West
if (Keyboard.GetState().IsKeyDown(Keys.A))
{
dx = -1;
}
InputHandler.TranslateMovement(dx, dy, isRunning);
}
if (IsKeyPressed(Keys.OemOpenBrackets))
{
player.SetState(Player.ActorState.IN_COMBAT);
}
if (IsKeyPressed(Keys.OemCloseBrackets))
{
player.SetState(Player.ActorState.NORMAL);
}
if (IsKeyPressed(Keys.T))
{
Blink();
}
if (IsKeyPressed(Keys.G))
{
player.RemoveHitPoints(10);
}
if (IsKeyPressed(Keys.H))
{
player.SetHitPoints(player.GetHitPoints() / 2);
}
if (IsKeyPressed(Keys.L))
{
Player.AddLevel();
}
if (IsKeyPressed(Keys.I) && !(UserInterface.InventoryPanel.IsDisplayed()))
{
UserInterface.InventoryPanel.SetDisplayed(true);
}
else if (IsKeyPressed(Keys.I) && UserInterface.InventoryPanel.IsDisplayed())
{
UserInterface.InventoryPanel.SetDisplayed(false);
}
if (IsKeyPressed(Keys.C) && !(UserInterface.CharacterPanel.IsDisplayed()))
{
UserInterface.CharacterPanel.SetDisplayed(true);
}
else if (IsKeyPressed(Keys.C) && UserInterface.CharacterPanel.IsDisplayed())
{
UserInterface.CharacterPanel.SetDisplayed(false);
}
//if (IsKeyPressed(Keys.Q))
//{
// JustCombat.UserInterface.CoolDownTimer.Start(2.5f);
//}
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
JustCombat.TargetingSystem.Release();
}
if (IsKeyPressed(Keys.F5))
{
if (JustCombat.UserInterface.InDebugMode())
{
JustCombat.UserInterface.SetDebug(false);
}
else
{
JustCombat.UserInterface.SetDebug(true);
}
Console.WriteLine("Debug toggled.");
}
if (keyboardState.IsKeyDown(Keys.OemPeriod))
{
OrthographicCamera camera = JustCombat.WorldCamera;
JustCombat.TransformMatrix = camera.GetViewMatrix();
camera.ZoomIn(0.1f);
}
if (keyboardState.IsKeyDown(Keys.OemComma))
{
OrthographicCamera camera = JustCombat.WorldCamera;
JustCombat.TransformMatrix = camera.GetViewMatrix();
camera.ZoomOut(0.1f);
}
if (IsKeyPressed(Keys.OemPlus))
{
Player.Instance().AddXP(300);
}
if (keyboardState.IsKeyDown(Keys.OemCloseBrackets))
{
Player.Instance().AddXP(300);
}
if (RightButtonPressed())
{
Entity entity = TargetingSystem.Instance().GetCurrentTarget();
if (entity != null)
{
if (entity is Actor)
{
Actor victim = (Actor)(entity);
victim.RemoveHitPoints(13);
}
}
}
}
private static void Blink()
{
Player player = Player.Instance();
float heading = player.GetDirection().GetHeading();
float currentX = player.GetX();
float currentY = player.GetY();
float distance = 150.0f;
CollisionBox bounds = UserInterface.ScrollTransformBounds;
OrthographicCamera camera = JustCombat.WorldCamera;
Vector2 worldCoords = camera.ScreenToWorld(bounds.GetPosX(), bounds.GetPosY());
float boundWest = worldCoords.X;
float boundNorth = worldCoords.Y;
float boundEast = boundWest + bounds.GetWidth();
float boundSouth = boundNorth + bounds.GetHeight();
float cameraDiffX = 0;
float cameraDiffY = 0;
// North
if (heading == 0)
{
float newY = (currentY - distance);
float diff = (boundNorth - (newY + player.GetHeight()));
if (diff > 0)
{
cameraDiffY = -(diff);
}
player.Teleport(currentX, newY);
camera.Move(new Vector2(cameraDiffX, cameraDiffY));
}
// East
if (heading == 90)
{
float newX = (currentX + distance);
float diff = (newX - boundEast);
if (diff > 0)
{
cameraDiffX = diff;
}
player.Teleport(newX, currentY);
camera.Move(new Vector2(cameraDiffX, cameraDiffY));
}
// South
if (heading == 180)
{
float newY = (currentY + distance);
float diff = (newY - boundSouth);
if (diff > 0)
{
cameraDiffY = diff;
}
player.Teleport(currentX, newY);
camera.Move(new Vector2(cameraDiffX, cameraDiffY));
}
// West
if (heading == 270)
{
float newX = (currentX - distance);
float diff = (boundWest - (newX + player.GetWidth()));
if (diff > 0)
{
cameraDiffX = -(diff);
}
player.Teleport(newX, currentY);
camera.Move(new Vector2(cameraDiffX, cameraDiffY));
}
JustCombat.TransformMatrix = camera.GetViewMatrix();
}
public static void OnMouseHover()
{
MouseState state = Mouse.GetState();
foreach (Entity entity in JustCombat.EntityContainer)
{
if (entity is Actor)
{
Actor actor = (Actor)(entity);
if (actor.MouseOver(state))
{
if (actor is Player)
{
// Show the select / glove cursor...
UserInterface.Cursor = UserInterface.CursorList[0];
}
else
{
// Show the attack / sword cursor...
UserInterface.Cursor = UserInterface.CursorList[1];
}
JustCombat.UserInterface.GetCursorInfoPanel().SetText(actor.ToString());
JustCombat.UserInterface.GetCursorInfoPanel().SetDisplayed(true);
if (state.LeftButton == ButtonState.Pressed)
{
JustCombat.TargetingSystem.Acquire(actor);
break;
}
return;
}
else
{
// Show the select / glove cursor...
UserInterface.Cursor = UserInterface.CursorList[0];
JustCombat.UserInterface.GetCursorInfoPanel().SetText(string.Empty);
JustCombat.UserInterface.GetCursorInfoPanel().SetDisplayed(false);
if (state.LeftButton == ButtonState.Pressed)
{
JustCombat.TargetingSystem.Release();
}
}
}
}
}
public static bool IsValidMovementKey(KeyboardState state)
{
return (state.IsKeyDown(Keys.W) ||
state.IsKeyDown(Keys.A) ||
state.IsKeyDown(Keys.S) ||
state.IsKeyDown(Keys.D));
}
}
}