-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extensions.cs
95 lines (83 loc) · 3.16 KB
/
Extensions.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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Kafe
{
public static class VectorExtensions
{
public static Vector2 ToInteger(this Vector2 vector)
{
return new Vector2((int)vector.X, (int)vector.Y);
}
}
public static class RectangleExtensions
{
public static string ToShort(this Rectangle rect)
{
return string.Format("<{0},{1},{2},{3}>", rect.X, rect.Y, rect.Width, rect.Height);
}
public static bool Intersects(this Rectangle a, Rectangle b, Vector2 originA, Vector2 originB)
{
var aX = a.X + originA.X;
var aY = a.Y + originA.Y;
var bX = b.X + originB.X;
var bY = b.Y + originB.Y;
return (bX < aX + a.Width && aX < bX + b.Width && bY < aY + a.Height && aY < bY + b.Height);
}
}
public static class InputExtensions
{
private static KeyboardState oldKeyState, newKeyState;
private static GamePadDPad oldDPadState, newDPadState;
private static GamePadButtons oldButtonState, newButtonState;
static InputExtensions()
{
oldKeyState = newKeyState = Keyboard.GetState();
var padState = GamePad.GetState(PlayerIndex.One);
oldDPadState = newDPadState = padState.DPad;
oldButtonState = newButtonState = padState.Buttons;
}
public static void Update()
{
oldKeyState = newKeyState;
newKeyState = Keyboard.GetState();
var padState = GamePad.GetState(PlayerIndex.One);
oldDPadState = newDPadState;
newDPadState = padState.DPad;
oldButtonState = newButtonState;
newButtonState = padState.Buttons;
}
public static bool IsDown(this Keys key)
{
return newKeyState.IsKeyDown(key);
}
public static bool IsUp(this Keys key)
{
return newKeyState.IsKeyUp(key);
}
public static bool WasJustPressed(this Keys key)
{
return newKeyState.IsKeyDown(key) && oldKeyState.IsKeyUp(key);
}
public static bool WasJustReleased(this Keys key)
{
return newKeyState.IsKeyUp(key) && oldKeyState.IsKeyDown(key);
}
public static bool WasJustPressed(this Buttons key)
{
switch (key)
{
case Buttons.DPadUp: return newDPadState.Up == ButtonState.Pressed && oldDPadState.Up == ButtonState.Released;
case Buttons.DPadDown: return newDPadState.Down == ButtonState.Pressed && oldDPadState.Down == ButtonState.Released;
case Buttons.DPadLeft: return newDPadState.Left == ButtonState.Pressed && oldDPadState.Left == ButtonState.Released;
case Buttons.DPadRight: return newDPadState.Right == ButtonState.Pressed && oldDPadState.Right == ButtonState.Released;
case Buttons.A: return newButtonState.A == ButtonState.Pressed && oldButtonState.A == ButtonState.Released;
case Buttons.B: return newButtonState.B == ButtonState.Pressed && oldButtonState.B == ButtonState.Released;
case Buttons.X: return newButtonState.X == ButtonState.Pressed && oldButtonState.X == ButtonState.Released;
case Buttons.Y: return newButtonState.Y == ButtonState.Pressed && oldButtonState.Y == ButtonState.Released;
}
return false;
}
}
}