view api/Commands/ReplaceUnitEquipmentCommand.cs @ 9:6ad505b6c36e

Re #10 - Refactor for readability * Refactor out repeated number parsing in to a separate method
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Jan 2009 20:06:28 +0000
parents 520818033bb6
children 306558904c2a
line wrap: on
line source

using System;
using IBBoard.Commands;
using IBBoard.Lang;
using IBBoard.WarFoundry.API.Objects;

namespace IBBoard.WarFoundry.API.Commands
{
	/// <summary>
	/// Summary description for ReplaceUnitEquipmentCommand.
	/// </summary>
	public class ReplaceUnitEquipmentCommand : Command
	{
		private SetUnitEquipmentAmountCommand removeOldCommand, addNewCommand;
		//private Unit unit;
		//private EquipmentItem oldEquip, newEquip;
		//private float newAmount, oldAmount;
		
		public ReplaceUnitEquipmentCommand(Unit unit, EquipmentItem oldItem, EquipmentItem newItem, float amount)
		{
			removeOldCommand = new SetUnitEquipmentAmountCommand(unit, oldItem, 0);
			addNewCommand = new SetUnitEquipmentAmountCommand(unit, newItem, amount);
		}

		public override bool CanExecute()
		{
			return removeOldCommand.CanExecute() && addNewCommand.CanExecute();
		}

		public override string Description
		{
			get { return "Replace "+StringManipulation.CutToLength(removeOldCommand.EquipItem.Name, 20)+" with "+StringManipulation.CutToLength(addNewCommand.EquipItem.Name, 20)+ " for "+StringManipulation.CutToLength(removeOldCommand.Unit.Name, 20); }
		}

		public override string UndoDescription
		{
			get { return "Replace "+StringManipulation.CutToLength(addNewCommand.EquipItem.Name, 20)+" with "+StringManipulation.CutToLength(removeOldCommand.EquipItem.Name, 20)+ " for "+StringManipulation.CutToLength(removeOldCommand.Unit.Name, 20); }
		}

		public override bool Execute()
		{
			this.Redo();
			return true;
		}

		public override void Redo()
		{
			removeOldCommand.Redo();
			addNewCommand.Redo();
		}

		public override void Undo()
		{
			addNewCommand.Undo();
			removeOldCommand.Undo();
		}


		public override string Name
		{
			get { return "Replace required equipment"; }
		}
	}
}