Mercurial > repos > IBBoard.WarFoundry.API
annotate 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 |
rev | line source |
---|---|
15 | 1 // This file (Army.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. |
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. | |
4 | |
0 | 5 using System; |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
6 using System.IO; |
0 | 7 using System.Collections.Generic; |
8 using System.Text; | |
9 using System.Xml; | |
10 using IBBoard.WarFoundry.API; | |
11 using IBBoard.WarFoundry.API.Factories; | |
12 using IBBoard.WarFoundry.API.Requirements; | |
13 using ICSharpCode.SharpZipLib.Zip; | |
14 | |
15 namespace IBBoard.WarFoundry.API.Objects | |
16 { | |
17 /// <summary> | |
18 /// Summary description for Army. | |
19 /// </summary> | |
20 public class Army : WarFoundryObject | |
21 { | |
22 //private GameSystem system; | |
23 private Race armyRace; | |
24 private int maxPoints; | |
25 private double pointsTotal; | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
26 private Dictionary<Category, ArmyCategory> categories; |
0 | 27 |
28 public event ObjectAddDelegate UnitAdded; | |
29 public event ObjectRemoveDelegate UnitRemoved; | |
30 public event FailedUnitRequirementDelegate FailedRequirement; | |
31 public event DoubleValChangedDelegate PointsValueChanged; | |
32 private DoubleValChangedDelegate PointsValueChangedMethod; | |
33 | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
34 public Army(Race race, string armyName, int maxArmyPoints) : this(race, armyName, maxArmyPoints, null/*, factory*/) |
0 | 35 { |
36 } | |
37 | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
38 public Army(Race race, string armyName, int maxArmyPoints, ZipFile file) : base(armyName) |
0 | 39 { |
40 armyRace = race; | |
41 Name = armyName; | |
42 maxPoints = maxArmyPoints; | |
43 PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler); | |
44 } | |
45 | |
46 public ArmyCategory GetCategory(Category cat) | |
47 { | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
48 ArmyCategory armyCat = null; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
49 ArmyCategories.TryGetValue(cat, out armyCat); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
50 return armyCat; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
51 } |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
52 |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
53 private Dictionary<Category, ArmyCategory> ArmyCategories |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
54 { |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
55 get |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
56 { |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
57 if (categories==null) |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
58 { |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
59 categories = new Dictionary<Category, ArmyCategory>(); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
60 Category[] raceCats = Race.Categories; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
61 ArmyCategory cat; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
62 int raceCatCount = raceCats.Length; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
63 |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
64 for (int i = 0; i < raceCatCount; i++) |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
65 { |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
66 Category raceCat = raceCats[i]; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
67 cat = new ArmyCategory(this, raceCat); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
68 categories[raceCat] = cat; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
69 cat.PointsValueChanged+= PointsValueChangedMethod; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
70 cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
71 cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
72 } |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
73 } |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
74 |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
75 return categories; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
76 } |
0 | 77 } |
78 | |
79 public ArmyCategory[] Categories | |
80 { | |
81 get | |
82 { | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
83 return DictionaryUtils.ToArray<Category, ArmyCategory>(ArmyCategories); |
0 | 84 } |
85 } | |
86 | |
87 public Race Race | |
88 { | |
89 get { return armyRace; } | |
90 } | |
91 | |
92 public GameSystem GameSystem | |
93 { | |
94 get { return (armyRace!=null ? armyRace.GameSystem : null); } | |
95 } | |
96 | |
97 protected void OnUnitAdded(Unit unit) | |
98 { | |
99 OnUnitAdded(unit, null); | |
100 } | |
101 | |
102 protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) | |
103 { | |
104 if (UnitAdded!=null) | |
105 { | |
106 UnitAdded(unit); | |
107 } | |
108 | |
109 if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0) | |
110 { | |
111 FailedRequirement(failedReqs); | |
112 } | |
113 } | |
114 | |
115 protected void OnUnitRemoved(Unit unit) | |
116 { | |
117 OnUnitRemoved(unit, null); | |
118 } | |
119 | |
120 protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) | |
121 { | |
122 if (UnitRemoved!=null) | |
123 { | |
124 UnitRemoved(unit); | |
125 } | |
126 | |
127 if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0) | |
128 { | |
129 FailedRequirement(failedReqs); | |
130 } | |
131 } | |
132 | |
133 private void OnPointsValueChanged(double oldValue, double newValue) | |
134 { | |
135 if (PointsValueChanged!=null) | |
136 { | |
137 PointsValueChanged(this, oldValue, newValue); | |
138 } | |
139 } | |
140 | |
141 private double TotalPoints | |
142 { | |
143 get { return pointsTotal; } | |
144 set | |
145 { | |
146 double oldPoints = pointsTotal; | |
147 pointsTotal = value; | |
148 | |
149 if (oldPoints!=pointsTotal) | |
150 { | |
151 OnPointsValueChanged(oldPoints, pointsTotal); | |
152 } | |
153 } | |
154 } | |
155 | |
156 public double PointsTotal | |
157 { | |
158 get { return TotalPoints; } | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
159 } |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
160 |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
161 public void AddUnit(Unit unit) |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
162 { |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
163 List<FailedUnitRequirement> failedReqs = CanAddUnit(unit); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
164 unit.Army = this; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
165 ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
166 armyCat.AddUnit(unit); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
167 OnUnitAdded(unit, failedReqs); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
168 } |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
169 |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
170 public void RemoveUnit(Unit unit) |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
171 { |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
172 List<FailedUnitRequirement> failedReqs = CanRemoveUnit(unit); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
173 unit.Army = null; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
174 ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
175 armyCat.RemoveUnit(unit); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
176 OnUnitRemoved(unit, failedReqs); |
0 | 177 } |
178 | |
179 public Unit[] GetUnits(Category cat) | |
180 { | |
181 return GetUnits(this.GetCategory(cat)); | |
182 } | |
183 | |
184 public Unit[] GetUnits(ArmyCategory cat) | |
185 { | |
186 return cat.GetUnits(); | |
187 } | |
188 | |
189 public Unit[] GetUnits() | |
190 { | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
191 List<Unit> fullList = new List<Unit>(); |
0 | 192 |
193 foreach(ArmyCategory cat in Categories) | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
194 { |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
195 fullList.AddRange(cat.GetUnits()); |
0 | 196 } |
197 | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
198 return fullList.ToArray(); |
0 | 199 } |
200 | |
201 public int MaxPoints | |
202 { | |
203 get { return maxPoints; } | |
204 set | |
205 { | |
206 if (value > 0) | |
207 { | |
208 maxPoints = value; | |
209 } | |
210 } | |
211 } | |
212 | |
213 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal) | |
214 { | |
215 if (obj is ArmyCategory) | |
216 { | |
217 double points = 0; | |
218 | |
219 foreach (ArmyCategory cat in Categories) | |
220 { | |
221 points+= cat.PointsTotal; | |
222 } | |
223 | |
224 TotalPoints = points; | |
225 } | |
226 } | |
227 | |
228 public List<FailedUnitRequirement> CanAddUnit(Unit unit) | |
229 { | |
230 return CanAddUnitType(unit.UnitType); | |
231 } | |
232 | |
233 public List<FailedUnitRequirement> CanAddUnitType(UnitType unitType) | |
234 { | |
235 return unitType.CanAddToArmy(this); | |
236 } | |
237 | |
238 public List<FailedUnitRequirement> CanRemoveUnit(Unit unit) | |
239 { | |
240 return CanRemoveUnitType(unit.UnitType); | |
241 } | |
242 | |
243 public List<FailedUnitRequirement> CanRemoveUnitType(UnitType unitType) | |
244 { | |
245 return unitType.CanRemoveFromArmy(this); | |
246 } | |
247 | |
248 public int GetUnitTypeCount(UnitType unitType) | |
249 { | |
250 int count = 0; | |
251 | |
252 foreach (ArmyCategory cat in Categories) | |
253 { | |
254 count+= cat.GetUnitTypeCount(unitType); | |
255 } | |
256 | |
257 return count; | |
258 } | |
259 | |
260 private void Army_UnitAdded(object val) | |
261 { | |
262 OnUnitAdded((Unit)val); | |
263 } | |
264 | |
265 private void Army_UnitRemoved(object val) | |
266 { | |
267 OnUnitRemoved((Unit)val); | |
268 } | |
269 } | |
270 } |