view api/Objects/UnitEquipmentNumericSelection.cs @ 140:678054e2bc02 WarFoundry_v0.1beta_Winforms

* Change assembly number to 0.0.1.* because we've not reached 0.1 yet, never mind 1.0! no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 13 Sep 2009 19:04:13 +0000
parents 2f3cafb69799
children 36adabb1c3ea
line wrap: on
line source

// This file (UnitEquipmentNumericSelection.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
// 
// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.

using System;

namespace IBBoard.WarFoundry.API.Objects
{	
	/// <summary>
	/// An object to hold the selection of a unit's equipment where the selection was made as an absolute number
	/// </summary>
	public class UnitEquipmentNumericSelection : AbstractUnitEquipmentItemSelection
	{	
		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount)
		{
		}
		
		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, item.MinNumber)
		{
		}
					
		public override int NumberTaken
		{
			get
			{
				return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : (int) AmountTaken);
			}
		}
		
		protected bool IsWholeNumber(double newValue)
		{
			return newValue == Math.Round(newValue);
		}
		
		protected override bool IsInRange(double newValue)
		{
			bool isInRange = IsWholeNumber(newValue);
			
			if (newValue == WarFoundryCore.INFINITY)
			{
				isInRange = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY);
			}
			else if (isInRange)
			{
				int minLimit = (EquipmentItem.MinNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MinNumber);
				int maxLimit = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MaxNumber);
				isInRange = (minLimit <= newValue) && (newValue <= maxLimit);
			}
			
			return isInRange;
		}

		public override string GetEquipmentAmountString ()
		{
			return GetEquipmentAmountString(AmountTaken);
		}

		/// <summary>
		/// Gets a string representing the amount of equipment taken when the amount is a numeric selection
		/// </summary>
		/// <param name="amount">
		/// A <see cref="System.Double"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		public static string GetEquipmentAmountString(double amount)
		{
			string amountString;
			
			if (amount == WarFoundryCore.INFINITY)
			{
				amountString = "all"; //TODO: Translate
			}
			else
			{
				amountString = amount.ToString();
			}

			return amountString;
		}
	}
}