Mercurial > repos > snowblizz-super-API-ideas
annotate api/Objects/Army.cs @ 83:89cc29b4c012
Re #90: Stop new units showing up twice
* Hand all of unit adding/removing down to category
* Refactor out OnFailedRequirement method in Army
* Make Army listen to and propogate FailedRequirement events from Units
* Add OnUnitAdded/Removed method to ArmyCategory that takes list of failures
* Remove direct reference to Army from Unit and go via ArmyCategory instead
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 27 Jun 2009 19:39:04 +0000 |
parents | 3ea0ab04352b |
children | cb3759c3ea19 |
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 | |
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> | |
20 public class Army : WarFoundryObject | |
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); | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
101 } |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
102 |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
103 protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
104 { |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
105 if (UnitAdded != null) |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
106 { |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
107 UnitAdded(unit); |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
108 } |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
109 |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
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); | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
123 } |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
124 |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
125 OnFailedRequirement(failedReqs); |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
126 } |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
127 |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
128 protected void OnFailedRequirement(List<FailedUnitRequirement> failedReqs) |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
129 { |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
130 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
131 { |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
132 FailedRequirement(failedReqs); |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
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 | |
159 public double PointsTotal | |
160 { | |
161 get { return TotalPoints; } | |
58
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 |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
164 public void AddUnit(Unit unit) |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
165 { |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
166 ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); |
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
167 armyCat.AddUnit(unit); |
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 { |
82 | 172 unit.Category.RemoveUnit(unit); |
173 } | |
174 | |
175 public Unit[] GetUnits(Category cat) | |
176 { | |
177 return GetUnits(this.GetCategory(cat)); | |
178 } | |
179 | |
180 public Unit[] GetUnits(ArmyCategory cat) | |
181 { | |
182 return cat.GetUnits(); | |
183 } | |
184 | |
185 public Unit[] GetUnits() | |
186 { | |
187 List<Unit> fullList = new List<Unit>(); | |
188 | |
189 foreach(ArmyCategory cat in Categories) | |
190 { | |
191 fullList.AddRange(cat.GetUnits()); | |
192 } | |
193 | |
194 return fullList.ToArray(); | |
195 } | |
196 | |
197 public int MaxPoints | |
198 { | |
199 get { return maxPoints; } | |
200 set | |
201 { | |
202 if (value > 0) | |
203 { | |
204 maxPoints = value; | |
205 } | |
206 } | |
207 } | |
208 | |
209 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal) | |
210 { | |
211 if (obj is ArmyCategory) | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
212 { |
82 | 213 double points = 0; |
214 | |
215 foreach (ArmyCategory cat in Categories) | |
216 { | |
217 points+= cat.PointsTotal; | |
218 } | |
219 | |
220 TotalPoints = points; | |
221 } | |
222 } | |
223 | |
224 public List<FailedUnitRequirement> CanAddUnit(Unit unit) | |
225 { | |
226 return CanAddUnitType(unit.UnitType); | |
227 } | |
228 | |
229 public List<FailedUnitRequirement> CanAddUnitType(UnitType unitType) | |
230 { | |
231 return unitType.CanAddToArmy(this); | |
232 } | |
233 | |
234 public List<FailedUnitRequirement> CanRemoveUnit(Unit unit) | |
235 { | |
236 return CanRemoveUnitType(unit.UnitType); | |
237 } | |
238 | |
239 public List<FailedUnitRequirement> CanRemoveUnitType(UnitType unitType) | |
240 { | |
241 return unitType.CanRemoveFromArmy(this); | |
242 } | |
243 | |
244 public int GetUnitTypeCount(UnitType unitType) | |
245 { | |
246 int count = 0; | |
247 | |
248 foreach (ArmyCategory cat in Categories) | |
249 { | |
250 count+= cat.GetUnitTypeCount(unitType); | |
251 } | |
252 | |
253 return count; | |
254 } | |
255 | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
256 private void Army_UnitAdded(WarFoundryObject val) |
82 | 257 { |
258 OnUnitAdded((Unit)val); | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
259 } |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
260 |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
261 private void Army_UnitRemoved(WarFoundryObject val) |
82 | 262 { |
263 OnUnitRemoved((Unit)val); | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
264 } |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
265 |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
266 private void Army_FailedRequirement(List<FailedUnitRequirement> val) |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
267 { |
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
268 OnFailedRequirement(val); |
82 | 269 } |
270 } | |
271 } |