comparison api/Objects/ArmyCategory.cs @ 58:e53ed2d613a1

Re #61 - Complete structure of WarFoundry API objects * Migrate AddUnit/RemoveUnit methods to Army for easier army loading * Migrate requirement checking call to Army, since ArmyCategory just called parent army anyway Also: * Use genericed collections in Army * Remove failed unit requirements from ArmyCategory * Alter army.xsd to stop negatives in equipment amounts
author IBBoard <dev@ibboard.co.uk>
date Tue, 07 Apr 2009 15:28:06 +0000
parents 306558904c2a
children 3ea0ab04352b
comparison
equal deleted inserted replaced
57:e0ce5578e7c2 58:e53ed2d613a1
20 private Dictionary<string, int> unitTypes; 20 private Dictionary<string, int> unitTypes;
21 private DoubleValChangedDelegate PointsValueChangedMethod; 21 private DoubleValChangedDelegate PointsValueChangedMethod;
22 public event DoubleValChangedDelegate PointsValueChanged; 22 public event DoubleValChangedDelegate PointsValueChanged;
23 public event ObjectAddDelegate UnitAdded; 23 public event ObjectAddDelegate UnitAdded;
24 public event ObjectRemoveDelegate UnitRemoved; 24 public event ObjectRemoveDelegate UnitRemoved;
25 public event FailedUnitRequirementDelegate RequirementsFailed;
26 25
27 public ArmyCategory(Army army, Category cat) : base() 26 public ArmyCategory(Army army, Category cat) : base()
28 { 27 {
29 parentArmy = army; 28 parentArmy = army;
30 category = cat; 29 category = cat;
63 { 62 {
64 category.Name = value; 63 category.Name = value;
65 } 64 }
66 } 65 }
67 66
68 public void AddUnit(Unit unit) 67 internal void AddUnit(Unit unit)
69 { 68 {
70 List<FailedUnitRequirement> failedReqs = ParentArmy.CanAddUnit(unit);
71
72 units.Add(unit); 69 units.Add(unit);
73 unit.Army = ParentArmy;
74 unit.Category = this; 70 unit.Category = this;
75 unit.PointsValueChanged+= PointsValueChangedMethod; 71 unit.PointsValueChanged+= PointsValueChangedMethod;
76 int unitTypeCount; 72 int unitTypeCount;
77 unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount); 73 unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount);
78 unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1; 74 unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1;
79 TotalPoints+= unit.PointsValue; 75 TotalPoints+= unit.PointsValue;
80 OnUnitAdded(unit, failedReqs); 76 OnUnitAdded(unit);
81 } 77 }
82 78
83 public void RemoveUnit(Unit unit) 79 internal void RemoveUnit(Unit unit)
84 { 80 {
85 List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit);
86 units.Remove(unit); 81 units.Remove(unit);
87 unit.Army = null;
88 unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1; 82 unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1;
89 TotalPoints-= unit.PointsValue; 83 TotalPoints-= unit.PointsValue;
90 unit.PointsValueChanged-= PointsValueChangedMethod; 84 unit.PointsValueChanged-= PointsValueChangedMethod;
91 OnUnitRemoved(unit, failedReqs); 85 OnUnitRemoved(unit);
92 } 86 }
93 87
94 public int GetUnitTypeCount(UnitType unitType) 88 public int GetUnitTypeCount(UnitType unitType)
95 { 89 {
96 return unitTypes.ContainsKey(unitType.ID) ? (int)unitTypes[unitType.ID] : 0; 90 return unitTypes.ContainsKey(unitType.ID) ? (int)unitTypes[unitType.ID] : 0;
128 double diff = newVal - oldVal; 122 double diff = newVal - oldVal;
129 TotalPoints+= diff; 123 TotalPoints+= diff;
130 } 124 }
131 } 125 }
132 126
133 private void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) 127 private void OnUnitAdded(Unit unit)
134 { 128 {
135 if (UnitAdded!=null) 129 if (UnitAdded!=null)
136 { 130 {
137 UnitAdded(unit); 131 UnitAdded(unit);
138 } 132 }
139
140 if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count > 0)
141 {
142 RequirementsFailed(failedReqs);
143 }
144 } 133 }
145 134
146 private void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) 135 private void OnUnitRemoved(Unit unit)
147 { 136 {
148 if (UnitRemoved!=null) 137 if (UnitRemoved!=null)
149 { 138 {
150 UnitRemoved(unit); 139 UnitRemoved(unit);
151 }
152
153 if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count>0)
154 {
155 RequirementsFailed(failedReqs);
156 } 140 }
157 } 141 }
158 142
159 protected virtual void OnPointsValueChanged(double oldValue, double newValue) 143 protected virtual void OnPointsValueChanged(double oldValue, double newValue)
160 { 144 {