changeset 473:0d032c04210e

Re #359: Add "only contained" attribute to unit types * Make sure remove handles sub-units as well Note: warning the user that the unit has sub-units rather than just removing them is left to the UI
author IBBoard <dev@ibboard.co.uk>
date Mon, 16 Apr 2012 20:47:14 +0100
parents e6c93ceba119
children e47cb422aac1
files API/Commands/RemoveUnitCommand.cs
diffstat 1 files changed, 28 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/API/Commands/RemoveUnitCommand.cs	Mon Apr 16 20:14:23 2012 +0100
     1.2 +++ b/API/Commands/RemoveUnitCommand.cs	Mon Apr 16 20:47:14 2012 +0100
     1.3 @@ -6,6 +6,7 @@
     1.4  using IBBoard.Commands;
     1.5  using IBBoard.Lang;
     1.6  using IBBoard.WarFoundry.API.Objects;
     1.7 +using System.Collections.Generic;
     1.8  
     1.9  namespace IBBoard.WarFoundry.API.Commands
    1.10  {
    1.11 @@ -15,12 +16,15 @@
    1.12  	public class RemoveUnitCommand : Command
    1.13  	{
    1.14  		private Unit unit;
    1.15 +		private Unit parentUnit;
    1.16  		private ArmyCategory cat;
    1.17 +		private List<RemoveUnitCommand> subCommands;
    1.18  
    1.19  		public RemoveUnitCommand(Unit toRemove)
    1.20  		{
    1.21  			unit = toRemove;
    1.22  			cat = unit.Category;
    1.23 +			parentUnit = unit.ParentUnit;
    1.24  		}
    1.25  
    1.26  		public override bool CanExecute()
    1.27 @@ -44,20 +48,43 @@
    1.28  			}
    1.29  		}
    1.30  
    1.31 +		private void Prepare()
    1.32 +		{
    1.33 +			subCommands = new List<RemoveUnitCommand>();
    1.34 +			foreach (Unit containedUnit in unit.ContainedUnits)
    1.35 +			{
    1.36 +				RemoveUnitCommand removeUnitCommand = new RemoveUnitCommand(containedUnit);
    1.37 +				removeUnitCommand.Prepare();
    1.38 +				subCommands.Add(removeUnitCommand);
    1.39 +			}
    1.40 +		}
    1.41  		public override bool Execute()
    1.42  		{
    1.43 -			this.Redo();
    1.44 +			Prepare();
    1.45 +			Redo();
    1.46  			return true;
    1.47  		}
    1.48  
    1.49  		public override void Redo()
    1.50  		{
    1.51 +			unit.ParentUnit = null;
    1.52  			cat.RemoveUnit(unit);
    1.53 +
    1.54 +			foreach (RemoveUnitCommand subCommand in subCommands)
    1.55 +			{
    1.56 +				subCommand.Redo();
    1.57 +			}
    1.58  		}
    1.59  
    1.60  		public override void Undo()
    1.61  		{
    1.62 +			unit.ParentUnit = parentUnit;
    1.63  			cat.AddUnit(unit);
    1.64 +
    1.65 +			foreach (RemoveUnitCommand subCommand in subCommands)
    1.66 +			{
    1.67 +				subCommand.Undo();
    1.68 +			}
    1.69  		}
    1.70  
    1.71  		public override string Name