Mercurial > repos > IBDev-IBBoard.WarFoundry.API
comparison api/Objects/ArmyCategory.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 IBBoard.WarFoundry.API.Requirements; | |
4 | |
5 namespace IBBoard.WarFoundry.API.Objects | |
6 { | |
7 /// <summary> | |
8 /// Summary description for ArmyCategory. | |
9 /// </summary> | |
10 public class ArmyCategory : WarFoundryObject | |
11 { | |
12 private Category category; | |
13 private Army parentArmy; | |
14 private double pointsTotal; | |
15 private List<Unit> units; | |
16 private Dictionary<string, int> unitTypes; | |
17 private DoubleValChangedDelegate PointsValueChangedMethod; | |
18 public event DoubleValChangedDelegate PointsValueChanged; | |
19 public event ObjectAddDelegate UnitAdded; | |
20 public event ObjectRemoveDelegate UnitRemoved; | |
21 public event FailedUnitRequirementDelegate RequirementsFailed; | |
22 | |
23 public ArmyCategory(Army army, Category cat) : base() | |
24 { | |
25 parentArmy = army; | |
26 category = cat; | |
27 cat.NameChanged+=new StringValChangedDelegate(cat_NameChanged); | |
28 PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler); | |
29 units = new List<Unit>(); | |
30 unitTypes = new Dictionary<string,int>(); | |
31 } | |
32 | |
33 public Category Category | |
34 { | |
35 get { return category; } | |
36 } | |
37 | |
38 public Army ParentArmy | |
39 { | |
40 get { return parentArmy; } | |
41 } | |
42 | |
43 public override string ID | |
44 { | |
45 get | |
46 { | |
47 return Category.ID; | |
48 } | |
49 set | |
50 { | |
51 Category.ID = value; | |
52 } | |
53 } | |
54 | |
55 public override string Name | |
56 { | |
57 get { return category.Name; } | |
58 set | |
59 { | |
60 category.Name = value; | |
61 } | |
62 } | |
63 | |
64 public void AddUnit(Unit unit) | |
65 { | |
66 List<FailedUnitRequirement> failedReqs = ParentArmy.CanAddUnit(unit); | |
67 | |
68 units.Add(unit); | |
69 unit.Army = ParentArmy; | |
70 unit.Category = this; | |
71 unit.PointsValueChanged+= PointsValueChangedMethod; | |
72 int unitTypeCount; | |
73 unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount); | |
74 unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1; | |
75 TotalPoints+= unit.PointsValue; | |
76 OnUnitAdded(unit, failedReqs); | |
77 } | |
78 | |
79 public void RemoveUnit(Unit unit) | |
80 { | |
81 List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit); | |
82 units.Remove(unit); | |
83 unit.Army = null; | |
84 unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1; | |
85 TotalPoints-= unit.PointsValue; | |
86 unit.PointsValueChanged-= PointsValueChangedMethod; | |
87 OnUnitRemoved(unit, failedReqs); | |
88 } | |
89 | |
90 public int GetUnitTypeCount(UnitType unitType) | |
91 { | |
92 return unitTypes.ContainsKey(unitType.ID) ? (int)unitTypes[unitType.ID] : 0; | |
93 } | |
94 | |
95 public Unit[] GetUnits() | |
96 { | |
97 return units.ToArray(); | |
98 } | |
99 | |
100 private double TotalPoints | |
101 { | |
102 get { return pointsTotal; } | |
103 set | |
104 { | |
105 double oldVal = pointsTotal; | |
106 pointsTotal = value; | |
107 | |
108 if (oldVal!=pointsTotal) | |
109 { | |
110 OnPointsValueChanged(oldVal, pointsTotal); | |
111 } | |
112 } | |
113 } | |
114 | |
115 public double PointsTotal | |
116 { | |
117 get { return TotalPoints; } | |
118 } | |
119 | |
120 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal) | |
121 { | |
122 if (obj is Unit) | |
123 { | |
124 double diff = newVal - oldVal; | |
125 TotalPoints+= diff; | |
126 } | |
127 } | |
128 | |
129 private void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) | |
130 { | |
131 if (UnitAdded!=null) | |
132 { | |
133 UnitAdded(unit); | |
134 } | |
135 | |
136 if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count > 0) | |
137 { | |
138 RequirementsFailed(failedReqs); | |
139 } | |
140 } | |
141 | |
142 private void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) | |
143 { | |
144 if (UnitRemoved!=null) | |
145 { | |
146 UnitRemoved(unit); | |
147 } | |
148 | |
149 if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count>0) | |
150 { | |
151 RequirementsFailed(failedReqs); | |
152 } | |
153 } | |
154 | |
155 protected virtual void OnPointsValueChanged(double oldValue, double newValue) | |
156 { | |
157 if (PointsValueChanged!=null) | |
158 { | |
159 PointsValueChanged(this, oldValue, newValue); | |
160 } | |
161 } | |
162 | |
163 protected void cat_NameChanged(WarFoundryObject obj, string oldValue, string newValue) | |
164 { | |
165 OnNameChanged(oldValue, newValue); | |
166 } | |
167 | |
168 public int GetPointsPercentage() | |
169 { | |
170 return (int)Math.Round((PointsTotal / ParentArmy.MaxPoints) * 100, 0); | |
171 } | |
172 } | |
173 } |