changeset 57:e0ce5578e7c2

Re #61 - Complete structure of WarFoundry API objects * Add methods for adding and getting Notes and contained UnitTypes of a UnitType * Add method to get requirements of a unit * Separate out required abilities from optional abilities * Remove UnitAbility and handle by passing a boolean to the AddAbility method
author IBBoard <dev@ibboard.co.uk>
date Tue, 07 Apr 2009 14:43:04 +0000
parents 9561ef46c6fb
children e53ed2d613a1
files IBBoard.WarFoundry.API.csproj api/Objects/UnitAbility.cs api/Objects/UnitType.cs
diffstat 3 files changed, 57 insertions(+), 129 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.csproj	Sun Apr 05 19:57:32 2009 +0000
+++ b/IBBoard.WarFoundry.API.csproj	Tue Apr 07 14:43:04 2009 +0000
@@ -124,7 +124,6 @@
     <Compile Include="api\Factories\Xml\WarFoundryXmlArmyFactory.cs" />
     <Compile Include="api\Factories\Xml\WarFoundryXmlFactoryUtils.cs" />
     <Compile Include="api\Factories\Xml\AbstractStagedLoadedSubFactory.cs" />
-    <Compile Include="api\Objects\UnitAbility.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="libs\ICSharpCode.SharpZipLib.dll" />
--- a/api/Objects/UnitAbility.cs	Sun Apr 05 19:57:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,67 +0,0 @@
-//  This file (UnitAbility.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
-// 
-//  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.
-// 
-
-using System;
-
-namespace IBBoard.WarFoundry.API.Objects
-{
-	public class UnitAbility : WarFoundryObject
-	{		
-		private Ability ability;
-		private bool isRequired;
-		
-		public UnitAbility(Ability ability)
-		{
-			this.ability = ability;
-		}
-		
-		public Ability Ability
-		{
-			get { return ability; }
-		}
-		
-		public bool IsRequired
-		{
-			get { return isRequired; }
-			set { isRequired = value; }
-		}
-		
-		public override int GetHashCode ()
-		{
-			return typeof(UnitAbility).GetHashCode() + ability.GetHashCode();
-		}
-		
-		public override bool Equals (object obj)
-		{
-			bool equal = true;
-			
-			if (obj == this)
-			{
-				equal = true;
-			}
-			else if (obj != null && GetType().Equals(obj.GetType()))
-			{
-				UnitAbility other = (UnitAbility)obj;
-				
-				if (!Ability.Equals(other.Ability))
-				{
-					equal = false;
-				}
-				else if (IsRequired != other.IsRequired)
-				{
-					equal = false;
-				}
-			}
-			else
-			{
-				equal = false;
-			}
-			
-			return equal;
-		}
-
-
-	}
-}
--- a/api/Objects/UnitType.cs	Sun Apr 05 19:57:32 2009 +0000
+++ b/api/Objects/UnitType.cs	Tue Apr 07 14:43:04 2009 +0000
@@ -26,7 +26,10 @@
 		private Dictionary<string, UnitEquipmentItem> equipment = new Dictionary<string, UnitEquipmentItem>();
 		private Dictionary<string, List<UnitEquipmentItem>> equipmentExclusionGroups = new Dictionary<string, List<UnitEquipmentItem>>();
 		private List<string> equipmentKeyOrder = new List<string>();
-		private Dictionary<string, UnitAbility> abilities = new Dictionary<string, UnitAbility>();
+		private Dictionary<string, Ability> requiredAbilities = new Dictionary<string, Ability>();
+		private Dictionary<string, Ability> optionalAbilities = new Dictionary<string, Ability>();
+		private List<String> notes = new List<string>();
+		private List<UnitType> containedTypes = new List<UnitType>();
 
 
 		public UnitType(string id, string typeName, Race parentRace) : base(id, typeName)
