view API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs @ 345:008537acf244

Re #27: Unit requirements * Make basic "Requires at least" requirement take into account the unit type being added * Make unit version of "requires at least" requirement pass execution up to the matching parent method
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 11:00:09 +0000
parents 7bd2a7cdbfbd
children 6c0404277cad
line source
1 // This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
4 using System;
5 using System.Collections.Generic;
6 using IBBoard.WarFoundry.API.Objects;
8 namespace IBBoard.WarFoundry.API.Objects.Requirement
9 {
10 /// <summary>
11 /// A requirement where a UnitType requires at least N units of one or more unit types before any number of that object can be taken in an army.
12 /// </summary>
13 public class UnitRequiresAtLeastNUnitsRequirement : RequiresAtLeastNUnitsRequirement
14 {
15 private UnitType requirementOnType;
17 public UnitRequiresAtLeastNUnitsRequirement(UnitType requirementOn) : base()
18 {
19 requirementOnType = requirementOn;
20 }
22 /// <summary>
23 /// Checks whether the supplied WarFoundryObject can be added to the supplied army.
24 /// </summary>
25 /// <returns>
26 /// A <code>Validation</code> enum to show the result of the validation
27 /// </returns>
28 /// <param name='wfObject'>
29 /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
30 /// </param>
31 /// <param name='toArmy'>
32 /// The army to add the object to.
33 /// </param>
34 public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
35 {
36 return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
37 }
40 private bool IsApplicable(WarFoundryObject toObject, Army toArmy)
41 {
42 return IsApplicable(toArmy) || IsApplicable(toObject);
43 }
46 private bool IsApplicable(Army toArmy)
47 {
48 return toArmy.GetUnitTypeCount(requirementOnType) > 0;
49 }
52 private bool IsApplicable(WarFoundryObject toObject)
53 {
54 return requirementOnType.Equals(toObject) || (toObject is Unit && requirementOnType.Equals(((Unit)toObject).UnitType));
55 }
58 public override Validation ValidatesArmy(Army toArmy)
59 {
60 return IsApplicable(toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable;
61 }
62 }
63 }