root/IBBoard.WarFoundry.API/trunk/api/Objects/Army.cs @ 143

Revision 143, 5.7 KB (checked in by ibboard, 15 months ago)
  • Fix line terminators

no-open-ticket

Line 
1// This file (Army.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard.
2//
3// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
4
5using System;
6using System.IO;
7using System.Collections.Generic;
8using System.Text;
9using System.Xml;
10using IBBoard.WarFoundry.API;
11using IBBoard.WarFoundry.API.Factories;
12using IBBoard.WarFoundry.API.Requirements;
13using ICSharpCode.SharpZipLib.Zip;
14
15namespace IBBoard.WarFoundry.API.Objects
16{
17    /// <summary>
18    /// Summary description for Army.
19    /// </summary>
20    public class Army : WarFoundryObject
21    {
22        //private GameSystem system;
23        private Race armyRace;
24        private int maxPoints;
25        private double pointsTotal;
26        private Dictionary<Category, ArmyCategory> categories;
27
28        public event ObjectAddDelegate UnitAdded;
29        public event ObjectRemoveDelegate UnitRemoved;
30        public event FailedUnitRequirementDelegate FailedRequirement;
31        public event DoubleValChangedDelegate PointsValueChanged;
32        private DoubleValChangedDelegate PointsValueChangedMethod;
33       
34        public Army(Race race, string armyName, int maxArmyPoints) : this(race, armyName, maxArmyPoints, null)
35        {
36        }
37
38        public Army(Race race, string armyName, int maxArmyPoints, ZipFile file) : base(armyName)
39        {
40            armyRace = race;
41            Name = armyName;
42            maxPoints = maxArmyPoints;
43            PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler);
44        }
45       
46        public ArmyCategory GetCategory(Category cat)
47        {
48            ArmyCategory armyCat = null;
49            ArmyCategories.TryGetValue(cat, out armyCat);
50            return armyCat;
51        }
52       
53        private Dictionary<Category, ArmyCategory> ArmyCategories
54        {
55            get
56            {
57                if (categories==null)
58                {
59                    categories = new Dictionary<Category, ArmyCategory>();
60                    Category[] raceCats = Race.Categories;
61                    ArmyCategory cat;
62                    int raceCatCount = raceCats.Length;
63
64                    for (int i = 0; i < raceCatCount; i++)
65                    {
66                        Category raceCat = raceCats[i];
67                        cat = new ArmyCategory(this, raceCat);
68                        categories[raceCat] = cat;
69                        cat.PointsValueChanged+= PointsValueChangedMethod;
70                        cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded);
71                        cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved);
72                    }
73                }
74               
75                return categories;
76            }
77        }
78
79        public ArmyCategory[] Categories
80        {
81            get
82            {
83                return DictionaryUtils.ToArray<Category, ArmyCategory>(ArmyCategories);
84            }
85        }
86
87        public Race Race
88        {
89            get { return armyRace; }
90        }
91
92        public GameSystem GameSystem
93        {
94            get { return (armyRace!=null ? armyRace.GameSystem : null); }
95        }
96
97        protected void OnUnitAdded(Unit unit)
98        {
99            OnUnitAdded(unit, null);
100        }
101
102        protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs)
103        {
104            if (UnitAdded!=null)
105            {
106                UnitAdded(unit);
107            }
108
109            if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0)
110            {
111                FailedRequirement(failedReqs);
112            }
113        }
114
115        protected void OnUnitRemoved(Unit unit)
116        {
117            OnUnitRemoved(unit, null);
118        }
119
120        protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs)
121        {
122            if (UnitRemoved!=null)
123            {
124                UnitRemoved(unit);
125            }
126
127            if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0)
128            {
129                FailedRequirement(failedReqs);
130            }
131        }
132
133        private void OnPointsValueChanged(double oldValue, double newValue)
134        {
135            if (PointsValueChanged!=null)
136            {
137                PointsValueChanged(this, oldValue, newValue);
138            }
139        }
140       
141        private double TotalPoints
142        {
143            get { return pointsTotal; }
144            set
145            {
146                double oldPoints = pointsTotal;
147                pointsTotal = value;
148
149                if (oldPoints!=pointsTotal)
150                {
151                    OnPointsValueChanged(oldPoints, pointsTotal);
152                }
153            }
154        }
155
156        public double PointsTotal
157        {
158            get { return TotalPoints; }
159        }
160       
161        public void AddUnit(Unit unit)
162        {
163            unit.Army = this;
164            ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory);
165            armyCat.AddUnit(unit);
166        }
167       
168        public void RemoveUnit(Unit unit)
169        {
170            List<FailedUnitRequirement> failedReqs = CanRemoveUnit(unit);
171            unit.Army = null;
172            unit.Category.RemoveUnit(unit);
173            OnUnitRemoved(unit, failedReqs);
174        }
175
176        public Unit[] GetUnits(Category cat)
177        {
178            return GetUnits(this.GetCategory(cat));
179        }
180
181        public Unit[] GetUnits(ArmyCategory cat)
182        {
183            return cat.GetUnits();
184        }
185
186        public Unit[] GetUnits()
187        {
188            List<Unit> fullList = new List<Unit>();
189
190            foreach(ArmyCategory cat in Categories)
191            {
192                fullList.AddRange(cat.GetUnits());
193            }
194
195            return fullList.ToArray();
196        }
197
198        public int MaxPoints
199        {
200            get { return maxPoints; }
201            set
202            {
203                if (value > 0)
204                {
205                    maxPoints = value;
206                }
207            }
208        }
209
210        private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
211        {
212            if (obj is ArmyCategory)
213            {
214                double points = 0;
215
216                foreach (ArmyCategory cat in Categories)
217                {
218                    points+= cat.PointsTotal;
219                }
220
221                TotalPoints = points;
222            }
223        }
224
225        public List<FailedUnitRequirement> CanAddUnit(Unit unit)
226        {
227            return CanAddUnitType(unit.UnitType);
228        }
229
230        public List<FailedUnitRequirement> CanAddUnitType(UnitType unitType)
231        {
232            return unitType.CanAddToArmy(this);
233        }
234
235        public List<FailedUnitRequirement> CanRemoveUnit(Unit unit)
236        {
237            return CanRemoveUnitType(unit.UnitType);
238        }
239
240        public List<FailedUnitRequirement> CanRemoveUnitType(UnitType unitType)
241        {
242            return unitType.CanRemoveFromArmy(this);
243        }
244
245        public int GetUnitTypeCount(UnitType unitType)
246        {
247            int count = 0;
248
249            foreach (ArmyCategory cat in Categories)
250            {
251                count+= cat.GetUnitTypeCount(unitType);
252            }
253
254            return count;
255        }
256
257        private void Army_UnitAdded(object val)
258        {
259            OnUnitAdded((Unit)val);
260        }
261
262        private void Army_UnitRemoved(object val)
263        {
264            OnUnitRemoved((Unit)val);
265        }
266    }
267}
Note: See TracBrowser for help on using the browser.