@@ -251,71 +254,59 @@
 		/// </returns>
 		public UnitEquipmentItem[] GetEquipmentItems()
 		{
-			UnitEquipmentItem[] items = new UnitEquipmentItem[equipment.Count];
-			int i = 0;
-
-			foreach (string itemID in equipmentKeyOrder)
-			{
-				items[i++] = equipment[itemID];
-			}
-
-			return items;
+			return DictionaryUtils.ToArray<string, UnitEquipmentItem>(equipment);
 		}
-		
-		public ICollection<Ability> GetAbilities()
+
+		public UnitEquipmentItem[] GetEquipmentItemsByExclusionGroup(string group)
 		{
-			ICollection<Ability> abilitiesList = new List<Ability>();
-			
-			foreach (UnitAbility ability in abilities.Values)
+			List<UnitEquipmentItem> list = null;
+			equipmentExclusionGroups.TryGetValue(group, out list);
+
+			if (list == null)
 			{
-				abilitiesList.Add(ability.Ability);
+				return new UnitEquipmentItem[0];
 			}
-			
-			return abilitiesList;
+			else
+			{
+				return list.ToArray();
+			}
 		}
 		
 		public ICollection<Ability> GetRequiredAbilities()
 		{
-			ICollection<Ability> requiredAbilities = new List<Ability>();
-			
-			foreach (UnitAbility ability in abilities.Values)
-			{
-				if (ability.IsRequired)
-				{
-					requiredAbilities.Add(ability.Ability);
-				}
-			}
-			
-			return requiredAbilities;
+			return requiredAbilities.Values;
 		}
 		
 		public ICollection<Ability> GetOptionalAbilities()
 		{
-			ICollection<Ability> optionalAbilities = new List<Ability>();
-			
-			foreach (UnitAbility ability in abilities.Values)
-			{
-				if (!ability.IsRequired)
-				{
-					optionalAbilities.Add(ability.Ability);
-				}
-			}
-			
-			return optionalAbilities;
+			return optionalAbilities.Values;
 		}
 		
-		public void AddAbility(Ability ability)
-		{			
-			if (!abilities.ContainsKey(ability.ID))
-			{
-				UnitAbility unitAbility = new UnitAbility(ability);
-				abilities[ability.ID] = unitAbility;
+		public void AddAbility(Ability ability, bool isRequired)
+		{
+			string id = ability.ID;
+			
+			if (!requiredAbilities.ContainsKey(id) && !optionalAbilities.ContainsKey(id))
+			{				
+				if (isRequired)
+				{
+					requiredAbilities[id] = ability;
+				}
+				else
+				{
+					optionalAbilities[id] = ability;
+				}
 			}
 		}
 
 		public void AddRequirement(UnitRequirement requirement)
 		{
 			requirements.Add(requirement);
+		}
+		
+		public UnitRequirement[] Requirements
+		{
+			get { return requirements.ToArray(); }
 		}
 		
 		public List<FailedUnitRequirement> CanAddToArmy(Army army)
@@ -356,21 +347,26 @@
 			}
 			
 			return failures;
-		}
-
-		public UnitEquipmentItem[] GetEquipmentItemsByExclusionGroup(string group)
-		{
-			List<UnitEquipmentItem> list = null;
-			equipmentExclusionGroups.TryGetValue(group, out list);
-
-			if (list == null)
-			{
-				return new UnitEquipmentItem[0];
-			}
-			else
-			{
-				return list.ToArray();
-			}
+		}
+		
+		public string[] Notes
+		{
+			get { return notes.ToArray(); }
+		}
+		
+		public void AddNote(string note)
+		{
+			notes.Add(note);
+		}
+		
+		public UnitType[] ContainedUnitTypes
+		{
+			get { return containedTypes.ToArray(); }
+		}
+		
+		public void AddContainedUnitType(UnitType containedType)
+		{
+			containedTypes.Add(containedType);
 		}
 	}
 }