changeset 61:3c77722a02b5

Re #61 - Complete structure of WarFoundry API objects * Add containership support to Unit class * Add method to UnitType to check if a UnitType is of an allowed type
author IBBoard <dev@ibboard.co.uk>
date Thu, 09 Apr 2009 14:17:53 +0000
parents 0c3500fca49d
children e8735def0db5
files IBBoard.WarFoundry.API.csproj api/Objects/InvalidContainershipException.cs api/Objects/Unit.cs api/Objects/UnitType.cs
diffstat 4 files changed, 84 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/IBBoard.WarFoundry.API.csproj	Thu Apr 09 13:58:23 2009 +0000
     1.2 +++ b/IBBoard.WarFoundry.API.csproj	Thu Apr 09 14:17:53 2009 +0000
     1.3 @@ -124,6 +124,7 @@
     1.4      <Compile Include="api\Factories\Xml\WarFoundryXmlArmyFactory.cs" />
     1.5      <Compile Include="api\Factories\Xml\WarFoundryXmlFactoryUtils.cs" />
     1.6      <Compile Include="api\Factories\Xml\AbstractStagedLoadedSubFactory.cs" />
     1.7 +    <Compile Include="api\Objects\InvalidContainershipException.cs" />
     1.8    </ItemGroup>
     1.9    <ItemGroup>
    1.10      <Content Include="libs\ICSharpCode.SharpZipLib.dll" />
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/api/Objects/InvalidContainershipException.cs	Thu Apr 09 14:17:53 2009 +0000
     2.3 @@ -0,0 +1,46 @@
     2.4 +//  This file (InvalidContainershipException.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
     2.5 +// 
     2.6 +//  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.
     2.7 +// 
     2.8 +
     2.9 +using System;
    2.10 +
    2.11 +namespace IBBoard.WarFoundry.API.Objects
    2.12 +{
    2.13 +	/// <summary>
    2.14 +	/// A custom exception for when a unit was added as a sub-unit of another unit, but was not of a <see cref=" UnitType"/> that can be contained
    2.15 +	/// by that unit.
    2.16 +	/// </summary>
    2.17 +	public class InvalidContainershipException : Exception
    2.18 +	{
    2.19 +		private Unit containing;
    2.20 +		private Unit contained;
    2.21 +		
    2.22 +		public InvalidContainershipException(Unit containingUnit, Unit containedUnit) : base(CreateMessageString(containingUnit, containedUnit))
    2.23 +		{
    2.24 +			containing = containingUnit;
    2.25 +			contained = containedUnit;
    2.26 +		}
    2.27 +		
    2.28 +		private static string CreateMessageString(Unit containingUnit, Unit containedUnit)
    2.29 +		{
    2.30 +			return containingUnit.Name+" cannot contain "+containedUnit.Name+" because units of "+containingUnit.UnitType.Name+" cannot contain units of "+containedUnit.UnitType.Name;
    2.31 +		}
    2.32 +		
    2.33 +		/// <value>
    2.34 +		/// The <see cref=" Unit"/> that the contained unit was added to
    2.35 +		/// </value>
    2.36 +		public Unit ContainingUnit
    2.37 +		{
    2.38 +			get { return containing; }
    2.39 +		}
    2.40 +		
    2.41 +		/// <value>
    2.42 +		/// The <see cref=" Unit"/> that was added as a contained unit, but which was not of an allowed type
    2.43 +		/// </value>
    2.44 +		public Unit ContainedUnit
    2.45 +		{
    2.46 +			get { return contained; }
    2.47 +		}
    2.48 +	}
    2.49 +}
     3.1 --- a/api/Objects/Unit.cs	Thu Apr 09 13:58:23 2009 +0000
     3.2 +++ b/api/Objects/Unit.cs	Thu Apr 09 14:17:53 2009 +0000
     3.3 @@ -21,7 +21,8 @@
     3.4  		private Race race;
     3.5  		private double points;
     3.6  		private ArmyCategory cat;
     3.7 -		private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>();
     3.8 +		private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>();
     3.9 +		private List<Unit> containedUnits = new List<Unit>();
    3.10  		public event DoubleValChangedDelegate PointsValueChanged;
    3.11  		public event IntValChangedDelegate UnitSizeChanged;
    3.12  		public event DoubleValChangedDelegate UnitEquipmentAmountChanged;
    3.13 @@ -195,6 +196,31 @@
    3.14  
    3.15  				return points;
    3.16  			}
    3.17 +		}
    3.18 +		
    3.19 +		public Unit[] ContainedUnits
    3.20 +		{
    3.21 +			get { return containedUnits.ToArray(); }
    3.22 +		}
    3.23 +		
    3.24 +		public void AddContainedUnit(Unit unit)
    3.25 +		{
    3.26 +			if (UnitType.CanContainUnit(unit))
    3.27 +			{
    3.28 +				if (!containedUnits.Contains(unit))
    3.29 +				{
    3.30 +					containedUnits.Add(unit);
    3.31 +				}
    3.32 +			}
    3.33 +			else
    3.34 +			{
    3.35 +				throw new InvalidContainershipException(this, unit);
    3.36 +			}
    3.37 +		}
    3.38 +		
    3.39 +		public void RemoveContainedUnit(Unit unit)
    3.40 +		{
    3.41 +			containedUnits.Remove(unit);
    3.42  		}
    3.43  
    3.44  		public UnitEquipmentItem[] GetAllowedOptionalEquipment()
     4.1 --- a/api/Objects/UnitType.cs	Thu Apr 09 13:58:23 2009 +0000
     4.2 +++ b/api/Objects/UnitType.cs	Thu Apr 09 14:17:53 2009 +0000
     4.3 @@ -359,6 +359,16 @@
     4.4  			notes.Add(note);
     4.5  		}
     4.6  		
     4.7 +		public bool CanContainUnit(Unit unit)
     4.8 +		{
     4.9 +			return CanContainUnitType(unit.UnitType);
    4.10 +		}
    4.11 +		
    4.12 +		public bool CanContainUnitType(UnitType unitType)
    4.13 +		{
    4.14 +			return containedTypes.Contains(unitType);
    4.15 +		}
    4.16 +		
    4.17  		public UnitType[] ContainedUnitTypes
    4.18  		{
    4.19  			get { return containedTypes.ToArray(); }