changeset 178:ca2cd24f6872

Re #198: Add slots with counts to units * Add storing of selections by slot on add and removing on remove * Add method to get total number of items in a slot
author IBBoard <dev@ibboard.co.uk>
date Sat, 24 Oct 2009 14:01:50 +0000
parents 948e90426a8d
children c1caf467dd40
files api/Objects/Unit.cs
diffstat 1 files changed, 54 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/api/Objects/Unit.cs	Fri Oct 23 19:55:54 2009 +0000
+++ b/api/Objects/Unit.cs	Sat Oct 24 14:01:50 2009 +0000
@@ -22,6 +22,7 @@
 		private double points;
 		private ArmyCategory cat;
 		private Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection> equipment = new Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection>();
+		private Dictionary<string, List<AbstractUnitEquipmentItemSelection>> equipmentSlots = new Dictionary<string, List<AbstractUnitEquipmentItemSelection>>();
 		private List<Unit> containedUnits = new List<Unit>();
 		public event DoubleValChangedDelegate PointsValueChanged;
 		public event IntValChangedDelegate UnitSizeChanged;
@@ -359,6 +360,17 @@
 			}
 			
 			equipment[equip] = newItem;
+			List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName);
+			
+			if (selections == null)
+			{
+				selections = new List<AbstractUnitEquipmentItemSelection>();
+				equipmentSlots[equip.SlotName] = selections;
+			}
+			
+			Console.WriteLine ("Sels: "+selections.Count);
+			
+			selections.Add(newItem);
 		}
 		
 		public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
@@ -409,7 +421,17 @@
 		
 		private void AddEquipmentRatio(UnitEquipmentItem equip, double ratio)
 		{
-			equipment[equip] = new UnitEquipmentRatioSelection(this, equip, ratio);
+			UnitEquipmentRatioSelection newItem = new UnitEquipmentRatioSelection (this, equip, ratio);
+			equipment[equip] = newItem;
+			List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName);
+			
+			if (selections == null)
+			{
+				selections = new List<AbstractUnitEquipmentItemSelection>();
+				equipmentSlots[equip.SlotName] = selections;
+			}
+			
+			selections.Add(newItem);
 		}
 		
 		private void RemoveEquipmentItem(UnitEquipmentItem equip)
@@ -418,7 +440,10 @@
 		
 			if (oldAmount != 0)
 			{
+				AbstractUnitEquipmentItemSelection selection = DictionaryUtils.GetValue (equipment, equip);
 				equipment.Remove(equip);
+				List<AbstractUnitEquipmentItemSelection> slotSelections = DictionaryUtils.GetValue (equipmentSlots, equip.SlotName);
+				slotSelections.Remove(selection);
 				OnUnitEquipmentAmountChanged(equip, oldAmount, 0);
 			}
 		}
@@ -489,5 +514,33 @@
 		{
 			return UnitType.GetStatValue(statName);
 		}
+
+		public int GetEquipmentAmountInSlot (string slotName)
+		{
+			int amount = 0;
+			
+			List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, slotName);
+			
+			if (selections != null)
+			{
+				amount = GetSelectionTotal(selections);
+			}			
+			
+			return amount;
+		}
+
+		private int GetSelectionTotal (List<AbstractUnitEquipmentItemSelection> selections)
+		{
+			int amount = 0;
+			
+			foreach (AbstractUnitEquipmentItemSelection selection in selections)
+			{
+				Console.WriteLine ("Sel: "+selection.NumberTaken);
+				amount+= selection.NumberTaken;
+			}
+			
+			return amount;
+		}
+
 	}
 }