Mercurial > repos > IBBoard.WarFoundry.API
comparison api/Objects/Unit.cs @ 288:5c4b3d91795f WarFoundry_v0.1
* Add extra notification required by #308 to make sure that unit name changes when translation changes in GTK# UI
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 02 Jan 2011 16:04:49 +0000 |
parents | 51b3027a7dae |
children | 92d10b06ab0f |
comparison
equal
deleted
inserted
replaced
287:3e74bf7b0f72 | 288:5c4b3d91795f |
---|---|
23 private double points; | 23 private double points; |
24 private ArmyCategory cat; | 24 private ArmyCategory cat; |
25 private Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection> equipment = new Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection>(); | 25 private Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection> equipment = new Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection>(); |
26 private Dictionary<string, List<AbstractUnitEquipmentItemSelection>> equipmentSlots = new Dictionary<string, List<AbstractUnitEquipmentItemSelection>>(); | 26 private Dictionary<string, List<AbstractUnitEquipmentItemSelection>> equipmentSlots = new Dictionary<string, List<AbstractUnitEquipmentItemSelection>>(); |
27 private List<Unit> containedUnits = new List<Unit>(); | 27 private List<Unit> containedUnits = new List<Unit>(); |
28 | |
28 public event DoubleValChangedDelegate PointsValueChanged; | 29 public event DoubleValChangedDelegate PointsValueChanged; |
29 public event IntValChangedDelegate UnitSizeChanged; | 30 public event IntValChangedDelegate UnitSizeChanged; |
30 public event DoubleValChangedDelegate UnitEquipmentAmountChanged; | 31 public event DoubleValChangedDelegate UnitEquipmentAmountChanged; |
31 | 32 |
32 public Unit(UnitType unitType, ArmyCategory parentArmyCat) : this(unitType, unitType.MinSize, parentArmyCat) { } | 33 public Unit(UnitType unitType, ArmyCategory parentArmyCat) : this(unitType, unitType.MinSize, parentArmyCat) |
34 { | |
35 //Do nothing extra | |
36 } | |
33 | 37 |
34 public Unit(UnitType unitType, int startSize, ArmyCategory parentArmyCat) : this("", "", startSize, unitType, parentArmyCat) | 38 public Unit(UnitType unitType, int startSize, ArmyCategory parentArmyCat) : this("", "", startSize, unitType, parentArmyCat) |
35 { | 39 { |
36 SetInitialEquipment(); | 40 SetInitialEquipment(); |
37 UnitSizeChanged += new IntValChangedDelegate(RefreshUnitEquipmentAmounts); | 41 UnitSizeChanged += new IntValChangedDelegate(RefreshUnitEquipmentAmounts); |
38 } | 42 } |
39 | 43 |
40 public Unit(string id, string name, int startSize, UnitType unitType, ArmyCategory parentArmyCat) : base(id, name) | 44 public Unit(string id, string name, int startSize, UnitType unitType, ArmyCategory parentArmyCat) : base(id, name) |
41 { | 45 { |
42 Category = parentArmyCat; | 46 Category = parentArmyCat; |
43 type = unitType; | 47 type = unitType; |
44 Size = startSize; | 48 Size = startSize; |
45 CalcCost(); | 49 CalcCost(); |
46 UnitEquipmentAmountChanged+= new DoubleValChangedDelegate(UnitEquipmentAmountChangedHandler); | 50 UnitEquipmentAmountChanged += new DoubleValChangedDelegate(UnitEquipmentAmountChangedHandler); |
47 UnitSizeChanged+= new IntValChangedDelegate(UnitSizeChangedHandler); | 51 UnitSizeChanged += new IntValChangedDelegate(UnitSizeChangedHandler); |
48 } | 52 Translation.TranslationChanged += HandleTranslationChanged; |
49 | 53 } |
54 | |
50 private void UnitEquipmentAmountChangedHandler(WarFoundryObject obj, double oldVal, double newVal) | 55 private void UnitEquipmentAmountChangedHandler(WarFoundryObject obj, double oldVal, double newVal) |
51 { | 56 { |
52 CalcCost(); | 57 CalcCost(); |
53 } | 58 } |
54 | 59 |
55 private void UnitSizeChangedHandler(WarFoundryObject obj, int oldVal, int newVal) | 60 private void UnitSizeChangedHandler(WarFoundryObject obj, int oldVal, int newVal) |
56 { | 61 { |
57 CalcCost(); | 62 CalcCost(); |
58 | 63 |
59 if (HasDefaultName()) | 64 if (HasDefaultName()) |
76 } | 81 } |
77 } | 82 } |
78 else | 83 else |
79 { | 84 { |
80 return "Unknown Unit"; | 85 return "Unknown Unit"; |
86 } | |
87 } | |
88 | |
89 private void HandleTranslationChanged() | |
90 { | |
91 if (type != null && HasDefaultName() && size != 1) | |
92 { | |
93 OnNameChanged(null, DefaultName()); | |
81 } | 94 } |
82 } | 95 } |
83 | 96 |
84 private void SetInitialEquipment() | 97 private void SetInitialEquipment() |
85 { | 98 { |
112 foreach (AbstractUnitEquipmentItemSelection equipSelection in equipment.Values) | 125 foreach (AbstractUnitEquipmentItemSelection equipSelection in equipment.Values) |
113 { | 126 { |
114 points += equipSelection.TotalCost; | 127 points += equipSelection.TotalCost; |
115 } | 128 } |
116 | 129 |
117 if (oldpoints!=points) | 130 if (oldpoints != points) |
118 { | 131 { |
119 OnPointsValueChanged(oldpoints, points); | 132 OnPointsValueChanged(oldpoints, points); |
120 } | 133 } |
121 } | 134 } |
122 | 135 |
123 public int AdditionalTroopers | 136 public int AdditionalTroopers |
124 { | 137 { |
125 get { return Math.Max(Size - type.BaseSize, 0); } | 138 get { return Math.Max(Size - type.BaseSize, 0); } |
126 } | 139 } |
127 | 140 |
128 public int Size | 141 public int Size |
129 { | 142 { |
130 get { return size; } | 143 get { return size; } |
131 set | 144 set |
132 { | 145 { |
133 if (value!=size) | 146 if (value != size) |
134 { | 147 { |
135 int oldValue = size; | 148 int oldValue = size; |
136 size = (value>0 ? value : 1); | 149 size = (value > 0 ? value : 1); |
137 OnUnitSizeChanged(oldValue, size); | 150 OnUnitSizeChanged(oldValue, size); |
138 } | 151 } |
139 } | 152 } |
140 } | 153 } |
141 | 154 |
166 [Obsolete("Use Points instead")] | 179 [Obsolete("Use Points instead")] |
167 public double PointsValue | 180 public double PointsValue |
168 { | 181 { |
169 get { return Points; } | 182 get { return Points; } |
170 } | 183 } |
171 | 184 |
172 public double Points | 185 public double Points |
173 { | 186 { |
174 get | 187 get |
175 { | 188 { |
176 if (points == 0) | 189 if (points == 0) |
177 { | 190 { |
178 CalcCost(); | 191 CalcCost(); |
179 } | 192 } |
180 | 193 |
181 return points; | 194 return points; |
182 } | 195 } |
183 } | 196 } |
184 | 197 |
185 public Unit[] ContainedUnits | 198 public Unit[] ContainedUnits |
186 { | 199 { |
187 get { return containedUnits.ToArray(); } | 200 get { return containedUnits.ToArray(); } |
188 } | 201 } |
189 | 202 |
190 public void AddContainedUnit(Unit unit) | 203 public void AddContainedUnit(Unit unit) |
191 { | 204 { |
192 if (UnitType.CanContainUnit(unit)) | 205 if (UnitType.CanContainUnit(unit)) |
193 { | 206 { |
194 if (!containedUnits.Contains(unit)) | 207 if (!containedUnits.Contains(unit)) |
201 else | 214 else |
202 { | 215 { |
203 throw new InvalidContainershipException(this, unit); | 216 throw new InvalidContainershipException(this, unit); |
204 } | 217 } |
205 } | 218 } |
206 | 219 |
207 public void RemoveContainedUnit(Unit unit) | 220 public void RemoveContainedUnit(Unit unit) |
208 { | 221 { |
209 containedUnits.Remove(unit); | 222 containedUnits.Remove(unit); |
210 } | 223 } |
211 | 224 |
212 public Unit ParentUnit | 225 public Unit ParentUnit |
213 { | 226 { |
214 get { return parentUnit; } | 227 get { return parentUnit; } |
215 set | 228 set |
216 { | 229 { |
217 if (!(parentUnit == value || (parentUnit != null && parentUnit.Equals(value)))) | 230 if (!(parentUnit == value || (parentUnit != null && parentUnit.Equals(value)))) |
218 { | 231 { |
219 parentUnit = value; | 232 parentUnit = value; |
220 | 233 |
221 if (value!=null) | 234 if (value != null) |
222 { | 235 { |
223 value.AddContainedUnit(this); | 236 value.AddContainedUnit(this); |
224 } | 237 } |
225 } | 238 } |
226 } | 239 } |
239 | 252 |
240 public EquipmentItem[] GetRequiredEquipment() | 253 public EquipmentItem[] GetRequiredEquipment() |
241 { | 254 { |
242 List<EquipmentItem> list = new List<EquipmentItem>(); | 255 List<EquipmentItem> list = new List<EquipmentItem>(); |
243 | 256 |
244 foreach(UnitEquipmentItem item in GetEquipment()) | 257 foreach (UnitEquipmentItem item in GetEquipment()) |
245 { | 258 { |
246 if (item.IsRequired) | 259 if (item.IsRequired) |
247 { | 260 { |
248 list.Add(item.EquipmentItem); | 261 list.Add(item.EquipmentItem); |
249 } | 262 } |
290 [Obsolete("Implementation is down to the UI")] | 303 [Obsolete("Implementation is down to the UI")] |
291 public string GetEquipmentAmountString(UnitEquipmentItem item) | 304 public string GetEquipmentAmountString(UnitEquipmentItem item) |
292 { | 305 { |
293 return ""; | 306 return ""; |
294 } | 307 } |
295 | 308 |
296 public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) | 309 public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) |
297 { | 310 { |
298 if (amount <1 && amount != WarFoundryCore.INFINITY) | 311 if (amount < 1 && amount != WarFoundryCore.INFINITY) |
299 { | 312 { |
300 amount = 0; | 313 amount = 0; |
301 } | 314 } |
302 | 315 |
303 if (amount == 0) | 316 if (amount == 0) |
328 | 341 |
329 OnUnitEquipmentAmountChanged(equip, oldAmount, amount); | 342 OnUnitEquipmentAmountChanged(equip, oldAmount, amount); |
330 } | 343 } |
331 } | 344 } |
332 } | 345 } |
333 | 346 |
334 private void AddEquipmentAmount(UnitEquipmentItem equip, int amount) | 347 private void AddEquipmentAmount(UnitEquipmentItem equip, int amount) |
335 { | 348 { |
336 AbstractUnitEquipmentItemSelection newItem = new UnitEquipmentNumericSelection(this, equip, amount); | 349 AbstractUnitEquipmentItemSelection newItem = new UnitEquipmentNumericSelection(this, equip, amount); |
337 equipment[equip] = newItem; | 350 equipment[equip] = newItem; |
338 List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName); | 351 List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName); |
343 equipmentSlots[equip.SlotName] = selections; | 356 equipmentSlots[equip.SlotName] = selections; |
344 } | 357 } |
345 | 358 |
346 selections.Add(newItem); | 359 selections.Add(newItem); |
347 } | 360 } |
348 | 361 |
349 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) | 362 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) |
350 { | 363 { |
351 if (!equip.IsRatioLimit) | 364 if (!equip.IsRatioLimit) |
352 { | 365 { |
353 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit"); | 366 throw new InvalidOperationException("Equipment with ID " + equip.ID + " for unit of type " + UnitType.ID + " has an absolute limit, not a ratio limit"); |
354 } | 367 } |
355 | 368 |
356 if (ratio > 100) | 369 if (ratio > 100) |
357 { | 370 { |
358 ratio = 100; | 371 ratio = 100; |
389 | 402 |
390 OnUnitEquipmentAmountChanged(equip, oldRatio, ratio); | 403 OnUnitEquipmentAmountChanged(equip, oldRatio, ratio); |
391 } | 404 } |
392 } | 405 } |
393 } | 406 } |
394 | 407 |
395 private void AddEquipmentRatio(UnitEquipmentItem equip, double ratio) | 408 private void AddEquipmentRatio(UnitEquipmentItem equip, double ratio) |
396 { | 409 { |
397 UnitEquipmentRatioSelection newItem = new UnitEquipmentRatioSelection (this, equip, ratio); | 410 UnitEquipmentRatioSelection newItem = new UnitEquipmentRatioSelection(this, equip, ratio); |
398 equipment[equip] = newItem; | 411 equipment[equip] = newItem; |
399 List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName); | 412 List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName); |
400 | 413 |
401 if (selections == null) | 414 if (selections == null) |
402 { | 415 { |
404 equipmentSlots[equip.SlotName] = selections; | 417 equipmentSlots[equip.SlotName] = selections; |
405 } | 418 } |
406 | 419 |
407 selections.Add(newItem); | 420 selections.Add(newItem); |
408 } | 421 } |
409 | 422 |
410 private void RemoveEquipmentItem(UnitEquipmentItem equip) | 423 private void RemoveEquipmentItem(UnitEquipmentItem equip) |
411 { | 424 { |
412 double oldAmount = UnitEquipmentUtil.GetEquipmentAmount(this, equip); | 425 double oldAmount = UnitEquipmentUtil.GetEquipmentAmount(this, equip); |
413 | 426 |
414 if (oldAmount != 0) | 427 if (oldAmount != 0) |
415 { | 428 { |
416 AbstractUnitEquipmentItemSelection selection = DictionaryUtils.GetValue (equipment, equip); | 429 AbstractUnitEquipmentItemSelection selection = DictionaryUtils.GetValue(equipment, equip); |
417 equipment.Remove(equip); | 430 equipment.Remove(equip); |
418 List<AbstractUnitEquipmentItemSelection> slotSelections = DictionaryUtils.GetValue (equipmentSlots, equip.SlotName); | 431 List<AbstractUnitEquipmentItemSelection> slotSelections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName); |
419 slotSelections.Remove(selection); | 432 slotSelections.Remove(selection); |
420 OnUnitEquipmentAmountChanged(equip, oldAmount, 0); | 433 OnUnitEquipmentAmountChanged(equip, oldAmount, 0); |
421 } | 434 } |
422 } | 435 } |
423 | 436 |
424 public bool CanEquipWithItem(UnitEquipmentItem item) | 437 public bool CanEquipWithItem(UnitEquipmentItem item) |
425 { | 438 { |
426 string[] mutexes = item.MutexGroups; | 439 string[] mutexes = item.MutexGroups; |
427 bool canEquip = false; | 440 bool canEquip = false; |
428 | 441 |
449 return CanEquipWithItem(UnitType.GetEquipmentItem(equipID)); | 462 return CanEquipWithItem(UnitType.GetEquipmentItem(equipID)); |
450 } | 463 } |
451 | 464 |
452 private void OnPointsValueChanged(double oldValue, double newValue) | 465 private void OnPointsValueChanged(double oldValue, double newValue) |
453 { | 466 { |
454 if (PointsValueChanged!=null) | 467 if (PointsValueChanged != null) |
455 { | 468 { |
456 PointsValueChanged(this, oldValue, newValue); | 469 PointsValueChanged(this, oldValue, newValue); |
457 } | 470 } |
458 } | 471 } |
459 | 472 |
460 private void OnUnitSizeChanged(int oldValue, int newValue) | 473 private void OnUnitSizeChanged(int oldValue, int newValue) |
461 { | 474 { |
462 if (UnitSizeChanged!=null) | 475 if (UnitSizeChanged != null) |
463 { | 476 { |
464 UnitSizeChanged(this, oldValue, newValue); | 477 UnitSizeChanged(this, oldValue, newValue); |
465 } | 478 } |
466 } | 479 } |
467 | 480 |
468 private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue) | 481 private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue) |
469 { | 482 { |
470 if (UnitEquipmentAmountChanged!=null) | 483 if (UnitEquipmentAmountChanged != null) |
471 { | 484 { |
472 UnitEquipmentAmountChanged(equip, oldValue, newValue); | 485 UnitEquipmentAmountChanged(equip, oldValue, newValue); |
473 } | 486 } |
474 } | 487 } |
475 | 488 |
476 [Obsolete("Use UnitStatsArrays instead")] | 489 [Obsolete("Use UnitStatsArrays instead")] |
477 public Stat[] UnitStatsArray | 490 public Stat[] UnitStatsArray |
478 { | 491 { |
479 get { return UnitType.UnitStatsArray; } | 492 get { return UnitType.UnitStatsArray; } |
480 } | 493 } |
481 | 494 |
482 public Stat[][] UnitStatsArrays | 495 public Stat[][] UnitStatsArrays |
483 { | 496 { |
484 get { return UnitType.UnitStatsArrays; } | 497 get { return UnitType.UnitStatsArrays; } |
485 } | 498 } |
486 | 499 |
487 [Obsolete("Use UnitStatsArraysWithName instead")] | 500 [Obsolete("Use UnitStatsArraysWithName instead")] |
488 public Stat[] UnitStatsArrayWithName | 501 public Stat[] UnitStatsArrayWithName |
489 { | 502 { |
490 get { return UnitType.UnitStatsArrayWithName; } | 503 get { return UnitType.UnitStatsArrayWithName; } |
491 } | 504 } |
492 | 505 |
493 public Stat[][] UnitStatsArraysWithName | 506 public Stat[][] UnitStatsArraysWithName |
494 { | 507 { |
495 get { return UnitType.UnitStatsArraysWithName; } | 508 get { return UnitType.UnitStatsArraysWithName; } |
496 } | 509 } |
497 | 510 |
498 public string[] UnitStatsArrayIDs | 511 public string[] UnitStatsArrayIDs |
499 { | 512 { |
500 get | 513 get |
501 { | 514 { |
502 return UnitType.UnitStatsArrayIDs; | 515 return UnitType.UnitStatsArrayIDs; |
503 } | 516 } |
504 } | 517 } |
505 | 518 |
506 public string GetStatValue(string statName) | 519 public string GetStatValue(string statName) |
507 { | 520 { |
508 return UnitType.GetStatValue(statName); | 521 return UnitType.GetStatValue(statName); |
509 } | 522 } |
510 | 523 |
511 public int GetEquipmentAmountInSlot (string slotName) | 524 public int GetEquipmentAmountInSlot(string slotName) |
512 { | 525 { |
513 int amount = 0; | 526 int amount = 0; |
514 | 527 |
515 List<AbstractUnitEquipmentItemSelection> selections = GetEquipmentSlotSelections(slotName); | 528 List<AbstractUnitEquipmentItemSelection> selections = GetEquipmentSlotSelections(slotName); |
516 | 529 |
556 { | 569 { |
557 selections.Remove(selection); | 570 selections.Remove(selection); |
558 } | 571 } |
559 } | 572 } |
560 | 573 |
561 private int GetSelectionTotal (List<AbstractUnitEquipmentItemSelection> selections) | 574 private int GetSelectionTotal(List<AbstractUnitEquipmentItemSelection> selections) |
562 { | 575 { |
563 int amount = 0; | 576 int amount = 0; |
564 | 577 |
565 foreach (AbstractUnitEquipmentItemSelection selection in selections) | 578 foreach (AbstractUnitEquipmentItemSelection selection in selections) |
566 { | 579 { |
567 amount+= selection.NumberTaken; | 580 amount += selection.NumberTaken; |
568 } | 581 } |
569 | 582 |
570 return amount; | 583 return amount; |
571 } | 584 } |
572 | 585 |