public void GetNull() { UnitGroup unitGroup = new UnitGroup(); unitGroup.GetUnit(42); Assert.Fail(); }
public void GetUnit() { Unit unit = new Unit("Test", 42, "Test Unit 42"); UnitGroup unitGroup = new UnitGroup(); unitGroup.Add(unit); Unit gotUnit = unitGroup.GetUnit(unit.Id); Assert.AreEqual(unit.Name, gotUnit.Name); }
// TODO: handle the situation when one side is eliminated completely (end the combat) public bool ResolveAttack(UnitGroup targetGroup, int index, AttackRollResult attack) { Unit target; if (index > 0) { target = targetGroup.GetUnit(index); if (target == null) { Debug.Log("Invalid attack resolution target!"); return false; } if (target.Quantity <= 0) { Debug.Log("Zero Quantity!"); return false; } } else { target = targetGroup.GetRandomUnit(); } if (attackers.Contains(targetGroup) && attackers.Contains(attack.UnitGroup)) { Debug.Log("Invalid attack resolution target!"); return false; } if (defenders.Contains(targetGroup) && defenders.Contains(attack.UnitGroup)) { Debug.Log("Invalid attack resolution target!"); return false; } int defenseRoll = Dice.RollDie(10) + Dice.RollDie(10) + target.UnitType.Defense; if ((useCriticals && attack.IsCritical) || (attack.Attack > defenseRoll)) { Debug.Log(attack.Attack.ToString() + " attack vs " + defenseRoll + " defense: hit!"); int damage = attack.FullDamage; if (!attack.UnitGroup.UnitType.Attack.IsAP) { damage -= target.UnitType.Armor; } Debug.Log(attack.FullDamage.ToString() + " damage vs " + target.UnitType.Armor + " armor, result = " + damage); if (damage > 0) { target.Quantity -= 1; int newHP = target.CurrentHealth - damage; Debug.Log("HP: " + target.CurrentHealth + " - " + damage + " = " + newHP); if (newHP > 0) { Unit reinforced = targetGroup.GetUnitWithHealth(newHP); if (reinforced == null) { Debug.Log("Attempted to reinforce a null unit"); return false; } reinforced.Quantity += 1; } } } else { Debug.Log(attack.Attack.ToString() + " attack vs " + defenseRoll + " defense: miss."); } if (attackerRollResults.Contains(attack)) { attackerRollResults.Remove(attack); } else { defenderRollResults.Remove(attack); } /* if (attackerRollResults.Count == 0 && defenderRollResults.Count == 0) { if (CurrentPhase == attackPhase.CHARGE) { CurrentPhase = attackPhase.MELEE; } else { CurrentPhase = attackPhase.CHARGE; } } */ return true; }