-
Notifications
You must be signed in to change notification settings - Fork 0
/
EffectApplier.cs
87 lines (71 loc) · 2.71 KB
/
EffectApplier.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
namespace SurpriseChess;
public class EffectApplier
{
private readonly Board board;
private readonly Random random = new();
private const float MorphChance = 0.25f;
private const float InvisibilityChance = 0.1f;
private const float ParalysisChance = 0.5f;
private const float ShieldChance = 0.5f;
public EffectApplier(Board board)
{
this.board = board;
}
public void ApplyEffects(Position movedPosition)
{
Piece? movedPiece = board.GetPieceAt(movedPosition);
if (movedPiece == null) return;
ApplyMorphEffect(movedPiece, movedPosition);
ApplyInvisibilityEffect(movedPiece.Color);
ApplyParalysisEffect(ChessUtils.OpponentColor(movedPiece.Color));
ApplyShieldEffect(movedPiece.Color);
}
public void ClearEffects()
{
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
Piece? piece = board.GetPieceAt(new Position(row, col));
if (piece == null) continue;
piece.IsInvisible = false;
piece.IsParalyzed = false;
piece.IsShielded = false;
}
}
}
private void ApplyMorphEffect(Piece movedPiece, Position movedPosition)
{
// Make sure the king doesn't morph
if (movedPiece.Type == PieceType.King || random.NextDouble() > MorphChance) return;
PieceType newType = (PieceType)random.Next(1, 6); // Cannot morph into a king (index 0)
Piece newPiece = PieceFactory.Create(movedPiece.Color, newType);
board.SetPieceAt(movedPosition, newPiece);
}
private void ApplyInvisibilityEffect(PieceColor color)
{
if (random.NextDouble() > InvisibilityChance) return;
Dictionary<Position, Piece> piecesPositions = board.LocatePieces(color);
foreach (Piece piece in piecesPositions.Values)
{
piece.IsInvisible = true;
}
}
private void ApplyParalysisEffect(PieceColor color)
{
if (random.NextDouble() > ParalysisChance) return;
Dictionary<Position, Piece> piecesPositions = board.LocatePieces(color);
Piece randomPiece = GetRandomPiece(piecesPositions);
randomPiece.IsParalyzed = true;
}
private void ApplyShieldEffect(PieceColor color)
{
if (random.NextDouble() > ShieldChance) return;
Dictionary<Position, Piece> piecesPositions = board.LocatePieces(color);
Piece randomPiece = GetRandomPiece(piecesPositions);
randomPiece.IsShielded = true;
}
private Piece GetRandomPiece(Dictionary<Position, Piece> piecesPositions) => (
piecesPositions.Values.ElementAt(random.Next(piecesPositions.Count))
);
}