diff api/Objects/ArmyCategory.cs @ 83:89cc29b4c012

Re #90: Stop new units showing up twice * Hand all of unit adding/removing down to category * Refactor out OnFailedRequirement method in Army * Make Army listen to and propogate FailedRequirement events from Units * Add OnUnitAdded/Removed method to ArmyCategory that takes list of failures * Remove direct reference to Army from Unit and go via ArmyCategory instead
author IBBoard <dev@ibboard.co.uk>
date Sat, 27 Jun 2009 19:39:04 +0000
parents 3ea0ab04352b
children cb3759c3ea19
line wrap: on
line diff
--- a/api/Objects/ArmyCategory.cs	Sat Jun 27 18:59:49 2009 +0000
+++ b/api/Objects/ArmyCategory.cs	Sat Jun 27 19:39:04 2009 +0000
@@ -21,7 +21,8 @@
 		private DoubleValChangedDelegate PointsValueChangedMethod;
 		public event DoubleValChangedDelegate PointsValueChanged;
 		public event ObjectAddDelegate UnitAdded;
-		public event ObjectRemoveDelegate UnitRemoved;
+		public event ObjectRemoveDelegate UnitRemoved;
+		public event FailedUnitRequirementDelegate FailedRequirement;
 
 		public ArmyCategory(Army army, Category cat) : base()
 		{
@@ -74,16 +75,17 @@
 			unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount);
 			unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1;
 			TotalPoints+= unit.PointsValue;
-			OnUnitAdded(unit);
+			OnUnitAdded(unit, failedReqs);
 		}
 
 		internal void RemoveUnit(Unit unit)
 		{
+			List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit);
 			units.Remove(unit);
 			unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1;
 			TotalPoints-= unit.PointsValue;
 			unit.PointsValueChanged-= PointsValueChangedMethod;
-			OnUnitRemoved(unit);
+			OnUnitRemoved(unit, failedReqs);
 		}
 
 		public int GetUnitTypeCount(UnitType unitType)
@@ -122,23 +124,43 @@
 			{
 				double diff = newVal - oldVal;
 				TotalPoints+= diff;
-			}
-		}
-
-		private void OnUnitAdded(Unit unit)
-		{
-			if (UnitAdded!=null)
-			{
-				UnitAdded(unit);
-			}
-		}
-
-		private void OnUnitRemoved(Unit unit)
-		{
-			if (UnitRemoved!=null)
-			{
-				UnitRemoved(unit);
-			}
+			}
+		}
+
+		protected void OnUnitAdded(Unit unit)
+		{
+			OnUnitAdded(unit, null);
+		}
+
+		protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs)
+		{
+			if (UnitAdded != null)
+			{
+				UnitAdded(unit);
+			}
+
+			if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0)
+			{
+				FailedRequirement(failedReqs);
+			}
+		}
+
+		protected void OnUnitRemoved(Unit unit)
+		{
+			OnUnitRemoved(unit, null);
+		}
+
+		protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs)
+		{
+			if (UnitRemoved != null)
+			{
+				UnitRemoved(unit);
+			}
+
+			if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0)
+			{
+				FailedRequirement(failedReqs);
+			}
 		}
 
 		protected virtual void OnPointsValueChanged(double oldValue, double newValue)