Mercurial > repos > IBBoard.WarFoundry.API
changeset 350:6c0404277cad
Re #27: Unit requirements
* Rebuild base "at least" requirement to say "N/A" when added unit isn't constrained
* Remove duplicate code from "unit requires at least" requirement and override where necessary
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 09 Apr 2011 19:57:40 +0000 |
parents | 12a56786120c |
children | d97016f2515b |
files | API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs |
diffstat | 2 files changed, 65 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Sat Apr 09 19:14:11 2011 +0000 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Sat Apr 09 19:57:40 2011 +0000 @@ -38,8 +38,66 @@ /// </param> public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) { + return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + } + + private bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy) + { + return IsApplicable(toArmy) || IsApplicable(toObjectAdded); + } + + protected virtual bool IsApplicable(Army toArmy) + { + return false; + } + + protected virtual bool IsApplicable(WarFoundryObject toObject) + { + bool isApplicable = false; + UnitType unitType = GetUnitTypeFromObject(toObject); + + if (unitType != null) + { + isApplicable = IsApplicable(unitType); + } + + return isApplicable; + } + + protected UnitType GetUnitTypeFromObject (WarFoundryObject toObject) + { + UnitType unitType = null; + + if (toObject is UnitType) + { + unitType = (UnitType)toObject; + } + else if (toObject is Unit) + { + unitType = ((Unit)toObject).UnitType; + } + + return unitType; + } + + private bool IsApplicable (UnitType unitType) + { + bool isApplicable = false; + foreach (UnitCountRequirementData requirement in requiredTypes) + { + if (requirement.UnitType.Equals(unitType)) + { + isApplicable = true; + break; + } + } + return isApplicable; + } + + private Validation CheckAllowsAdding(WarFoundryObject wfObject, Army toArmy) + { Validation isValid = Validation.Passed; - + foreach (UnitCountRequirementData requirement in requiredTypes) { if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count) @@ -48,7 +106,7 @@ break; } } - + return isValid; }
--- a/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs Sat Apr 09 19:14:11 2011 +0000 +++ b/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs Sat Apr 09 19:57:40 2011 +0000 @@ -19,42 +19,21 @@ requirementOnType = requirementOn; } - /// <summary> - /// Checks whether the supplied WarFoundryObject can be added to the supplied army. - /// </summary> - /// <returns> - /// A <code>Validation</code> enum to show the result of the validation - /// </returns> - /// <param name='wfObject'> - /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement - /// </param> - /// <param name='toArmy'> - /// The army to add the object to. - /// </param> - public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) + protected override bool IsApplicable(WarFoundryObject toObjectAdded) { - return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + return base.IsApplicable(toObjectAdded) || IsRequirementOnType(toObjectAdded); } - - private bool IsApplicable(WarFoundryObject toObject, Army toArmy) + private bool IsRequirementOnType(WarFoundryObject toObjectAdded) { - return IsApplicable(toArmy) || IsApplicable(toObject); + return requirementOnType.Equals(toObjectAdded) || (toObjectAdded is Unit && requirementOnType.Equals(((Unit)toObjectAdded).UnitType)); } - - private bool IsApplicable(Army toArmy) + protected override bool IsApplicable(Army toArmy) { return toArmy.GetUnitTypeCount(requirementOnType) > 0; } - - private bool IsApplicable(WarFoundryObject toObject) - { - return requirementOnType.Equals(toObject) || (toObject is Unit && requirementOnType.Equals(((Unit)toObject).UnitType)); - } - - public override Validation ValidatesArmy(Army toArmy) { return IsApplicable(toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable;