changeset 471:0a2068897793

Re #359: Add "only contained" attribute to unit types * Add "main units" methods to get only top-level units * Add nesting to unit creation command
author IBBoard <dev@ibboard.co.uk>
date Sun, 15 Apr 2012 20:52:32 +0100
parents 426b8c5e283c
children e6c93ceba119
files API/Commands/CreateAndAddUnitCommand.cs API/Objects/Army.cs API/Objects/ArmyCategory.cs
diffstat 3 files changed, 30 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/API/Commands/CreateAndAddUnitCommand.cs	Sat Apr 14 20:44:22 2012 +0100
+++ b/API/Commands/CreateAndAddUnitCommand.cs	Sun Apr 15 20:52:32 2012 +0100
@@ -14,11 +14,13 @@
 		private UnitType addedUnitType;
 		private ArmyCategory armyCat;
 		private Unit addedUnit;
+		private Unit parentUnit;
 
-		public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory armyCatTo)
+		public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory armyCatTo, Unit parentUnit = null)
 		{
 			addedUnitType = toAdd;
 			armyCat = armyCatTo;
+			this.parentUnit = parentUnit;
 		}
 
 		public override bool CanExecute()
@@ -45,6 +47,7 @@
 		public override bool Execute()
 		{
 			addedUnit = new Unit(addedUnitType, armyCat);
+			addedUnit.ParentUnit = parentUnit;
 			this.Redo();
 			return true;
 		}
--- a/API/Objects/Army.cs	Sat Apr 14 20:44:22 2012 +0100
+++ b/API/Objects/Army.cs	Sun Apr 15 20:52:32 2012 +0100
@@ -170,7 +170,7 @@
 
 		public Unit[] GetUnits(Category cat)
 		{
-			return GetUnits(this.GetCategory(cat));
+			return GetUnits(GetCategory(cat));
 		}
 
 		public Unit[] GetUnits(ArmyCategory cat)
@@ -178,6 +178,16 @@
 			return cat.GetUnits();
 		}
 
+		public Unit[] GetMainUnits(Category cat)
+		{
+			return GetMainUnits(GetCategory(cat));
+		}
+
+		public Unit[] GetMainUnits(ArmyCategory cat)
+		{
+			return cat.GetMainUnits();
+		}
+
 		public Unit[] GetUnits()
 		{
 			List<Unit> fullList = new List<Unit>();
--- a/API/Objects/ArmyCategory.cs	Sat Apr 14 20:44:22 2012 +0100
+++ b/API/Objects/ArmyCategory.cs	Sun Apr 15 20:52:32 2012 +0100
@@ -98,6 +98,21 @@
 			return units.ToArray();
 		}
 
+		public Unit[] GetMainUnits()
+		{
+			List<Unit> mainUnits = new List<Unit>();
+
+			foreach (Unit unit in units)
+			{
+				if (unit.ParentUnit == null)
+				{
+					mainUnits.Add(unit);
+				}
+			}
+
+			return mainUnits.ToArray();
+		}
+
 		private double TotalPoints
 		{
 			get { return pointsTotal; }