-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputManager.cs
67 lines (57 loc) · 2.25 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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace chess
{
public static class InputManager
{
private static MouseState _curMouseState;
private static MouseState _prevMouseState;
private static KeyboardState _curKeyboardState;
private static KeyboardState _prevKeyboardState;
private static Rectangle _mouseRectangle;
public static void Update(GameManager gameManager, GameTime gameTime)
{
HandleMouse(gameManager);
}
private static void HandleMouse(GameManager gameManager)
{
_curMouseState = Mouse.GetState();
_mouseRectangle = new Rectangle(_curMouseState.X, _curMouseState.Y, 1, 1);
Player curPlayer = gameManager.CurrentPlayer;
// If the player hasn't selected a piece
if (!curPlayer.HasSelectedPiece && _curMouseState.LeftButton == ButtonState.Pressed && _prevMouseState.LeftButton == ButtonState.Released)
{
foreach (Tile t in Board.Tiles)
{
if (t.Position.Contains(_mouseRectangle))
{
//Try selecting a piece at tile 't'
if (curPlayer.SelectPiece(t)) break;
}
}
}
//If the player has selected a piece
else if (curPlayer.HasSelectedPiece && _curMouseState.LeftButton == ButtonState.Pressed && _prevMouseState.LeftButton == ButtonState.Released)
{
foreach (Tile t in Board.Tiles)
{
if (t.Position.Contains(_mouseRectangle))
{
//Try moving the player's selected piece to tile 't'. Go to next turn if it was.
if (curPlayer.MovePiece(t, gameManager.OtherPlayer))
{
gameManager.NextTurn();
break;
}
}
}
}
_prevMouseState = _curMouseState;
}
}
}