comparison api/Objects/Unit.cs @ 178:ca2cd24f6872

Re #198: Add slots with counts to units * Add storing of selections by slot on add and removing on remove * Add method to get total number of items in a slot
author IBBoard <dev@ibboard.co.uk>
date Sat, 24 Oct 2009 14:01:50 +0000
parents 91f7b8da0b53
children 55dc7c97fcfe
comparison
equal deleted inserted replaced
177:948e90426a8d 178:ca2cd24f6872
20 private int size; 20 private int size;
21 private Unit parentUnit; 21 private Unit parentUnit;
22 private double points; 22 private double points;
23 private ArmyCategory cat; 23 private ArmyCategory cat;
24 private Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection> equipment = new Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection>(); 24 private Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection> equipment = new Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection>();
25 private Dictionary<string, List<AbstractUnitEquipmentItemSelection>> equipmentSlots = new Dictionary<string, List<AbstractUnitEquipmentItemSelection>>();
25 private List<Unit> containedUnits = new List<Unit>(); 26 private List<Unit> containedUnits = new List<Unit>();
26 public event DoubleValChangedDelegate PointsValueChanged; 27 public event DoubleValChangedDelegate PointsValueChanged;
27 public event IntValChangedDelegate UnitSizeChanged; 28 public event IntValChangedDelegate UnitSizeChanged;
28 public event DoubleValChangedDelegate UnitEquipmentAmountChanged; 29 public event DoubleValChangedDelegate UnitEquipmentAmountChanged;
29 30
357 { 358 {
358 newItem = new UnitEquipmentNumericSelection(this, equip, amount); 359 newItem = new UnitEquipmentNumericSelection(this, equip, amount);
359 } 360 }
360 361
361 equipment[equip] = newItem; 362 equipment[equip] = newItem;
363 List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName);
364
365 if (selections == null)
366 {
367 selections = new List<AbstractUnitEquipmentItemSelection>();
368 equipmentSlots[equip.SlotName] = selections;
369 }
370
371 Console.WriteLine ("Sels: "+selections.Count);
372
373 selections.Add(newItem);
362 } 374 }
363 375
364 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) 376 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
365 { 377 {
366 if (!equip.IsRatioLimit) 378 if (!equip.IsRatioLimit)
407 } 419 }
408 } 420 }
409 421
410 private void AddEquipmentRatio(UnitEquipmentItem equip, double ratio) 422 private void AddEquipmentRatio(UnitEquipmentItem equip, double ratio)
411 { 423 {
412 equipment[equip] = new UnitEquipmentRatioSelection(this, equip, ratio); 424 UnitEquipmentRatioSelection newItem = new UnitEquipmentRatioSelection (this, equip, ratio);
425 equipment[equip] = newItem;
426 List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName);
427
428 if (selections == null)
429 {
430 selections = new List<AbstractUnitEquipmentItemSelection>();
431 equipmentSlots[equip.SlotName] = selections;
432 }
433
434 selections.Add(newItem);
413 } 435 }
414 436
415 private void RemoveEquipmentItem(UnitEquipmentItem equip) 437 private void RemoveEquipmentItem(UnitEquipmentItem equip)
416 { 438 {
417 double oldAmount = GetEquipmentAmount(equip); 439 double oldAmount = GetEquipmentAmount(equip);
418 440
419 if (oldAmount != 0) 441 if (oldAmount != 0)
420 { 442 {
443 AbstractUnitEquipmentItemSelection selection = DictionaryUtils.GetValue (equipment, equip);
421 equipment.Remove(equip); 444 equipment.Remove(equip);
445 List<AbstractUnitEquipmentItemSelection> slotSelections = DictionaryUtils.GetValue (equipmentSlots, equip.SlotName);
446 slotSelections.Remove(selection);
422 OnUnitEquipmentAmountChanged(equip, oldAmount, 0); 447 OnUnitEquipmentAmountChanged(equip, oldAmount, 0);
423 } 448 }
424 } 449 }
425 450
426 public bool CanEquipWithItem(UnitEquipmentItem item) 451 public bool CanEquipWithItem(UnitEquipmentItem item)
487 512
488 public string GetStatValue(string statName) 513 public string GetStatValue(string statName)
489 { 514 {
490 return UnitType.GetStatValue(statName); 515 return UnitType.GetStatValue(statName);
491 } 516 }
517
518 public int GetEquipmentAmountInSlot (string slotName)
519 {
520 int amount = 0;
521
522 List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, slotName);
523
524 if (selections != null)
525 {
526 amount = GetSelectionTotal(selections);
527 }
528
529 return amount;
530 }
531
532 private int GetSelectionTotal (List<AbstractUnitEquipmentItemSelection> selections)
533 {
534 int amount = 0;
535
536 foreach (AbstractUnitEquipmentItemSelection selection in selections)
537 {
538 Console.WriteLine ("Sel: "+selection.NumberTaken);
539 amount+= selection.NumberTaken;
540 }
541
542 return amount;
543 }
544
492 } 545 }
493 } 546 }