comparison api/Objects/Army.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 3fa4658c50c6
comparison
equal deleted inserted replaced
57:e0ce5578e7c2 58:e53ed2d613a1
2 // 2 //
3 // 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. 3 // 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.
4 4
5 using System; 5 using System;
6 using System.IO; 6 using System.IO;
7 using System.Collections;
8 using System.Collections.Generic; 7 using System.Collections.Generic;
9 using System.Text; 8 using System.Text;
10 using System.Xml; 9 using System.Xml;
11 using IBBoard.WarFoundry.API; 10 using IBBoard.WarFoundry.API;
12 using IBBoard.WarFoundry.API.Factories; 11 using IBBoard.WarFoundry.API.Factories;
22 { 21 {
23 //private GameSystem system; 22 //private GameSystem system;
24 private Race armyRace; 23 private Race armyRace;
25 private int maxPoints; 24 private int maxPoints;
26 private double pointsTotal; 25 private double pointsTotal;
27 private ArmyCategory[] categories; 26 private Dictionary<Category, ArmyCategory> categories;
28 27
29 public event ObjectAddDelegate UnitAdded; 28 public event ObjectAddDelegate UnitAdded;
30 public event ObjectRemoveDelegate UnitRemoved; 29 public event ObjectRemoveDelegate UnitRemoved;
31 public event FailedUnitRequirementDelegate FailedRequirement; 30 public event FailedUnitRequirementDelegate FailedRequirement;
32 public event DoubleValChangedDelegate PointsValueChanged; 31 public event DoubleValChangedDelegate PointsValueChanged;
44 PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler); 43 PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler);
45 } 44 }
46 45
47 public ArmyCategory GetCategory(Category cat) 46 public ArmyCategory GetCategory(Category cat)
48 { 47 {
49 foreach (ArmyCategory armyCat in Categories) 48 ArmyCategory armyCat = null;
50 { 49 ArmyCategories.TryGetValue(cat, out armyCat);
51 if (armyCat.Category.Equals(cat)) 50 return armyCat;
52 { 51 }
53 return armyCat; 52
54 } 53 private Dictionary<Category, ArmyCategory> ArmyCategories
55 } 54 {
56 55 get
57 return null;
58 }
59
60 public ArmyCategory[] Categories
61 {
62 get
63 { 56 {
64 if (categories==null) 57 if (categories==null)
65 { 58 {
59 categories = new Dictionary<Category, ArmyCategory>();
66 Category[] raceCats = Race.Categories; 60 Category[] raceCats = Race.Categories;
67 ArmyCategory cat; 61 ArmyCategory cat;
68 int raceCatCount = raceCats.Length; 62 int raceCatCount = raceCats.Length;
69 categories = new ArmyCategory[raceCatCount];
70 63
71 for (int i = 0; i < raceCatCount; i++) 64 for (int i = 0; i < raceCatCount; i++)
72 { 65 {
73 cat = new ArmyCategory(this, raceCats[i]); 66 Category raceCat = raceCats[i];
74 categories[i] = cat; 67 cat = new ArmyCategory(this, raceCat);
68 categories[raceCat] = cat;
75 cat.PointsValueChanged+= PointsValueChangedMethod; 69 cat.PointsValueChanged+= PointsValueChangedMethod;
76 cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded); 70 cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded);
77 cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved); 71 cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved);
78 cat.RequirementsFailed+=new FailedUnitRequirementDelegate(Army_FailedRequirement);
79 } 72 }
80 } 73 }
81 74
82 return categories; 75 return categories;
76 }
77 }
78
79 public ArmyCategory[] Categories
80 {
81 get
82 {
83 return DictionaryUtils.ToArray<Category, ArmyCategory>(ArmyCategories);
83 } 84 }
84 } 85 }
85 86
86 public Race Race 87 public Race Race
87 { 88 {
154 155
155 public double PointsTotal 156 public double PointsTotal
156 { 157 {
157 get { return TotalPoints; } 158 get { return TotalPoints; }
158 } 159 }
160
161 public void AddUnit(Unit unit)
162 {
163 List<FailedUnitRequirement> failedReqs = CanAddUnit(unit);
164 unit.Army = this;
165 ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory);
166 armyCat.AddUnit(unit);
167 OnUnitAdded(unit, failedReqs);
168 }
169
170 public void RemoveUnit(Unit unit)
171 {
172 List<FailedUnitRequirement> failedReqs = CanRemoveUnit(unit);
173 unit.Army = null;
174 ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory);
175 armyCat.RemoveUnit(unit);
176 OnUnitRemoved(unit, failedReqs);
177 }
159 178
160 public Unit[] GetUnits(Category cat) 179 public Unit[] GetUnits(Category cat)
161 { 180 {
162 return GetUnits(this.GetCategory(cat)); 181 return GetUnits(this.GetCategory(cat));
163 } 182 }
167 return cat.GetUnits(); 186 return cat.GetUnits();
168 } 187 }
169 188
170 public Unit[] GetUnits() 189 public Unit[] GetUnits()
171 { 190 {
172 ArrayList fullList = new ArrayList(); 191 List<Unit> fullList = new List<Unit>();
173 192
174 foreach(ArmyCategory cat in Categories) 193 foreach(ArmyCategory cat in Categories)
175 { 194 {
176 foreach(Unit unit in cat.GetUnits()) 195 fullList.AddRange(cat.GetUnits());
177 { 196 }
178 if (!fullList.Contains(unit)) 197
179 { 198 return fullList.ToArray();
180 fullList.Add(unit);
181 }
182 }
183 }
184
185 return (Unit[])fullList.ToArray(typeof(Unit));
186 } 199 }
187 200
188 public int MaxPoints 201 public int MaxPoints
189 { 202 {
190 get { return maxPoints; } 203 get { return maxPoints; }
251 264
252 private void Army_UnitRemoved(object val) 265 private void Army_UnitRemoved(object val)
253 { 266 {
254 OnUnitRemoved((Unit)val); 267 OnUnitRemoved((Unit)val);
255 } 268 }
256
257 private void Army_FailedRequirement(List<FailedUnitRequirement> failedRequirements)
258 {
259 if (FailedRequirement!=null)
260 {
261 FailedRequirement(failedRequirements);
262 }
263 }
264 } 269 }
265 } 270 }