Mercurial > repos > IBBoard.WarFoundry.API
annotate api/Objects/ArmyCategory.cs @ 211:1f39e325deee
Fixes #219: Units can only have one contained type
* Commit Snowblizz's schema fix that makes the contained unit type list "unbounded"
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 21 Nov 2009 11:50:48 +0000 |
parents | 938409fc24cc |
children | 92d10b06ab0f |
rev | line source |
---|---|
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
1 // This file (ArmyCategory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard. |
15 | 2 // |
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
89
diff
changeset
|
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. |
15 | 4 |
82 | 5 using System; |
0 | 6 using System.Collections.Generic; |
82 | 7 using IBBoard.WarFoundry.API.Requirements; |
8 | |
9 namespace IBBoard.WarFoundry.API.Objects | |
10 { | |
11 /// <summary> | |
12 /// Summary description for ArmyCategory. | |
13 /// </summary> | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
14 public class ArmyCategory : WarFoundryObject, ICostedWarFoundryObject |
82 | 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; | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
22 public event ObjectAddDelegate UnitAdded; |
147
938409fc24cc
Fixes #175: Add attribute to schema to define which stats set to use
IBBoard <dev@ibboard.co.uk>
parents:
143
diff
changeset
|
23 public event ObjectRemoveDelegate UnitRemoved; |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
24 public event FailedUnitRequirementDelegate FailedRequirement; |
82 | 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; | |
0 | 73 unit.PointsValueChanged+= PointsValueChangedMethod; |
82 | 74 int unitTypeCount; |
75 unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount); | |
76 unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1; | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
77 TotalPoints+= unit.Points; |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
78 OnUnitAdded(unit, failedReqs); |
82 | 79 } |
80 | |
81 internal void RemoveUnit(Unit unit) | |
82 { | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
83 List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit); |
82 | 84 units.Remove(unit); |
85 unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1; | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
86 TotalPoints-= unit.Points; |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
87 unit.PointsValueChanged-= PointsValueChangedMethod; |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
88 OnUnitRemoved(unit, failedReqs); |
82 | 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 | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
116 [Obsolete("Use Points instead")] |
82 | 117 public double PointsTotal |
118 { | |
119 get { return TotalPoints; } | |
120 } | |
121 | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
122 public double Points |
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
123 { |
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
124 get { return TotalPoints; } |
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
125 } |
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
126 |
82 | 127 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal) |
128 { | |
129 if (obj is Unit) | |
130 { | |
131 double diff = newVal - oldVal; | |
132 TotalPoints+= diff; | |
89 | 133 } |
134 } | |
135 | |
136 protected void OnUnitAdded(Unit unit) | |
137 { | |
138 OnUnitAdded(unit, null); | |
139 } | |
140 | |
141 protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) | |
142 { | |
143 if (UnitAdded != null) | |
144 { | |
145 UnitAdded(unit); | |
146 } | |
147 | |
148 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) | |
149 { | |
150 FailedRequirement(failedReqs); | |
151 } | |
152 } | |
153 | |
154 protected void OnUnitRemoved(Unit unit) | |
155 { | |
156 OnUnitRemoved(unit, null); | |
157 } | |
158 | |
159 protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) | |
160 { | |
161 if (UnitRemoved != null) | |
162 { | |
163 UnitRemoved(unit); | |
164 } | |
165 | |
166 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) | |
167 { | |
168 FailedRequirement(failedReqs); | |
169 } | |
82 | 170 } |
171 | |
172 protected virtual void OnPointsValueChanged(double oldValue, double newValue) | |
173 { | |
174 if (PointsValueChanged!=null) | |
175 { | |
176 PointsValueChanged(this, oldValue, newValue); | |
177 } | |
178 } | |
179 | |
180 protected void cat_NameChanged(WarFoundryObject obj, string oldValue, string newValue) | |
181 { | |
182 OnNameChanged(oldValue, newValue); | |
183 } | |
184 | |
185 public int GetPointsPercentage() | |
186 { | |
143
7f13ffcb8765
Re #164: Show unit cost in army tree
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
187 return (int)Math.Round((Points / ParentArmy.MaxPoints) * 100, 0); |
82 | 188 } |
189 } | |
190 } |