-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawableHand.cs
66 lines (55 loc) · 2.54 KB
/
DrawableHand.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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace ShogiClient
{
/// <summary>
/// An object that contains properties and methods used to be able to draw the hand of a player. Max 19 items.
/// </summary>
public class DrawableHand
{
public PlayerData PlayerData { get; init; }
public Vector2 Position { get; set; }
public Vector2 Scale { get; set; } = Vector2.One;
public Vector2 Size => GetTileOffsetFor(19) + TileSize;
public Vector2 TileSize => resources.Tile.Bounds.Size.ToVector2() * Scale;
public Vector2 GetTileOffsetFor(int x) => new Vector2((TileSize.X + 1) * x, 0);
private GameResources resources;
public DrawableHand(GameResources resources)
{
this.resources = resources;
}
public Point GetIndexForCoordinate(Vector2 position)
{
var topLeftPlayerHand = Position - Size / 2;
var positionOnHand = position - topLeftPlayerHand;
int x = (int)Math.Floor(positionOnHand.X / TileSize.X);
int y = (int)Math.Floor(positionOnHand.Y / TileSize.Y);
return new Point(x, y);
}
public void Draw(SpriteBatch spriteBatch)
{
for (int x = 0; x < 20; x++)
{
var tilePosition = Position - Size / 2 + GetTileOffsetFor(x);
spriteBatch.Draw(resources.Tile, tilePosition, null, Color.White, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
if (PlayerData.Hand.Count > x)
{
PieceType type = PlayerData.Hand[x];
// The second parameter (isPlayerOne) is used to get the jewel king,
// since you can't capture the king it doesn't matter what the value is.
// The third parameter (promoted) is used to get promoted character.
// since captured pieces automatically demote, it should be set to false.
var piecePrint = Utils.PieceToKanji(new PieceData { Type = type, Promoted = false });
// Can not scale the string as the character will become ugly
spriteBatch.DrawString(
resources.PieceFont,
piecePrint,
tilePosition - resources.PieceFont.MeasureString(piecePrint) / 2 + resources.Tile.Bounds.Size.ToVector2() * Scale / 2,
Color.Black
);
}
}
}
}
}