| 1 | // This file (ArmyCategory.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 | |
|---|
| 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
|
|---|
| 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 DoubleValChangedDelegate PointsValueChanged;
|
|---|
| 23 | public event ObjectAddDelegate UnitAdded;
|
|---|
| 24 | public event ObjectRemoveDelegate UnitRemoved;
|
|---|
| 25 |
|
|---|
| 26 | public ArmyCategory(Army army, Category cat) : base()
|
|---|
| 27 | {
|
|---|
| 28 | parentArmy = army;
|
|---|
| 29 | category = cat;
|
|---|
| 30 | cat.NameChanged+=new StringValChangedDelegate(cat_NameChanged);
|
|---|
| 31 | PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler);
|
|---|
| 32 | units = new List<Unit>();
|
|---|
| 33 | unitTypes = new Dictionary<string,int>();
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public Category Category
|
|---|
| 37 | {
|
|---|
| 38 | get { return category; }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | public Army ParentArmy
|
|---|
| 42 | {
|
|---|
| 43 | get { return parentArmy; }
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | public override string ID
|
|---|
| 47 | {
|
|---|
| 48 | get
|
|---|
| 49 | {
|
|---|
| 50 | return Category.ID;
|
|---|
| 51 | }
|
|---|
| 52 | set
|
|---|
| 53 | {
|
|---|
| 54 | Category.ID = value;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public override string Name
|
|---|
| 59 | {
|
|---|
| 60 | get { return category.Name; }
|
|---|
| 61 | set
|
|---|
| 62 | {
|
|---|
| 63 | category.Name = value;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | internal void AddUnit(Unit unit)
|
|---|
| 68 | {
|
|---|
| 69 | units.Add(unit);
|
|---|
| 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);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | internal void RemoveUnit(Unit unit)
|
|---|
| 80 | {
|
|---|
| 81 | units.Remove(unit);
|
|---|
| 82 | unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1;
|
|---|
| 83 | TotalPoints-= unit.PointsValue;
|
|---|
| 84 | unit.PointsValueChanged-= PointsValueChangedMethod; |
|---|
| 85 | OnUnitRemoved(unit);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | public int GetUnitTypeCount(UnitType unitType)
|
|---|
| 89 | {
|
|---|
| 90 | return unitTypes.ContainsKey(unitType.ID) ? (int)unitTypes[unitType.ID] : 0;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | public Unit[] GetUnits()
|
|---|
| 94 | {
|
|---|
| 95 | return units.ToArray();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | private double TotalPoints
|
|---|
| 99 | {
|
|---|
| 100 | get { return pointsTotal; }
|
|---|
| 101 | set
|
|---|
| 102 | {
|
|---|
| 103 | double oldVal = pointsTotal;
|
|---|
| 104 | pointsTotal = value;
|
|---|
| 105 |
|
|---|
| 106 | if (oldVal!=pointsTotal)
|
|---|
| 107 | {
|
|---|
| 108 | OnPointsValueChanged(oldVal, pointsTotal);
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | public double PointsTotal
|
|---|
| 114 | {
|
|---|
| 115 | get { return TotalPoints; }
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
|
|---|
| 119 | {
|
|---|
| 120 | if (obj is Unit)
|
|---|
| 121 | {
|
|---|
| 122 | double diff = newVal - oldVal;
|
|---|
| 123 | TotalPoints+= diff;
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | private void OnUnitAdded(Unit unit)
|
|---|
| 128 | {
|
|---|
| 129 | if (UnitAdded!=null)
|
|---|
| 130 | {
|
|---|
| 131 | UnitAdded(unit);
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | private void OnUnitRemoved(Unit unit)
|
|---|
| 136 | {
|
|---|
| 137 | if (UnitRemoved!=null)
|
|---|
| 138 | {
|
|---|
| 139 | UnitRemoved(unit);
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | protected virtual void OnPointsValueChanged(double oldValue, double newValue)
|
|---|
| 144 | {
|
|---|
| 145 | if (PointsValueChanged!=null)
|
|---|
| 146 | {
|
|---|
| 147 | PointsValueChanged(this, oldValue, newValue);
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | protected void cat_NameChanged(WarFoundryObject obj, string oldValue, string newValue)
|
|---|
| 152 | {
|
|---|
| 153 | OnNameChanged(oldValue, newValue);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | public int GetPointsPercentage()
|
|---|
| 157 | {
|
|---|
| 158 | return (int)Math.Round((PointsTotal / ParentArmy.MaxPoints) * 100, 0);
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|