comparison api/Objects/Army.cs @ 0:520818033bb6

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