view API/Objects/ArmyCategory.cs @ 497:cd367acd7c48 default tip

Re #419: Remove assumptions of a file-based install * Swap XSLTs to resources * Update XSL Exporter to use Streams, not files
author IBBoard <dev@ibboard.co.uk>
date Wed, 28 Nov 2012 20:24:36 +0000
parents aac77204ae0e
children
line wrap: on
line source

// This file (ArmyCategory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 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;
using System.Collections.Generic;

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Summary description for ArmyCategory.
	/// </summary>
	public class ArmyCategory : WarFoundryObject, ICostedWarFoundryObject
	{
		private Category category;
		private Army parentArmy;
		private double pointsTotal;
		private List<Unit> units;
		private Dictionary<string, int> unitTypes;
		private DoubleValChangedDelegate PointsValueChangedMethod;
		public event ObjectAddDelegate UnitAdded;
		public event ObjectRemoveDelegate UnitRemoved;
		public event DoubleValChangedDelegate PointsValueChanged;

		public ArmyCategory(Army army, Category cat) : base()
		{
			parentArmy = army;
			category = cat;
			cat.NameChanged += new StringValChangedDelegate(cat_NameChanged);
			PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler);
			units = new List<Unit>();
			unitTypes = new Dictionary<string,int>();
		}

		public Category Category
		{
			get { return category; }
		}

		public Army ParentArmy
		{
			get { return parentArmy; }
		}

		public override string ID
		{
			get
			{
				return Category.ID;
			}
			set
			{
				Category.ID = value;
			}
		}

		public override string Name
		{
			get { return category.Name; }
			set
			{
				category.Name = value;
			}
		}

		internal void AddUnit(Unit unit)
		{
			//TODO: Put back a similar check
			//List<FailedUnitRequirement> failedReqs = ParentArmy.CanAddUnit(unit);
			units.Add(unit);
			unit.Category = this;
			unit.PointsValueChanged += PointsValueChangedMethod;
			int unitTypeCount;
			unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount);
			unitTypes[unit.UnitType.ID] = unitTypeCount + 1;
			TotalPoints += unit.Points;
			OnUnitAdded(unit);
		}

		internal void RemoveUnit(Unit unit)
		{
			//TODO: Put back a similar check
			//List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit);
			units.Remove(unit);
			unitTypes[unit.UnitType.ID] = unitTypes[unit.UnitType.ID] - 1;
			TotalPoints -= unit.Points;
			unit.PointsValueChanged -= PointsValueChangedMethod;
			OnUnitRemoved(unit);
		}

		public int GetUnitTypeCount(UnitType unitType)
		{
			return unitTypes.ContainsKey(unitType.ID) ? unitTypes[unitType.ID] : 0;
		}

		public Unit[] GetUnits()
		{
			return units.ToArray();
		}

		public Unit[] GetUnits(UnitType unitType)
		{
			List<Unit> filteredUnits = new List<Unit>();
			foreach (Unit unit in units)
			{
				if (unit.UnitType.Equals(unitType))
				{
					filteredUnits.Add(unit);
				}
			}
			return filteredUnits.ToArray();
		}

		public Unit[] GetMainUnits()
		{
			List<Unit> mainUnits = new List<Unit>();

			foreach (Unit unit in units)
			{
				if (unit.ParentUnit == null)
				{
					mainUnits.Add(unit);
				}
			}

			return mainUnits.ToArray();
		}

		private double TotalPoints
		{
			get { return pointsTotal; }
			set
			{
				double oldVal = pointsTotal;
				pointsTotal = value;

				if (oldVal != pointsTotal)
				{
					OnPointsValueChanged(oldVal, pointsTotal);
				}
			}
		}

		public double Points
		{
			get { return TotalPoints; }
		}

		private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
		{
			if (obj is Unit)
			{
				double diff = newVal - oldVal;
				TotalPoints += diff;
			}
		}

		protected void OnUnitAdded(Unit unit)
		{
			if (UnitAdded != null)
			{
				UnitAdded(unit);
			}
		}

		protected void OnUnitRemoved(Unit unit)
		{
			if (UnitRemoved != null)
			{
				UnitRemoved(unit);
			}
		}

		protected virtual void OnPointsValueChanged(double oldValue, double newValue)
		{
			if (PointsValueChanged != null)
			{
				PointsValueChanged(this, oldValue, newValue);
			}
		}

		protected void cat_NameChanged(WarFoundryObject obj, string oldValue, string newValue)
		{
			OnNameChanged(oldValue, newValue);
		}
				
		public int GetPointsPercentage()
		{
			return (int)Math.Round((Points / ParentArmy.MaxPoints) * 100, 0);
		}
	}
}