Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view api/Objects/UnitType.cs @ 12:ac232763858b
Re #9 - Make WarFoundry API use smaller methods
* Obselete old Category and UnitType constructors and add minimal constructors
* Add setters to properties where needed
* Add value sanity checking to Category and UnitType
* Set default values for percentages, choices and points for Category
* Make XML factory use new constructor and setter properties
* Add DuplicateItemException to project file
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 18 Jan 2009 20:52:29 +0000 |
parents | 613bc5eaac59 |
children | 306558904c2a |
line wrap: on
line source
using System; using System.Collections; using System.Collections.Generic; using System.Xml; using IBBoard.Logging; using IBBoard.WarFoundry.API.Requirements; namespace IBBoard.WarFoundry.API.Objects { /// <summary> /// Summary description for Unit. /// </summary> public class UnitType : WarFoundryObject { protected Category mainCat; protected List<Category> categories = new List<Category>(); protected Race race; protected int min, max, baseSize = 0; protected int minSize, maxSize; protected double baseUnitCost; protected double costPerTrooper; protected Stats stats; protected List<UnitRequirement> requirements = new List<UnitRequirement>(); protected Hashtable equipment = new Hashtable(); protected Hashtable equipmentExclusionGroups = new Hashtable(); protected ArrayList equipmentKeyOrder = new ArrayList(); public UnitType(string id, string typeName, Race parentRace) : base(id, typeName) { race = parentRace; } [Obsolete("Use three parameter constructor and setters")] public UnitType(string id, string typeName, string mainCategoryID, string[] allCategoryIDs, int minNum, int maxNum, int minimumSize, int maximumSize, double unitCost, double trooperCost, Stats unitStats, UnitRequirement[] unitRequirements, Race parentRace) : base(id, typeName) { race = parentRace; mainCat = race.GetCategory(mainCategoryID); foreach (string catID in allCategoryIDs) { categories.Add(race.GetCategory(catID)); } MinNumber = minNum; MaxNumber = maxNum; MinSize = minimumSize; MaxSize = maximumSize; BaseUnitCost = unitCost; CostPerTrooper = trooperCost; UnitStats = unitStats; foreach (UnitRequirement requirement in requirements) { AddRequirement(requirement); } } public Race Race { get { return race; } } public virtual Category MainCategory { get { return mainCat; } set { if (value!=null && categories.Contains(value)) { mainCat = value; } else { throw new ArgumentException("MainCategory must exist in list of Categories"); } } } public virtual Category[] Categories { get { return categories.ToArray(); } } public void AddCategory(Category cat) { if (!categories.Contains(cat)) { categories.Add(cat); } } public int MinSize { get { return minSize; } set { minSize = (value >= 0 ? value : 0); CheckMinimumSize(); } } public int MaxSize { get { return maxSize; } set { maxSize = (value >= -1 ? value : -1); CheckMinimumSize(); } } public int BaseSize { get { return baseSize; } } public int MinNumber { get { return min; } set { min = (value >= 0 ? value : 0); CheckMinimumNumber(); } } public int MaxNumber { get { return max; } set { max = (value >= -1 ? value : -1); CheckMinimumNumber(); } } private void CheckMinimumNumber() { if (MinNumber > MaxNumber && MaxNumber!=-1) { MinNumber = MaxNumber; LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum number greater than their maximum number.", Name, ID); } } private void CheckMinimumSize() { if (MinSize > MaxSize && MaxSize!=-1) { MinSize = MaxNumber; LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum size greater than their maximum size.", Name, ID); } } public double BaseUnitCost { get { return baseUnitCost; } set { baseUnitCost = (value >= 0 ? value : 0); } } public double CostPerTrooper { get { return costPerTrooper; } set { costPerTrooper = (value >= 0 ? value : 0); } } protected override string DefaultName() { throw new InvalidOperationException("Unit type with id "+id+" did not have a name specified"); } public Stats UnitStats { get { return stats; } set { if (value!=null) { stats = value; } } } public UnitEquipmentItem GetEquipmentItem(string id) { return (UnitEquipmentItem)equipment[id]; } public UnitEquipmentItem[] GetEquipmentItems() { UnitEquipmentItem[] items = new UnitEquipmentItem[equipment.Count]; int i = 0; foreach (string itemID in equipmentKeyOrder) { items[i++] = (UnitEquipmentItem)equipment[itemID]; } return items; } public void AddRequirement(UnitRequirement requirement) { requirements.Add(requirement); } public List<FailedUnitRequirement> CanAddToArmy(Army army) { List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>(); if (requirements!=null && requirements.Count > 0) { foreach (UnitRequirement requirement in requirements) { FailedUnitRequirement failure = (FailedUnitRequirement)requirement.CanAddToWarFoundryObject(army); if (failure!=null) { failures.Add(failure); } } } return failures; } public List<FailedUnitRequirement> CanRemoveFromArmy(Army army) { List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>(); if (requirements!=null && requirements.Count > 0) { foreach (UnitRequirement requirement in requirements) { FailedUnitRequirement failure = (FailedUnitRequirement)requirement.CanRemoveFromWarFoundryObject(army); if (failure!=null) { failures.Add(failure); } } } return failures; } public UnitEquipmentItem[] GetEquipmentItemsByExclusionGroup(string group) { ArrayList list = (ArrayList)equipmentExclusionGroups[group]; if (list == null) { return new UnitEquipmentItem[0]; } else { return (UnitEquipmentItem[])list.ToArray(typeof(UnitEquipmentItem)); } } } }