Mercurial > repos > IBBoard.WarFoundry.API
comparison api/Requirements/UnitRequiresAtLeastRequirement.cs @ 0:520818033bb6
Initial commit of WarFoundry code
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 19 Dec 2008 15:57:51 +0000 |
parents | |
children | 306558904c2a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:520818033bb6 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Text; | |
4 using IBBoard.WarFoundry.API; | |
5 using IBBoard.WarFoundry.API.Objects; | |
6 | |
7 namespace IBBoard.WarFoundry.API.Requirements | |
8 { | |
9 /// <summary> | |
10 /// Summary description for UnitRequiresRequirement. | |
11 /// </summary> | |
12 public class UnitRequiresAtLeastRequirement : UnitRequirement | |
13 { | |
14 private UnitType[] requiredTypes; | |
15 private int[] requiredCounts; | |
16 private String unitList; | |
17 | |
18 public UnitRequiresAtLeastRequirement(UnitType type, UnitType requiredUnitType) : this(type, new UnitType[]{requiredUnitType}, new int[]{1}) | |
19 { | |
20 } | |
21 | |
22 public UnitRequiresAtLeastRequirement(UnitType type, UnitType[] requiredUnitTypes, int[] minNumsRequired) : base(type) | |
23 { | |
24 if (requiredUnitTypes.Length != minNumsRequired.Length) | |
25 { | |
26 throw new ArgumentException("List of required unit types and list of number of units required must be equal"); | |
27 } | |
28 else if (requiredUnitTypes.Length == 1) | |
29 { | |
30 throw new ArgumentException("List of required unit types must not be empty"); | |
31 } | |
32 | |
33 requiredTypes = requiredUnitTypes; | |
34 requiredCounts = minNumsRequired; | |
35 | |
36 if (requiredTypes.Length > 1) | |
37 { | |
38 StringBuilder sb = new StringBuilder(requiredCounts[0]+" x "+requiredTypes[0].Name); | |
39 | |
40 for (int i = 1; i<requiredTypes.Length; i++) | |
41 { | |
42 sb.Append(", "+requiredCounts[i]+" x "+requiredTypes[i].Name); | |
43 } | |
44 | |
45 unitList = sb.ToString(); | |
46 } | |
47 else | |
48 { | |
49 unitList = requiredTypes[0].Name; | |
50 } | |
51 } | |
52 | |
53 public override string Description | |
54 { | |
55 get { return "The army must include at least the following units to include a unit of type "+unitType.Name+": "+unitList; } | |
56 } | |
57 | |
58 protected override AbstractFailedRequirement CanRemoveFromArmy(Army army, UnitType type) | |
59 { | |
60 return null; | |
61 } | |
62 | |
63 protected override AbstractFailedRequirement CanAddToArmy(Army army, UnitType type) | |
64 { | |
65 FailedRequirement failure = null; | |
66 int count = requiredTypes.Length; | |
67 | |
68 for (int i = 0; i < count; i++) | |
69 { | |
70 if (army.GetUnitTypeCount(requiredTypes[i]) < requiredCounts[i]) | |
71 { | |
72 failure = new FailedRequirement(this); | |
73 break; | |
74 } | |
75 } | |
76 | |
77 return failure; | |
78 } | |
79 } | |
80 } |