view api/Objects/UnitEquipmentItemObj.cs @ 41:422ddd5fedd1

Re #48 - Require schemas to validate * Restructure schema caching so that it warns instead of exceptioning - allows a missing schema to not kill an unrelated file load * Fix exception handling for GameSystems while working out where exceptions were being caught Current situation: Missing Race/System schema stops file loading but missing cat/core schema doesn't
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Mar 2009 20:52:26 +0000
parents 548cfc776f54
children
line wrap: on
line source

// This file (UnitEquipmentItemObj.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 under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.

using System;
using System.Collections;

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Summary description for UnitEquipmentItemObj.
	/// </summary>
	public class UnitEquipmentItemObj
	{
		private Unit unit;
		private UnitEquipmentItem item;
		private EquipmentItem equip;

		private static Hashtable equipObjs = new Hashtable();

		private UnitEquipmentItemObj(Unit unit, UnitEquipmentItem unitItem)
		{
			this.unit = unit;
			item = unitItem;
			equip = item.EquipmentItem;
		}

		public static UnitEquipmentItemObj GetEquipObj(Unit unit, UnitEquipmentItem unitItem)
		{
			if (!equipObjs.ContainsKey(unitItem))
			{
				equipObjs[unitItem] = new UnitEquipmentItemObj(unit, unitItem);
			}
			
			return (UnitEquipmentItemObj)equipObjs[unitItem];
		}

		public override string ToString()
		{
			return String.Format("{0} (For {1} at {2}pts each)", equip.Name, FormatEquipmentAmount(unit, equip), equip.Cost);
		}

		public UnitEquipmentItem Item
		{
			get { return item; }
		}

		public static string FormatEquipmentAmount(Unit unit, EquipmentItem equip)
		{
			return FormatEquipmentAmount(equip, unit.GetEquipmentAmount(equip.ID));
		}

		public static string FormatEquipmentAmount(EquipmentItem equip, float amount)
		{
			if (equip.IsRatioLimit)
			{
				return Math.Round(amount * 100) + "%";
			}
			else
			{
				if (amount == WarFoundryCore.INFINITY)
				{
					return "all";
				}
				else
				{
					return amount.ToString();
				}
			}
		}
	}
}