-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDestroyWall.cs
51 lines (46 loc) · 1.7 KB
/
DestroyWall.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
namespace Advance
{
/// <summary>
/// Represents an action to destroy a wall in the game. Inherits from the Action class.
/// </summary>
internal class DestroyWall : Action
{
private Wall? builtWall;
private Square? actorInitialSquare;
/// <summary>
/// Initializes a new instance of the DestroyWall class.
/// </summary>
/// <param name="actor">The piece that is performing the action.</param>
/// <param name="target">The target square of the action.</param>
public DestroyWall(Piece actor, Square target) : base(actor, target)
{
if (!(actor is Miner)) throw new Exception("Only Miner can destroy a wall.");
actorInitialSquare = actor.Square;
}
/// <summary>
/// Executes the action to destroy a wall.
/// </summary>
/// <returns>True if the action was successful; otherwise, false.</returns>
public override bool DoAction()
{
if (Target.IsFree) throw new Exception("Cannot destroy a wall on an empty square.");
if (!Target.ContainsWall) return false;
builtWall = Target.Occupant as Wall;
builtWall.LeaveBoard();
// Move actor to current square
Actor.MoveTo(Target);
return true;
}
/// <summary>
/// Reverts the action of destroying a wall.
/// </summary>
public override void UndoAction()
{
Actor.MoveTo(actorInitialSquare);
if (Target.IsFree && builtWall != null)
{
builtWall.EnterBoard(Target);
}
}
}
}