comparison API/Objects/Army.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children d34ae0057a39
comparison
equal deleted inserted replaced
336:3631c1493c7f 337:3c4a6403a88c
1 // This file (Army.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard.
2 //
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.
4
5 using System;
6 using System.IO;
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 : WarFoundryLoadedObject, ICostedWarFoundryObject
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;
32 private DoubleValChangedDelegate PointsValueChangedMethod;
33
34 public Army(Race race, string armyName, int maxArmyPoints) : this(race, armyName, maxArmyPoints, null)
35 {
36 }
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;
51 }
52
53 private Dictionary<Category, ArmyCategory> ArmyCategories
54 {
55 get
56 {
57 if (categories==null)
58 {
59 categories = new Dictionary<Category, ArmyCategory>();
60 Category[] raceCats = Race.Categories;
61 ArmyCategory cat;
62 int raceCatCount = raceCats.Length;
63
64 for (int i = 0; i < raceCatCount; i++)
65 {
66 Category raceCat = raceCats[i];
67 cat = new ArmyCategory(this, raceCat);
68 categories[raceCat] = cat;
69 cat.PointsValueChanged+= PointsValueChangedMethod;
70 cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded);
71 cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved);
72 cat.FailedRequirement+=new FailedUnitRequirementDelegate(Army_FailedRequirement);
73 }
74 }
75
76 return categories;
77 }
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);
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);
111 }
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);
123 }
124
125 OnFailedRequirement(failedReqs);
126 }
127
128 protected void OnFailedRequirement(List<FailedUnitRequirement> failedReqs)
129 {
130 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0)
131 {
132 FailedRequirement(failedReqs);
133 }
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 Points
160 {
161 get { return TotalPoints; }
162 }
163
164 public void AddUnit(Unit unit)
165 {
166 Category category = unit.UnitType.MainCategory;
167 AddUnit(unit, category);
168 }
169
170 public void AddUnit(Unit unit, Category category)
171 {
172 ArmyCategory armyCat = GetCategory(category);
173 armyCat.AddUnit(unit);
174 }
175
176 public void RemoveUnit(Unit unit)
177 {
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)
218 {
219 double points = 0;
220
221 foreach (ArmyCategory cat in Categories)
222 {
223 points+= cat.Points;
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
262 private void Army_UnitAdded(WarFoundryObject val)
263 {
264 OnUnitAdded((Unit)val);
265 }
266
267 private void Army_UnitRemoved(WarFoundryObject val)
268 {
269 OnUnitRemoved((Unit)val);
270 }
271
272 private void Army_FailedRequirement(List<FailedUnitRequirement> val)
273 {
274 OnFailedRequirement(val);
275 }
276 }
277 }