Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Objects/Army.cs @ 161:81abc04b3dbe
Re #192: Improve "clean coding" practice
* Move some calculation methods for equipment out of the encapsulation objects
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 04 Oct 2009 12:00:16 +0000 |
parents | 7f13ffcb8765 |
children | 35dc06030355 |
rev | line source |
---|---|
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
1 // This file (Army.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard. |
15 | 2 // |
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. |
15 | 4 |
82 | 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; |
82 | 7 using System.Collections.Generic; |
8 using System.Text; | |
9 using System.Xml; | |
0 | 10 using IBBoard.WarFoundry.API; |
11 using IBBoard.WarFoundry.API.Factories; | |
12 using IBBoard.WarFoundry.API.Requirements; | |
82 | 13 using ICSharpCode.SharpZipLib.Zip; |
14 | |
15 namespace IBBoard.WarFoundry.API.Objects | |
16 { | |
17 /// <summary> | |
18 /// Summary description for Army. | |
19 /// </summary> | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
20 public class Army : WarFoundryObject, ICostedWarFoundryObject |
82 | 21 { |
22 //private GameSystem system; | |
23 private Race armyRace; | |
24 private int maxPoints; | |
25 private double pointsTotal; | |
26 private Dictionary<Category, ArmyCategory> categories; | |
27 | |
28 public event ObjectAddDelegate UnitAdded; | |
29 public event ObjectRemoveDelegate UnitRemoved; | |
30 public event FailedUnitRequirementDelegate FailedRequirement; | |
31 public event DoubleValChangedDelegate PointsValueChanged; | |
0 | 32 private DoubleValChangedDelegate PointsValueChangedMethod; |
33 | |
59
3fa4658c50c6
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
34 public Army(Race race, string armyName, int maxArmyPoints) : this(race, armyName, maxArmyPoints, null) |
0 | 35 { |
36 } | |
82 | 37 |
38 public Army(Race race, string armyName, int maxArmyPoints, ZipFile file) : base(armyName) | |
39 { | |
40 armyRace = race; | |
41 Name = armyName; | |
42 maxPoints = maxArmyPoints; | |
43 PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler); | |
44 } | |
45 | |
46 public ArmyCategory GetCategory(Category cat) | |
47 { | |
48 ArmyCategory armyCat = null; | |
49 ArmyCategories.TryGetValue(cat, out armyCat); | |
50 return armyCat; | |
58
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); |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
72 cat.FailedRequirement+=new FailedUnitRequirementDelegate(Army_FailedRequirement); |
58
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 |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
76 return categories; |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
77 } |
82 | 78 } |
79 | |
80 public ArmyCategory[] Categories | |
81 { | |
82 get | |
83 { | |
84 return DictionaryUtils.ToArray<Category, ArmyCategory>(ArmyCategories); | |
85 } | |
86 } | |
87 | |
88 public Race Race | |
89 { | |
90 get { return armyRace; } | |
91 } | |
92 | |
93 public GameSystem GameSystem | |
94 { | |
95 get { return (armyRace!=null ? armyRace.GameSystem : null); } | |
96 } | |
97 | |
98 protected void OnUnitAdded(Unit unit) | |
99 { | |
100 OnUnitAdded(unit, null); | |
89 | 101 } |
102 | |
103 protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) | |
104 { | |
105 if (UnitAdded != null) | |
106 { | |
107 UnitAdded(unit); | |
108 } | |
109 | |
110 OnFailedRequirement(failedReqs); | |
0 | 111 } |
82 | 112 |
113 protected void OnUnitRemoved(Unit unit) | |
114 { | |
115 OnUnitRemoved(unit, null); | |
116 } | |
117 | |
118 protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) | |
119 { | |
120 if (UnitRemoved!=null) | |
121 { | |
122 UnitRemoved(unit); | |
89 | 123 } |
124 | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
125 OnFailedRequirement(failedReqs); |
89 | 126 } |
127 | |
128 protected void OnFailedRequirement(List<FailedUnitRequirement> failedReqs) | |
129 { | |
130 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) | |
131 { | |
132 FailedRequirement(failedReqs); | |
133 } | |
82 | 134 } |
135 | |
136 private void OnPointsValueChanged(double oldValue, double newValue) | |
137 { | |
138 if (PointsValueChanged!=null) | |
139 { | |
140 PointsValueChanged(this, oldValue, newValue); | |
141 } | |
142 } | |
143 | |
144 private double TotalPoints | |
145 { | |
146 get { return pointsTotal; } | |
147 set | |
148 { | |
149 double oldPoints = pointsTotal; | |
150 pointsTotal = value; | |
151 | |
152 if (oldPoints!=pointsTotal) | |
153 { | |
154 OnPointsValueChanged(oldPoints, pointsTotal); | |
155 } | |
156 } | |
157 } | |
158 | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
159 [Obsolete("Use Points instead")] |
82 | 160 public double PointsTotal |
161 { | |
162 get { return TotalPoints; } | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
163 } |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
164 |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
165 public double Points |
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
166 { |
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
167 get { return TotalPoints; } |
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
168 } |
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
169 |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
170 public void AddUnit(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 ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
173 armyCat.AddUnit(unit); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
174 } |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
175 |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
176 public void RemoveUnit(Unit unit) |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
177 { |
82 | 178 unit.Category.RemoveUnit(unit); |
179 } | |
180 | |
181 public Unit[] GetUnits(Category cat) | |
182 { | |
183 return GetUnits(this.GetCategory(cat)); | |
184 } | |
185 | |
186 public Unit[] GetUnits(ArmyCategory cat) | |
187 { | |
188 return cat.GetUnits(); | |
189 } | |
190 | |
191 public Unit[] GetUnits() | |
192 { | |
193 List<Unit> fullList = new List<Unit>(); | |
194 | |
195 foreach(ArmyCategory cat in Categories) | |
196 { | |
197 fullList.AddRange(cat.GetUnits()); | |
198 } | |
199 | |
200 return fullList.ToArray(); | |
201 } | |
202 | |
203 public int MaxPoints | |
204 { | |
205 get { return maxPoints; } | |
206 set | |
207 { | |
208 if (value > 0) | |
209 { | |
210 maxPoints = value; | |
211 } | |
212 } | |
213 } | |
214 | |
215 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal) | |
216 { | |
217 if (obj is ArmyCategory) | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
218 { |
82 | 219 double points = 0; |
220 | |
221 foreach (ArmyCategory cat in Categories) | |
222 { | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
223 points+= cat.Points; |
82 | 224 } |
225 | |
226 TotalPoints = points; | |
227 } | |
228 } | |
229 | |
230 public List<FailedUnitRequirement> CanAddUnit(Unit unit) | |
231 { | |
232 return CanAddUnitType(unit.UnitType); | |
233 } | |
234 | |
235 public List<FailedUnitRequirement> CanAddUnitType(UnitType unitType) | |
236 { | |
237 return unitType.CanAddToArmy(this); | |
238 } | |
239 | |
240 public List<FailedUnitRequirement> CanRemoveUnit(Unit unit) | |
241 { | |
242 return CanRemoveUnitType(unit.UnitType); | |
243 } | |
244 | |
245 public List<FailedUnitRequirement> CanRemoveUnitType(UnitType unitType) | |
246 { | |
247 return unitType.CanRemoveFromArmy(this); | |
248 } | |
249 | |
250 public int GetUnitTypeCount(UnitType unitType) | |
251 { | |
252 int count = 0; | |
253 | |
254 foreach (ArmyCategory cat in Categories) | |
255 { | |
256 count+= cat.GetUnitTypeCount(unitType); | |
257 } | |
258 | |
259 return count; | |
260 } | |
261 | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
262 private void Army_UnitAdded(WarFoundryObject val) |
82 | 263 { |
264 OnUnitAdded((Unit)val); | |
89 | 265 } |
266 | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
267 private void Army_UnitRemoved(WarFoundryObject val) |
82 | 268 { |
269 OnUnitRemoved((Unit)val); | |
89 | 270 } |
271 | |
272 private void Army_FailedRequirement(List<FailedUnitRequirement> val) | |
273 { | |
274 OnFailedRequirement(val); | |
82 | 275 } |
276 } | |
277 } |