Mercurial > repos > snowblizz-super-API-ideas
changeset 58:e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
* Migrate AddUnit/RemoveUnit methods to Army for easier army loading
* Migrate requirement checking call to Army, since ArmyCategory just called parent army anyway
Also:
* Use genericed collections in Army
* Remove failed unit requirements from ArmyCategory
* Alter army.xsd to stop negatives in equipment amounts
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 07 Apr 2009 15:28:06 +0000 |
parents | e0ce5578e7c2 |
children | 3fa4658c50c6 |
files | api/Commands/CreateAndAddUnitCommand.cs api/Objects/Army.cs api/Objects/ArmyCategory.cs dtds/army.xsd |
diffstat | 4 files changed, 78 insertions(+), 85 deletions(-) [+] |
line wrap: on
line diff
--- a/api/Commands/CreateAndAddUnitCommand.cs Tue Apr 07 14:43:04 2009 +0000 +++ b/api/Commands/CreateAndAddUnitCommand.cs Tue Apr 07 15:28:06 2009 +0000 @@ -13,16 +13,20 @@ { private UnitType addedUnitType; private Army army; - private ArmyCategory cat; - private Unit addedUnit; - - public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory catTo, Army armyTo) + private Unit addedUnit; + + public CreateAndAddUnitCommand(UnitType toAdd, Army armyTo) + { + addedUnitType = toAdd; + army = armyTo; + } + + [Obsolete("Use two parameter constructor instead")] + public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory catTo, Army armyTo) : this(toAdd, armyTo) { - addedUnitType = toAdd; - cat = catTo; - army = armyTo; } - + + [Obsolete("Use two parameter constructor instead")] public CreateAndAddUnitCommand(UnitType toAdd, Category catTo, Army armyTo) : this (toAdd, armyTo.GetCategory(catTo), armyTo) { } @@ -51,12 +55,12 @@ public override void Redo() { - cat.AddUnit(addedUnit); + army.AddUnit(addedUnit); } public override void Undo() { - cat.RemoveUnit(addedUnit); + army.RemoveUnit(addedUnit); }
--- a/api/Objects/Army.cs Tue Apr 07 14:43:04 2009 +0000 +++ b/api/Objects/Army.cs Tue Apr 07 15:28:06 2009 +0000 @@ -3,8 +3,7 @@ // 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. using System; -using System.IO; -using System.Collections; +using System.IO; using System.Collections.Generic; using System.Text; using System.Xml; @@ -24,7 +23,7 @@ private Race armyRace; private int maxPoints; private double pointsTotal; - private ArmyCategory[] categories; + private Dictionary<Category, ArmyCategory> categories; public event ObjectAddDelegate UnitAdded; public event ObjectRemoveDelegate UnitRemoved; @@ -46,40 +45,42 @@ public ArmyCategory GetCategory(Category cat) { - foreach (ArmyCategory armyCat in Categories) - { - if (armyCat.Category.Equals(cat)) - { - return armyCat; - } - } - - return null; + ArmyCategory armyCat = null; + ArmyCategories.TryGetValue(cat, out armyCat); + return armyCat; + } + + private Dictionary<Category, ArmyCategory> ArmyCategories + { + get + { + if (categories==null) + { + categories = new Dictionary<Category, ArmyCategory>(); + Category[] raceCats = Race.Categories; + ArmyCategory cat; + int raceCatCount = raceCats.Length; + + for (int i = 0; i < raceCatCount; i++) + { + Category raceCat = raceCats[i]; + cat = new ArmyCategory(this, raceCat); + categories[raceCat] = cat; + cat.PointsValueChanged+= PointsValueChangedMethod; + cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded); + cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved); + } + } + + return categories; + } } public ArmyCategory[] Categories { get { - if (categories==null) - { - Category[] raceCats = Race.Categories; - ArmyCategory cat; - int raceCatCount = raceCats.Length; - categories = new ArmyCategory[raceCatCount]; - - for (int i = 0; i < raceCatCount; i++) - { - cat = new ArmyCategory(this, raceCats[i]); - categories[i] = cat; - cat.PointsValueChanged+= PointsValueChangedMethod; - cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded); - cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved); - cat.RequirementsFailed+=new FailedUnitRequirementDelegate(Army_FailedRequirement); - } - } - - return categories; + return DictionaryUtils.ToArray<Category, ArmyCategory>(ArmyCategories); } } @@ -155,6 +156,24 @@ public double PointsTotal { get { return TotalPoints; } + } + + public void AddUnit(Unit unit) + { + List<FailedUnitRequirement> failedReqs = CanAddUnit(unit); + unit.Army = this; + ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); + armyCat.AddUnit(unit); + OnUnitAdded(unit, failedReqs); + } + + public void RemoveUnit(Unit unit) + { + List<FailedUnitRequirement> failedReqs = CanRemoveUnit(unit); + unit.Army = null; + ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); + armyCat.RemoveUnit(unit); + OnUnitRemoved(unit, failedReqs); } public Unit[] GetUnits(Category cat) @@ -169,20 +188,14 @@ public Unit[] GetUnits() { - ArrayList fullList = new ArrayList(); + List<Unit> fullList = new List<Unit>(); foreach(ArmyCategory cat in Categories) - { - foreach(Unit unit in cat.GetUnits()) - { - if (!fullList.Contains(unit)) - { - fullList.Add(unit); - } - } + { + fullList.AddRange(cat.GetUnits()); } - return (Unit[])fullList.ToArray(typeof(Unit)); + return fullList.ToArray(); } public int MaxPoints @@ -253,13 +266,5 @@ { OnUnitRemoved((Unit)val); } - - private void Army_FailedRequirement(List<FailedUnitRequirement> failedRequirements) - { - if (FailedRequirement!=null) - { - FailedRequirement(failedRequirements); - } - } } }
--- a/api/Objects/ArmyCategory.cs Tue Apr 07 14:43:04 2009 +0000 +++ b/api/Objects/ArmyCategory.cs Tue Apr 07 15:28:06 2009 +0000 @@ -22,7 +22,6 @@ public event DoubleValChangedDelegate PointsValueChanged; public event ObjectAddDelegate UnitAdded; public event ObjectRemoveDelegate UnitRemoved; - public event FailedUnitRequirementDelegate RequirementsFailed; public ArmyCategory(Army army, Category cat) : base() { @@ -65,30 +64,25 @@ } } - public void AddUnit(Unit unit) + internal void AddUnit(Unit unit) { - List<FailedUnitRequirement> failedReqs = ParentArmy.CanAddUnit(unit); - units.Add(unit); - unit.Army = ParentArmy; unit.Category = this; unit.PointsValueChanged+= PointsValueChangedMethod; int unitTypeCount; unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount); unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1; - TotalPoints+= unit.PointsValue; - OnUnitAdded(unit, failedReqs); + TotalPoints+= unit.PointsValue; + OnUnitAdded(unit); } - public void RemoveUnit(Unit unit) + internal void RemoveUnit(Unit unit) { - List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit); - units.Remove(unit); - unit.Army = null; + units.Remove(unit); unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1; TotalPoints-= unit.PointsValue; - unit.PointsValueChanged-= PointsValueChangedMethod; - OnUnitRemoved(unit, failedReqs); + unit.PointsValueChanged-= PointsValueChangedMethod; + OnUnitRemoved(unit); } public int GetUnitTypeCount(UnitType unitType) @@ -130,30 +124,20 @@ } } - private void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) + private void OnUnitAdded(Unit unit) { if (UnitAdded!=null) { UnitAdded(unit); } - - if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count > 0) - { - RequirementsFailed(failedReqs); - } } - private void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) + private void OnUnitRemoved(Unit unit) { if (UnitRemoved!=null) { UnitRemoved(unit); } - - if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count>0) - { - RequirementsFailed(failedReqs); - } } protected virtual void OnPointsValueChanged(double oldValue, double newValue)
--- a/dtds/army.xsd Tue Apr 07 14:43:04 2009 +0000 +++ b/dtds/army.xsd Tue Apr 07 15:28:06 2009 +0000 @@ -35,7 +35,7 @@ </xs:complexType> <xs:complexType name="equipitemtype"> <xs:attribute name="id" type="xs:string" /> <!-- ID reference to either a custom equipment item or a Race equipment item --> - <xs:attribute name="amount" type="xs:double" use="required"/><!-- Double used to allow for percentages to be stored --> + <xs:attribute name="amount" type="core:nonNegativeDouble" use="required"/><!-- Double used to allow for percentages to be stored --> <xs:attribute name="isCustomEquipment" type="xs:boolean" default="false"/> </xs:complexType> <xs:complexType name="customequipmenttype">