view API/Objects/Army.cs @ 367:1a70ca80ef41

Re #27: Define unit requirements * Remove remaining references to old Requirements namespace
author IBBoard <dev@ibboard.co.uk>
date Sat, 28 May 2011 15:51:54 +0000
parents 4993e6f7c509
children 3f297a60db1e
line source
1 // This file (Army.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.
5 using System;
6 using System.IO;
7 using System.Collections.Generic;
8 using System.Text;
9 using System.Xml;
10 using IBBoard.WarFoundry.API;
11 using IBBoard.WarFoundry.API.Factories;
12 using ICSharpCode.SharpZipLib.Zip;
13 using IBBoard.WarFoundry.API.Objects.Requirement;
15 namespace IBBoard.WarFoundry.API.Objects
16 {
17 /// <summary>
18 /// Summary description for Army.
19 /// </summary>
20 public class Army : WarFoundryLoadedObject, ICostedWarFoundryObject
21 {
22 //private GameSystem system;
23 private Race armyRace;
24 private int maxPoints;
25 private double pointsTotal;
26 private Dictionary<Category, ArmyCategory> categories;
28 public event ObjectAddDelegate UnitAdded;
29 public event ObjectRemoveDelegate UnitRemoved;
30 public event DoubleValChangedDelegate PointsValueChanged;
31 private DoubleValChangedDelegate PointsValueChangedMethod;
33 public Army(Race race, string armyName, int maxArmyPoints) : this(race, armyName, maxArmyPoints, null)
34 {
35 }
37 public Army(Race race, string armyName, int maxArmyPoints, ZipFile file) : base(armyName)
38 {
39 armyRace = race;
40 Name = armyName;
41 maxPoints = maxArmyPoints;
42 PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler);
43 }
45 public ArmyCategory GetCategory(Category cat)
46 {
47 ArmyCategory armyCat = null;
48 ArmyCategories.TryGetValue(cat, out armyCat);
49 return armyCat;
50 }
52 private Dictionary<Category, ArmyCategory> ArmyCategories
53 {
54 get
55 {
56 if (categories==null)
57 {
58 categories = new Dictionary<Category, ArmyCategory>();
59 Category[] raceCats = Race.Categories;
60 ArmyCategory cat;
61 int raceCatCount = raceCats.Length;
63 for (int i = 0; i < raceCatCount; i++)
64 {
65 Category raceCat = raceCats[i];
66 cat = new ArmyCategory(this, raceCat);
67 categories[raceCat] = cat;
68 cat.PointsValueChanged+= PointsValueChangedMethod;
69 cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded);
70 cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved);
71 }
72 }
74 return categories;
75 }
76 }
78 public ArmyCategory[] Categories
79 {
80 get
81 {
82 return DictionaryUtils.ToArray<Category, ArmyCategory>(ArmyCategories);
83 }
84 }
86 public Race Race
87 {
88 get { return armyRace; }
89 }
91 public GameSystem GameSystem
92 {
93 get { return (armyRace!=null ? armyRace.GameSystem : null); }
94 }
96 protected void OnUnitAdded(Unit unit)
97 {
98 if (UnitAdded != null)
99 {
100 UnitAdded(unit);
101 }
102 }
104 protected void OnUnitRemoved(Unit unit)
105 {
106 if (UnitRemoved!=null)
107 {
108 UnitRemoved(unit);
109 }
110 }
112 private void OnPointsValueChanged(double oldValue, double newValue)
113 {
114 if (PointsValueChanged!=null)
115 {
116 PointsValueChanged(this, oldValue, newValue);
117 }
118 }
120 private double TotalPoints
121 {
122 get { return pointsTotal; }
123 set
124 {
125 double oldPoints = pointsTotal;
126 pointsTotal = value;
128 if (oldPoints!=pointsTotal)
129 {
130 OnPointsValueChanged(oldPoints, pointsTotal);
131 }
132 }
133 }
135 public double Points
136 {
137 get { return TotalPoints; }
138 }
140 public void AddUnit(Unit unit)
141 {
142 Category category = unit.UnitType.MainCategory;
143 AddUnit(unit, category);
144 }
146 public void AddUnit(Unit unit, Category category)
147 {
148 ArmyCategory armyCat = GetCategory(category);
149 armyCat.AddUnit(unit);
150 }
152 public void RemoveUnit(Unit unit)
153 {
154 unit.Category.RemoveUnit(unit);
155 }
157 public Unit[] GetUnits(Category cat)
158 {
159 return GetUnits(this.GetCategory(cat));
160 }
162 public Unit[] GetUnits(ArmyCategory cat)
163 {
164 return cat.GetUnits();
165 }
167 public Unit[] GetUnits()
168 {
169 List<Unit> fullList = new List<Unit>();
171 foreach(ArmyCategory cat in Categories)
172 {
173 fullList.AddRange(cat.GetUnits());
174 }
176 return fullList.ToArray();
177 }
179 public int MaxPoints
180 {
181 get { return maxPoints; }
182 set
183 {
184 if (value > 0)
185 {
186 maxPoints = value;
187 }
188 }
189 }
191 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
192 {
193 if (obj is ArmyCategory)
194 {
195 double points = 0;
197 foreach (ArmyCategory cat in Categories)
198 {
199 points+= cat.Points;
200 }
202 TotalPoints = points;
203 }
204 }
206 public int GetUnitTypeCount(UnitType unitType)
207 {
208 int count = 0;
210 foreach (ArmyCategory cat in Categories)
211 {
212 count+= cat.GetUnitTypeCount(unitType);
213 }
215 return count;
216 }
218 private void Army_UnitAdded(WarFoundryObject val)
219 {
220 OnUnitAdded((Unit)val);
221 }
223 private void Army_UnitRemoved(WarFoundryObject val)
224 {
225 OnUnitRemoved((Unit)val);
226 }
228 public ICollection<IRequirement> GetRequirements ()
229 {
230 return Race.GetRequirements();
231 }
232 }
233 }