Mercurial > repos > snowblizz-super-API-ideas
changeset 83:89cc29b4c012
Re #90: Stop new units showing up twice
* Hand all of unit adding/removing down to category
* Refactor out OnFailedRequirement method in Army
* Make Army listen to and propogate FailedRequirement events from Units
* Add OnUnitAdded/Removed method to ArmyCategory that takes list of failures
* Remove direct reference to Army from Unit and go via ArmyCategory instead
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 27 Jun 2009 19:39:04 +0000 |
parents | 3ea0ab04352b |
children | 2b977962302a |
files | api/Commands/CreateAndAddUnitCommand.cs api/Objects/Army.cs api/Objects/ArmyCategory.cs api/Objects/Unit.cs |
diffstat | 4 files changed, 94 insertions(+), 92 deletions(-) [+] |
line wrap: on
line diff
--- a/api/Commands/CreateAndAddUnitCommand.cs Sat Jun 27 18:59:49 2009 +0000 +++ b/api/Commands/CreateAndAddUnitCommand.cs Sat Jun 27 19:39:04 2009 +0000 @@ -12,17 +12,17 @@ public class CreateAndAddUnitCommand : Command { private UnitType addedUnitType; - private Army army; + private ArmyCategory armyCat; private Unit addedUnit; - public CreateAndAddUnitCommand(UnitType toAdd, Army armyTo) + public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory armyCatTo) { addedUnitType = toAdd; - army = armyTo; + armyCat = armyCatTo; } [Obsolete("Use two parameter constructor instead")] - public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory catTo, Army armyTo) : this(toAdd, armyTo) + public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory catTo, Army armyTo) : this(toAdd, catTo) { } @@ -33,7 +33,7 @@ public override bool CanExecute() { - return (addedUnitType!=null && army!=null); + return (addedUnitType!=null && armyCat!=null); } public override string Description @@ -48,19 +48,19 @@ public override bool Execute() { - addedUnit = new Unit(addedUnitType, army); + addedUnit = new Unit(addedUnitType, armyCat); this.Redo(); return true; } public override void Redo() { - army.AddUnit(addedUnit); + armyCat.AddUnit(addedUnit); } public override void Undo() - { - army.RemoveUnit(addedUnit); + { + armyCat.RemoveUnit(addedUnit); }
--- a/api/Objects/Army.cs Sat Jun 27 18:59:49 2009 +0000 +++ b/api/Objects/Army.cs Sat Jun 27 19:39:04 2009 +0000 @@ -69,6 +69,7 @@ cat.PointsValueChanged+= PointsValueChangedMethod; cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded); cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved); + cat.FailedRequirement+=new FailedUnitRequirementDelegate(Army_FailedRequirement); } } @@ -97,19 +98,16 @@ protected void OnUnitAdded(Unit unit) { OnUnitAdded(unit, null); - } - - protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) - { - if (UnitAdded!=null) - { - UnitAdded(unit); - } - - if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0) - { - FailedRequirement(failedReqs); - } + } + + protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) + { + if (UnitAdded != null) + { + UnitAdded(unit); + } + + OnFailedRequirement(failedReqs); } protected void OnUnitRemoved(Unit unit) @@ -122,12 +120,17 @@ if (UnitRemoved!=null) { UnitRemoved(unit); - } - - if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0) - { - FailedRequirement(failedReqs); - } + } + + OnFailedRequirement(failedReqs); + } + + protected void OnFailedRequirement(List<FailedUnitRequirement> failedReqs) + { + if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) + { + FailedRequirement(failedReqs); + } } private void OnPointsValueChanged(double oldValue, double newValue) @@ -160,17 +163,13 @@ public void AddUnit(Unit unit) { - unit.Army = this; ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); armyCat.AddUnit(unit); } public void RemoveUnit(Unit unit) { - List<FailedUnitRequirement> failedReqs = CanRemoveUnit(unit); - unit.Army = null; unit.Category.RemoveUnit(unit); - OnUnitRemoved(unit, failedReqs); } public Unit[] GetUnits(Category cat) @@ -254,14 +253,19 @@ return count; } - private void Army_UnitAdded(object val) + private void Army_UnitAdded(WarFoundryObject val) { OnUnitAdded((Unit)val); - } - - private void Army_UnitRemoved(object val) + } + + private void Army_UnitRemoved(WarFoundryObject val) { OnUnitRemoved((Unit)val); + } + + private void Army_FailedRequirement(List<FailedUnitRequirement> val) + { + OnFailedRequirement(val); } } }
--- a/api/Objects/ArmyCategory.cs Sat Jun 27 18:59:49 2009 +0000 +++ b/api/Objects/ArmyCategory.cs Sat Jun 27 19:39:04 2009 +0000 @@ -21,7 +21,8 @@ private DoubleValChangedDelegate PointsValueChangedMethod; public event DoubleValChangedDelegate PointsValueChanged; public event ObjectAddDelegate UnitAdded; - public event ObjectRemoveDelegate UnitRemoved; + public event ObjectRemoveDelegate UnitRemoved; + public event FailedUnitRequirementDelegate FailedRequirement; public ArmyCategory(Army army, Category cat) : base() { @@ -74,16 +75,17 @@ unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount); unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1; TotalPoints+= unit.PointsValue; - OnUnitAdded(unit); + OnUnitAdded(unit, failedReqs); } internal void RemoveUnit(Unit unit) { + List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit); units.Remove(unit); unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1; TotalPoints-= unit.PointsValue; unit.PointsValueChanged-= PointsValueChangedMethod; - OnUnitRemoved(unit); + OnUnitRemoved(unit, failedReqs); } public int GetUnitTypeCount(UnitType unitType) @@ -122,23 +124,43 @@ { double diff = newVal - oldVal; TotalPoints+= diff; - } - } - - private void OnUnitAdded(Unit unit) - { - if (UnitAdded!=null) - { - UnitAdded(unit); - } - } - - private void OnUnitRemoved(Unit unit) - { - if (UnitRemoved!=null) - { - UnitRemoved(unit); - } + } + } + + protected void OnUnitAdded(Unit unit) + { + OnUnitAdded(unit, null); + } + + protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) + { + if (UnitAdded != null) + { + UnitAdded(unit); + } + + if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) + { + FailedRequirement(failedReqs); + } + } + + protected void OnUnitRemoved(Unit unit) + { + OnUnitRemoved(unit, null); + } + + protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) + { + if (UnitRemoved != null) + { + UnitRemoved(unit); + } + + if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) + { + FailedRequirement(failedReqs); + } } protected virtual void OnPointsValueChanged(double oldValue, double newValue)
--- a/api/Objects/Unit.cs Sat Jun 27 18:59:49 2009 +0000 +++ b/api/Objects/Unit.cs Sat Jun 27 19:39:04 2009 +0000 @@ -17,7 +17,6 @@ { private UnitType type; private int size; - private Army army; private Unit parentUnit; private double points; private ArmyCategory cat; @@ -25,13 +24,13 @@ private List<Unit> containedUnits = new List<Unit>(); public event DoubleValChangedDelegate PointsValueChanged; public event IntValChangedDelegate UnitSizeChanged; - public event DoubleValChangedDelegate UnitEquipmentAmountChanged; - - public Unit(UnitType unitType, Army parentArmy) : this(unitType, unitType.MinSize, parentArmy){} - - public Unit(UnitType unitType, int startSize, Army parentArmy) - { - Army = parentArmy; + public event DoubleValChangedDelegate UnitEquipmentAmountChanged; + + public Unit(UnitType unitType, ArmyCategory parentArmyCat) : this(unitType, unitType.MinSize, parentArmyCat) { } + + public Unit(UnitType unitType, int startSize, ArmyCategory parentArmyCat) + { + Category = parentArmyCat; type = unitType; Size = startSize; SetInitialEquipment(); @@ -143,16 +142,7 @@ public Army Army { - get { return army; } - set - { - army = value; - - if (army == null) - { - Category = null; - } - } + get { return (Category == null ? null : Category.ParentArmy); } } public Race Race @@ -161,24 +151,10 @@ } public ArmyCategory Category - { - get - { - if (cat==null) - { - if (Army!=null) - { - return Army.GetCategory(UnitType.MainCategory); - } - else - { - return null; - } - } - else - { - return cat; - } + { + get + { + return cat; } set { cat = value; } }