Mercurial > repos > snowblizz-super-API-ideas
annotate api/Objects/Unit.cs @ 81:032b174fc17a
Re #10 - Refactoring for readability
* Remove "trainwreck code" by making Unit and UnitType publish methods to get arrays of stats
* Remove "trainwreck code" by making Unit, UnitType and Stats publish methods to get value of one stat
* Make factory use new methods
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 27 May 2009 19:43:09 +0000 |
parents | aa66dd18cdae |
children | 3ea0ab04352b |
rev | line source |
---|---|
15 | 1 // This file (Unit.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; |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
6 using System.Collections.Generic; |
0 | 7 using System.Text; |
8 using System.Xml; | |
9 using IBBoard.Lang; | |
10 | |
11 namespace IBBoard.WarFoundry.API.Objects | |
12 { | |
13 /// <summary> | |
14 /// Summary description for UnitInstance. | |
15 /// </summary> | |
16 public class Unit : WarFoundryObject | |
17 { | |
18 private UnitType type; | |
19 private int size; | |
65
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
20 private Army army; |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
21 private Unit parentUnit; |
0 | 22 private double points; |
23 private ArmyCategory cat; | |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
24 private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>(); |
61
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
25 private List<Unit> containedUnits = new List<Unit>(); |
0 | 26 public event DoubleValChangedDelegate PointsValueChanged; |
27 public event IntValChangedDelegate UnitSizeChanged; | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
28 public event DoubleValChangedDelegate UnitEquipmentAmountChanged; |
0 | 29 |
30 public Unit(UnitType unitType, Army parentArmy) : this(unitType, unitType.MinSize, parentArmy){} | |
31 | |
32 public Unit(UnitType unitType, int startSize, Army parentArmy) | |
33 { | |
34 Army = parentArmy; | |
35 type = unitType; | |
36 Size = startSize; | |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
37 SetInitialEquipment(); |
0 | 38 CalcCost(); |
39 } | |
40 | |
41 protected override string DefaultName() | |
42 { | |
43 if (type != null) | |
44 { | |
45 if (size == 1) | |
46 { | |
47 return type.Name; | |
48 } | |
49 else | |
50 { | |
51 return String.Format(Translation.GetTranslation("defaultUnitName"), size, type.Name); | |
52 } | |
53 } | |
54 else | |
55 { | |
56 return "Unknown Unit"; | |
57 } | |
58 } | |
59 | |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
60 private void SetInitialEquipment() |
0 | 61 { |
62 foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems()) | |
63 { | |
64 if (unitEquip.IsRequired) | |
65 { | |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
66 if (CanEquipWithItem(unitEquip)) |
0 | 67 { |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
68 equipment[unitEquip] = unitEquip.MinNumber; |
0 | 69 } |
70 } | |
71 } | |
72 } | |
73 | |
74 private void CalcCost() | |
75 { | |
76 String oldName = HasDefaultName() ? Name : null; | |
77 double oldpoints = points; | |
78 points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost; | |
79 | |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
80 foreach (UnitEquipmentItem unitEquipItem in equipment.Keys) |
0 | 81 { |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
82 double count = equipment[unitEquipItem]; |
0 | 83 |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
84 if (unitEquipItem.IsRatioLimit) |
0 | 85 { |
86 if (unitEquipItem.RoundNumberUp) | |
87 { | |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
88 points += Math.Ceiling(size * count) * unitEquipItem.Cost; |
0 | 89 } |
90 else | |
91 { | |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
92 points += Math.Floor(size * count) * unitEquipItem.Cost; |
0 | 93 } |
94 } | |
95 else | |
96 { | |
38
548cfc776f54
Fixes #34 - Remove "Choices" and "Base/Increment" from Category
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
97 if (count == WarFoundryCore.INFINITY) |
0 | 98 { |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
99 points += size * unitEquipItem.Cost; |
0 | 100 } |
101 else | |
102 { | |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
103 points += count * unitEquipItem.Cost; |
0 | 104 } |
105 } | |
106 } | |
107 | |
108 if (oldpoints!=points) | |
109 { | |
110 OnPointsValueChanged(oldpoints, points); | |
111 } | |
112 | |
113 if (oldName!=null) | |
114 { | |
115 OnNameChanged(oldName, Name); | |
116 } | |
117 } | |
118 | |
119 public int AdditionalTroopers | |
120 { | |
121 get { return Math.Max(Size - type.BaseSize, 0); } | |
122 } | |
123 | |
124 public int Size | |
125 { | |
126 get { return size; } | |
127 set | |
128 { | |
129 if (value!=size) | |
130 { | |
131 int oldValue = size; | |
132 size = (value>0 ? value : 1); | |
133 CalcCost(); | |
134 OnUnitSizeChanged(oldValue, size); | |
135 } | |
136 } | |
137 } | |
138 | |
139 public UnitType UnitType | |
140 { | |
141 get { return type; } | |
142 } | |
143 | |
144 public Army Army | |
145 { | |
146 get { return army; } | |
147 set | |
148 { | |
149 army = value; | |
150 | |
151 if (army == null) | |
152 { | |
153 Category = null; | |
154 } | |
155 } | |
156 } | |
157 | |
158 public Race Race | |
159 { | |
65
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
160 get { return UnitType.Race; } |
0 | 161 } |
162 | |
163 public ArmyCategory Category | |
164 { | |
165 get | |
166 { | |
167 if (cat==null) | |
168 { | |
169 if (Army!=null) | |
170 { | |
171 return Army.GetCategory(UnitType.MainCategory); | |
172 } | |
173 else | |
174 { | |
175 return null; | |
176 } | |
177 } | |
178 else | |
179 { | |
180 return cat; | |
181 } | |
182 } | |
183 set { cat = value; } | |
184 } | |
185 | |
186 public double PointsValue | |
187 { | |
188 get | |
189 { | |
190 if (points == 0) | |
191 { | |
192 CalcCost(); | |
193 } | |
194 | |
195 return points; | |
196 } | |
61
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
197 } |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
198 |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
199 public Unit[] ContainedUnits |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
200 { |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
201 get { return containedUnits.ToArray(); } |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
202 } |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
203 |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
204 public void AddContainedUnit(Unit unit) |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
205 { |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
206 if (UnitType.CanContainUnit(unit)) |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
207 { |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
208 if (!containedUnits.Contains(unit)) |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
209 { |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
210 containedUnits.Add(unit); |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
211 } |
65
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
212 |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
213 unit.ParentUnit = this; |
61
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
214 } |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
215 else |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
216 { |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
217 throw new InvalidContainershipException(this, unit); |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
218 } |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
219 } |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
220 |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
221 public void RemoveContainedUnit(Unit unit) |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
222 { |
3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
54
diff
changeset
|
223 containedUnits.Remove(unit); |
65
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
224 } |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
225 |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
226 public Unit ParentUnit |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
227 { |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
228 get { return parentUnit; } |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
229 set |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
230 { |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
231 if (!(parentUnit == value || (parentUnit != null && parentUnit.Equals(value)))) |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
232 { |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
233 parentUnit = value; |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
234 |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
235 if (value!=null) |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
236 { |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
237 value.AddContainedUnit(this); |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
238 } |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
239 } |
aa2d05a9c635
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
64
diff
changeset
|
240 } |
0 | 241 } |
242 | |
243 public UnitEquipmentItem[] GetAllowedOptionalEquipment() | |
244 { | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
245 List<UnitEquipmentItem> list = new List<UnitEquipmentItem>(); |
0 | 246 |
247 foreach (UnitEquipmentItem item in UnitType.GetEquipmentItems()) | |
248 { | |
249 if (!item.IsRequired) | |
250 { | |
251 list.Add(item); | |
252 } | |
253 } | |
254 | |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
255 return list.ToArray(); |
0 | 256 } |
257 | |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
258 public UnitEquipmentItem[] GetEquipment() |
0 | 259 { |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
260 return DictionaryUtils.ToKeyArray(equipment); |
0 | 261 } |
262 | |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
263 public EquipmentItem[] GetRequiredEquipment() |
0 | 264 { |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
265 List<EquipmentItem> list = new List<EquipmentItem>(); |
0 | 266 |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
267 foreach(UnitEquipmentItem item in GetEquipment()) |
0 | 268 { |
269 if (item.IsRequired) | |
270 { | |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
271 list.Add(item.EquipmentItem); |
0 | 272 } |
273 } | |
274 | |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
275 return list.ToArray(); |
0 | 276 } |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
277 |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
278 public double GetEquipmentAmount(UnitEquipmentItem item) |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
279 { |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
280 return GetEquipmentAmount(item.ID); |
0 | 281 } |
282 | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
283 public double GetEquipmentAmount(string equipID) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
284 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
285 return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID)); |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
286 } |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
287 |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
288 public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
289 { |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
290 if (equip.IsRatioLimit) |
0 | 291 { |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
292 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has a ratio limit, not an absolute limit"); |
0 | 293 } |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
294 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
295 if (amount <1 && amount != WarFoundryCore.INFINITY) |
0 | 296 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
297 amount = 0; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
298 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
299 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
300 SetEquipmentAmount(equip, amount); |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
301 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
302 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
303 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
304 { |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
305 if (!equip.IsRatioLimit) |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
306 { |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
307 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit"); |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
308 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
309 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
310 if (ratio > 1) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
311 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
312 ratio = 1; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
313 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
314 else if (ratio < 0) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
315 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
316 ratio = 0; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
317 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
318 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
319 SetEquipmentAmount(equip, ratio); |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
320 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
321 |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
322 private void SetEquipmentAmount(UnitEquipmentItem equip, double amount) |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
323 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
324 double oldAmount = 0; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
325 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
326 if (equipment.ContainsKey(equip)) |
0 | 327 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
328 oldAmount = equipment[equip]; |
0 | 329 } |
330 | |
331 if (amount!=oldAmount) | |
332 { | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
333 if (amount != 0) |
0 | 334 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
335 equipment[equip] = amount; |
0 | 336 } |
337 else | |
338 { | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
339 equipment.Remove(equip); |
0 | 340 } |
341 | |
342 OnUnitEquipmentAmountChanged(equip, oldAmount, amount); | |
343 CalcCost(); | |
344 } | |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
345 } |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
346 |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
347 public bool CanEquipWithItem(UnitEquipmentItem item) |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
348 { |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
349 string mutex = item.MutexGroup; |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
350 |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
351 if (mutex == "") |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
352 { |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
353 return true; |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
354 } |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
355 |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
356 foreach (UnitEquipmentItem unitItem in GetEquipment()) |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
357 { |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
358 if (unitItem.MutexGroup == mutex) |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
359 { |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
360 return false; |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
361 } |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
362 } |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
363 |
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
364 return true; |
0 | 365 } |
366 | |
367 public bool CanEquipWithItem(string equipID) | |
368 { | |
64
2094bd0ba652
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
61
diff
changeset
|
369 return CanEquipWithItem(UnitType.GetEquipmentItem(equipID)); |
0 | 370 } |
371 | |
372 private void OnPointsValueChanged(double oldValue, double newValue) | |
373 { | |
374 if (PointsValueChanged!=null) | |
375 { | |
376 PointsValueChanged(this, oldValue, newValue); | |
377 } | |
378 } | |
379 | |
380 private void OnUnitSizeChanged(int oldValue, int newValue) | |
381 { | |
382 if (UnitSizeChanged!=null) | |
383 { | |
384 UnitSizeChanged(this, oldValue, newValue); | |
385 } | |
386 } | |
387 | |
80
aa66dd18cdae
Closes #81 - Resolve Unit Equipment problems in WinForms GUI
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
388 private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue) |
0 | 389 { |
390 if (UnitEquipmentAmountChanged!=null) | |
391 { | |
392 UnitEquipmentAmountChanged(equip, oldValue, newValue); | |
393 } | |
394 } | |
395 | |
81
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
396 public Stat[] UnitStatsArray |
0 | 397 { |
81
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
398 get { return UnitType.UnitStatsArray; } |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
399 } |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
400 |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
401 public Stat[] UnitStatsArrayWithName |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
402 { |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
403 get { return UnitType.UnitStatsArrayWithName; } |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
404 } |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
405 |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
406 public string GetStatValue(string statName) |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
407 { |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
408 return UnitType.GetStatValue(statName); |
0 | 409 } |
410 } | |
411 } |