Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view api/Objects/ArmyCategory.cs @ 6:150a5669cd7b
Re #9 - more granular loading
* Remove SystemStatsSet class so that other classes don't know the internals of how GameSystem stores its stats (cleaner code principle)
* Make XML loader each stats set and add to the game system
* Add methods to GameSystem to remove use of SystemStatsSet and hide internal handling
* Add methods to add SystemStats to GameSystem
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 04 Jan 2009 12:13:59 +0000 |
parents | 520818033bb6 |
children | 306558904c2a |
line wrap: on
line source
using System; using System.Collections.Generic; using IBBoard.WarFoundry.API.Requirements; namespace IBBoard.WarFoundry.API.Objects { /// <summary> /// Summary description for ArmyCategory. /// </summary> public class ArmyCategory : WarFoundryObject { private Category category; private Army parentArmy; private double pointsTotal; private List<Unit> units; private Dictionary<string, int> unitTypes; private DoubleValChangedDelegate PointsValueChangedMethod; public event DoubleValChangedDelegate PointsValueChanged; public event ObjectAddDelegate UnitAdded; public event ObjectRemoveDelegate UnitRemoved; public event FailedUnitRequirementDelegate RequirementsFailed; public ArmyCategory(Army army, Category cat) : base() { parentArmy = army; category = cat; cat.NameChanged+=new StringValChangedDelegate(cat_NameChanged); PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler); units = new List<Unit>(); unitTypes = new Dictionary<string,int>(); } public Category Category { get { return category; } } public Army ParentArmy { get { return parentArmy; } } public override string ID { get { return Category.ID; } set { Category.ID = value; } } public override string Name { get { return category.Name; } set { category.Name = value; } } public void AddUnit(Unit unit) { List<FailedUnitRequirement> failedReqs = ParentArmy.CanAddUnit(unit); units.Add(unit); unit.Army = ParentArmy; unit.Category = this; unit.PointsValueChanged+= PointsValueChangedMethod; int unitTypeCount; unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount); unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1; TotalPoints+= unit.PointsValue; OnUnitAdded(unit, failedReqs); } public void RemoveUnit(Unit unit) { List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit); units.Remove(unit); unit.Army = null; unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1; TotalPoints-= unit.PointsValue; unit.PointsValueChanged-= PointsValueChangedMethod; OnUnitRemoved(unit, failedReqs); } public int GetUnitTypeCount(UnitType unitType) { return unitTypes.ContainsKey(unitType.ID) ? (int)unitTypes[unitType.ID] : 0; } public Unit[] GetUnits() { return units.ToArray(); } private double TotalPoints { get { return pointsTotal; } set { double oldVal = pointsTotal; pointsTotal = value; if (oldVal!=pointsTotal) { OnPointsValueChanged(oldVal, pointsTotal); } } } public double PointsTotal { get { return TotalPoints; } } private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal) { if (obj is Unit) { double diff = newVal - oldVal; TotalPoints+= diff; } } private void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) { if (UnitAdded!=null) { UnitAdded(unit); } if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count > 0) { RequirementsFailed(failedReqs); } } private void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) { if (UnitRemoved!=null) { UnitRemoved(unit); } if (RequirementsFailed!=null && failedReqs!=null && failedReqs.Count>0) { RequirementsFailed(failedReqs); } } protected virtual void OnPointsValueChanged(double oldValue, double newValue) { if (PointsValueChanged!=null) { PointsValueChanged(this, oldValue, newValue); } } protected void cat_NameChanged(WarFoundryObject obj, string oldValue, string newValue) { OnNameChanged(oldValue, newValue); } public int GetPointsPercentage() { return (int)Math.Round((PointsTotal / ParentArmy.MaxPoints) * 100, 0); } } }