Mercurial > repos > IBDev-IBBoard.WarFoundry.API
comparison API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs @ 458:680db2462e34
Re #379:
* Move GetObjectCountFromArmy(Army, OBJECT_TYPE) to top level and implement
* Fix army validation for appropriate "NA" returns
* Make basic "Requires..." requirements abstract so that we always need to make specific versions (e.g. UnitRequires... that knows how to check amount of units)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 26 Feb 2012 20:16:33 +0000 |
parents | afc6410e4efc |
children | f48c49b53738 |
comparison
equal
deleted
inserted
replaced
457:8e01c3174cc3 | 458:680db2462e34 |
---|---|
14 { | 14 { |
15 public UnitRequiresAtLeastNUnitsRequirement(UnitType requirementOn) : base(requirementOn) | 15 public UnitRequiresAtLeastNUnitsRequirement(UnitType requirementOn) : base(requirementOn) |
16 { | 16 { |
17 } | 17 } |
18 | 18 |
19 protected override bool IsApplicable(IWarFoundryObject toObjectAdded) | 19 public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) |
20 { | 20 { |
21 return base.IsApplicable(toObjectAdded) || IsRequirementOnType(toObjectAdded); | 21 return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; |
22 } | 22 } |
23 | 23 |
24 private bool IsRequirementOnType(IWarFoundryObject toObjectAdded) | 24 protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) |
25 { | 25 { |
26 return AllowedObject.Equals(toObjectAdded) || (toObjectAdded is Unit && AllowedObject.Equals(((Unit)toObjectAdded).UnitType)); | 26 return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy); |
27 } | 27 } |
28 | 28 |
29 protected override bool IsApplicable(Army toArmy) | 29 protected override bool IsApplicable(IWarFoundryObject toObject) |
30 { | 30 { |
31 return toArmy.GetUnitTypeCount(AllowedObject) > 0; | 31 return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType)); |
32 } | 32 } |
33 | 33 |
34 public override Validation ValidatesArmy(Army toArmy) | 34 private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy) |
35 { | 35 { |
36 return IsApplicable(toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable; | 36 bool isApplicable = false; |
37 UnitType addedType = toObject as UnitType; | |
38 | |
39 if (addedType == null) | |
40 { | |
41 addedType = (toObject is Unit) ? ((Unit)toObject).UnitType : null; | |
42 } | |
43 | |
44 if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0) | |
45 { | |
46 foreach (UnitCountRequirementData limit in ConstraintTypes) | |
47 { | |
48 if (Arrays.Contains(limit.UnitTypes, addedType)) | |
49 { | |
50 isApplicable = true; | |
51 break; | |
52 } | |
53 } | |
54 } | |
55 | |
56 return isApplicable; | |
57 } | |
58 | |
59 protected override int GetObjectCountFromArmy(Army toArmy, UnitType obj) | |
60 { | |
61 return toArmy.GetUnitTypeCount(obj); | |
37 } | 62 } |
38 } | 63 } |
39 } | 64 } |
40 | 65 |