Changeset 146

Show
Ignore:
Timestamp:
06/27/09 14:39:04 (14 months ago)
Author:
ibboard
Message:

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
Location:
IBBoard.WarFoundry.API/trunk/api
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • IBBoard.WarFoundry.API/trunk/api/Commands/CreateAndAddUnitCommand.cs

    r143 r146  
    1313    { 
    1414        private UnitType addedUnitType; 
    15         private Army army; 
     15        private ArmyCategory armyCat; 
    1616        private Unit addedUnit; 
    1717         
    18         public CreateAndAddUnitCommand(UnitType toAdd, Army armyTo) 
     18        public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory armyCatTo) 
    1919        { 
    2020            addedUnitType = toAdd; 
    21             army = armyTo; 
     21            armyCat = armyCatTo; 
    2222        } 
    2323 
    2424        [Obsolete("Use two parameter constructor instead")] 
    25         public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory catTo, Army armyTo) : this(toAdd, armyTo) 
     25        public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory catTo, Army armyTo) : this(toAdd, catTo) 
    2626        { 
    2727        } 
     
    3434        public override bool CanExecute() 
    3535        { 
    36             return (addedUnitType!=null && army!=null); 
     36            return (addedUnitType!=null && armyCat!=null); 
    3737        } 
    3838 
     
    4949        public override bool Execute() 
    5050        { 
    51             addedUnit = new Unit(addedUnitType, army); 
     51            addedUnit = new Unit(addedUnitType, armyCat); 
    5252            this.Redo(); 
    5353            return true; 
     
    5656        public override void Redo() 
    5757        { 
    58             army.AddUnit(addedUnit); 
     58            armyCat.AddUnit(addedUnit); 
    5959        } 
    6060 
    6161        public override void Undo() 
    6262        { 
    63             army.RemoveUnit(addedUnit); 
     63            armyCat.RemoveUnit(addedUnit); 
    6464        } 
    6565 
  • IBBoard.WarFoundry.API/trunk/api/Objects/Army.cs

    r143 r146  
    7070                        cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded); 
    7171                        cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved); 
     72                        cat.FailedRequirement+=new FailedUnitRequirementDelegate(Army_FailedRequirement); 
    7273                    } 
    7374                } 
     
    102103        protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) 
    103104        { 
    104             if (UnitAdded!=null) 
     105            if (UnitAdded != null) 
    105106            { 
    106107                UnitAdded(unit); 
    107108            } 
    108109 
    109             if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0) 
    110             { 
    111                 FailedRequirement(failedReqs); 
    112             } 
     110            OnFailedRequirement(failedReqs); 
    113111        } 
    114112 
     
    125123            } 
    126124 
    127             if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0) 
     125            OnFailedRequirement(failedReqs); 
     126        } 
     127 
     128        protected void OnFailedRequirement(List<FailedUnitRequirement> failedReqs) 
     129        { 
     130            if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) 
    128131            { 
    129132                FailedRequirement(failedReqs); 
     
    161164        public void AddUnit(Unit unit) 
    162165        { 
    163             unit.Army = this; 
    164166            ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); 
    165167            armyCat.AddUnit(unit); 
     
    168170        public void RemoveUnit(Unit unit) 
    169171        { 
    170             List<FailedUnitRequirement> failedReqs = CanRemoveUnit(unit); 
    171             unit.Army = null; 
    172172            unit.Category.RemoveUnit(unit); 
    173             OnUnitRemoved(unit, failedReqs); 
    174173        } 
    175174 
     
    255254        } 
    256255 
    257         private void Army_UnitAdded(object val) 
     256        private void Army_UnitAdded(WarFoundryObject val) 
    258257        { 
    259258            OnUnitAdded((Unit)val); 
    260259        } 
    261260 
    262         private void Army_UnitRemoved(object val) 
     261        private void Army_UnitRemoved(WarFoundryObject val) 
    263262        { 
    264263            OnUnitRemoved((Unit)val); 
     264        } 
     265 
     266        private void Army_FailedRequirement(List<FailedUnitRequirement> val) 
     267        { 
     268            OnFailedRequirement(val); 
    265269        } 
    266270    } 
  • IBBoard.WarFoundry.API/trunk/api/Objects/ArmyCategory.cs

    r143 r146  
    2323        public event ObjectAddDelegate UnitAdded; 
    2424        public event ObjectRemoveDelegate UnitRemoved; 
     25        public event FailedUnitRequirementDelegate FailedRequirement; 
    2526 
    2627        public ArmyCategory(Army army, Category cat) : base() 
     
    7576            unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1; 
    7677            TotalPoints+= unit.PointsValue; 
    77             OnUnitAdded(unit); 
     78            OnUnitAdded(unit, failedReqs); 
    7879        } 
    7980 
    8081        internal void RemoveUnit(Unit unit) 
    8182        { 
     83            List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit); 
    8284            units.Remove(unit); 
    8385            unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1; 
    8486            TotalPoints-= unit.PointsValue; 
    8587            unit.PointsValueChanged-= PointsValueChangedMethod; 
    86             OnUnitRemoved(unit); 
     88            OnUnitRemoved(unit, failedReqs); 
    8789        } 
    8890 
     
    126128        } 
    127129 
    128         private void OnUnitAdded(Unit unit) 
     130        protected void OnUnitAdded(Unit unit) 
    129131        { 
    130             if (UnitAdded!=null) 
     132            OnUnitAdded(unit, null); 
     133        } 
     134 
     135        protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) 
     136        { 
     137            if (UnitAdded != null) 
    131138            { 
    132139                UnitAdded(unit); 
    133140            } 
     141 
     142            if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) 
     143            { 
     144                FailedRequirement(failedReqs); 
     145            } 
    134146        } 
    135147 
    136         private void OnUnitRemoved(Unit unit) 
     148        protected void OnUnitRemoved(Unit unit) 
    137149        { 
    138             if (UnitRemoved!=null) 
     150            OnUnitRemoved(unit, null); 
     151        } 
     152 
     153        protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) 
     154        { 
     155            if (UnitRemoved != null) 
    139156            { 
    140157                UnitRemoved(unit); 
     158            } 
     159 
     160            if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) 
     161            { 
     162                FailedRequirement(failedReqs); 
    141163            } 
    142164        } 
  • IBBoard.WarFoundry.API/trunk/api/Objects/Unit.cs

    r143 r146  
    1818        private UnitType type; 
    1919        private int size; 
    20         private Army army; 
    2120        private Unit parentUnit; 
    2221        private double points; 
     
    2827        public event DoubleValChangedDelegate UnitEquipmentAmountChanged; 
    2928 
    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; 
     29        public Unit(UnitType unitType, ArmyCategory parentArmyCat) : this(unitType, unitType.MinSize, parentArmyCat) { } 
     30 
     31        public Unit(UnitType unitType, int startSize, ArmyCategory parentArmyCat) 
     32        { 
     33            Category = parentArmyCat; 
    3534            type = unitType; 
    3635            Size = startSize; 
     
    144143        public Army Army 
    145144        { 
    146             get { return army; } 
    147             set  
    148             { 
    149                 army = value; 
    150  
    151                 if (army == null) 
    152                 { 
    153                     Category = null; 
    154                 } 
    155             } 
     145            get { return (Category == null ? null : Category.ParentArmy); } 
    156146        } 
    157147 
     
    163153        public ArmyCategory Category 
    164154        { 
    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                 } 
     155            get 
     156            { 
     157                return cat; 
    182158            } 
    183159            set { cat = value; }