-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.cs
123 lines (106 loc) · 3.47 KB
/
Piece.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace chess
{
/// <summary>
/// An abstract class describing a chess piece.
/// </summary>
public abstract class Piece
{
protected int id;
protected int value;
protected (int, int) tile; //Coordinate of current tile
protected Vector2 boardPos; //The "real" position (used to move/draw tile)
protected (int, int) prevTile; //The tile where this piece was previously standing
protected List<(int, int)> possibleMoves; //The tiles this piece can move to
protected Texture2D texture;
protected PieceColor color;
protected string name;
protected string notation;
public Piece(Texture2D texture, PieceColor color, (int, int) pos)
{
this.color = color;
this.texture = texture;
Move(pos);
possibleMoves = new List<(int, int)>();
}
/// <summary>
/// Update this piece.
/// </summary>
public void Update(GameTime gameTime)
{
if (tile != prevTile)
{
possibleMoves = FindPossibleMoves();
}
}
/// <summary>
/// Draw this piece.
/// </summary>
/// <param name="spriteBatch"></param>
public void Draw(SpriteBatch spriteBatch)
{
//TODO: Implement usage of PieceColor
spriteBatch.Draw(texture, boardPos, Color.White);
}
/// <summary>
/// Move this piece to a specified tile, if possible.
/// Does nothing otherwise.
/// </summary>
/// <param name="pos">The tile coordinate to move to.</param>
/// <returns>true if move was made, false otherwise.</returns>
public bool Move((int, int) pos)
{
if (possibleMoves == null) //Collection is always null when the piece spawns for the first time
{
SetBoardPosition(pos);
return true;
}
else if (possibleMoves.Contains(pos))
{
SetBoardPosition(pos);
return true;
}
return false;
}
/// <summary>
/// Set the Vector2 position of this piece. ("Real" position)
/// </summary>
private void SetBoardPosition((int, int) pos)
{
tile = pos;
Rectangle t = Board.GetTileAtIndex(pos).Position; //Get the position of this tile
boardPos = new Vector2(t.Center.X - texture.Width / 2, t.Center.Y - texture.Height / 2);
}
/// <summary>
/// Calculate which tiles this piece can move to.
/// </summary>
/// <returns>A collection of possible tiles. Is empty if no moves can be made.</returns>
abstract protected List<(int, int)> FindPossibleMoves();
public Vector2 Pos
{
get => boardPos;
}
public (int, int) TileCoordinate
{
get => tile;
}
public List<(int, int)> PossibleMoves
{
get => possibleMoves;
}
public int Value
{
get => value;
}
}
public struct PieceColor
{
public PieceColor(string color)
{
Color = color;
}
public string Color { get; private set; }
}
}