comparison API/Objects/ArmyCategory.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children 1a70ca80ef41
comparison
equal deleted inserted replaced
336:3631c1493c7f 337:3c4a6403a88c
1 // This file (ArmyCategory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 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
5 using System;
6 using System.Collections.Generic;
7 using IBBoard.WarFoundry.API.Requirements;
8
9 namespace IBBoard.WarFoundry.API.Objects
10 {
11 /// <summary>
12 /// Summary description for ArmyCategory.
13 /// </summary>
14 public class ArmyCategory : WarFoundryObject, ICostedWarFoundryObject
15 {
16 private Category category;
17 private Army parentArmy;
18 private double pointsTotal;
19 private List<Unit> units;
20 private Dictionary<string, int> unitTypes;
21 private DoubleValChangedDelegate PointsValueChangedMethod;
22 public event ObjectAddDelegate UnitAdded;
23 public event ObjectRemoveDelegate UnitRemoved;
24 public event FailedUnitRequirementDelegate FailedRequirement;
25 public event DoubleValChangedDelegate PointsValueChanged;
26
27 public ArmyCategory(Army army, Category cat) : base()
28 {
29 parentArmy = army;
30 category = cat;
31 cat.NameChanged+=new StringValChangedDelegate(cat_NameChanged);
32 PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler);
33 units = new List<Unit>();
34 unitTypes = new Dictionary<string,int>();
35 }
36
37 public Category Category
38 {
39 get { return category; }
40 }
41
42 public Army ParentArmy
43 {
44 get { return parentArmy; }
45 }
46
47 public override string ID
48 {
49 get
50 {
51 return Category.ID;
52 }
53 set
54 {
55 Category.ID = value;
56 }
57 }
58
59 public override string Name
60 {
61 get { return category.Name; }
62 set
63 {
64 category.Name = value;
65 }
66 }
67
68 internal void AddUnit(Unit unit)
69 {
70 List<FailedUnitRequirement> failedReqs = ParentArmy.CanAddUnit(unit);
71 units.Add(unit);
72 unit.Category = this;
73 unit.PointsValueChanged+= PointsValueChangedMethod;
74 int unitTypeCount;
75 unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount);
76 unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1;
77 TotalPoints+= unit.Points;
78 OnUnitAdded(unit, failedReqs);
79 }
80
81 internal void RemoveUnit(Unit unit)
82 {
83 List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit);
84 units.Remove(unit);
85 unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1;
86 TotalPoints-= unit.Points;
87 unit.PointsValueChanged-= PointsValueChangedMethod;
88 OnUnitRemoved(unit, failedReqs);
89 }
90
91 public int GetUnitTypeCount(UnitType unitType)
92 {
93 return unitTypes.ContainsKey(unitType.ID) ? (int)unitTypes[unitType.ID] : 0;
94 }
95
96 public Unit[] GetUnits()
97 {
98 return units.ToArray();
99 }
100
101 private double TotalPoints
102 {
103 get { return pointsTotal; }
104 set
105 {
106 double oldVal = pointsTotal;
107 pointsTotal = value;
108
109 if (oldVal!=pointsTotal)
110 {
111 OnPointsValueChanged(oldVal, pointsTotal);
112 }
113 }
114 }
115
116 public double Points
117 {
118 get { return TotalPoints; }
119 }
120
121 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
122 {
123 if (obj is Unit)
124 {
125 double diff = newVal - oldVal;
126 TotalPoints+= diff;
127 }
128 }
129
130 protected void OnUnitAdded(Unit unit)
131 {
132 OnUnitAdded(unit, null);
133 }
134
135 protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs)
136 {
137 if (UnitAdded != null)
138 {
139 UnitAdded(unit);
140 }
141
142 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0)
143 {
144 FailedRequirement(failedReqs);
145 }
146 }
147
148 protected void OnUnitRemoved(Unit unit)
149 {
150 OnUnitRemoved(unit, null);
151 }
152
153 protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs)
154 {
155 if (UnitRemoved != null)
156 {
157 UnitRemoved(unit);
158 }
159
160 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0)
161 {
162 FailedRequirement(failedReqs);
163 }
164 }
165
166 protected virtual void OnPointsValueChanged(double oldValue, double newValue)
167 {
168 if (PointsValueChanged!=null)
169 {
170 PointsValueChanged(this, oldValue, newValue);
171 }
172 }
173
174 protected void cat_NameChanged(WarFoundryObject obj, string oldValue, string newValue)
175 {
176 OnNameChanged(oldValue, newValue);
177 }
178
179 public int GetPointsPercentage()
180 {
181 return (int)Math.Round((Points / ParentArmy.MaxPoints) * 100, 0);
182 }
183 }
184 }