public buildings ClosestBuilding() { int xDis = 0, yDis = 0; double distance = 1000; double temp = 1000; buildings target = null; foreach (buildings u in building) { if (u is FactoryBuildings) { FactoryBuildings b = (FactoryBuildings)u; if (factionType != b.Faction) { xDis = Math.Abs((PosX - b.PosX) * (PosX - b.PosX)); yDis = Math.Abs((PosY - b.PosY) * (PosY - b.PosY)); distance = Math.Round(Math.Sqrt(xDis + yDis), 0); } } else if (u is ResourceBuilding) { ResourceBuilding b = (ResourceBuilding)u; if (factionType != b.Faction) { xDis = Math.Abs((PosX - b.PosX) * (PosX - b.PosX)); yDis = Math.Abs((PosY - b.PosY) * (PosY - b.PosY)); distance = Math.Round(Math.Sqrt(xDis + yDis), 0); } } if (distance < temp) { temp = distance; target = u; } } return(target); }
public override void CheckAttackRange(List <Unit> uni, List <buildings> build) { units = uni; building = build; closestUnit = ClosestEnemy(); closestBuilding = ClosestBuilding(); int enemyType; int xDis = 0, yDis = 0; int uDistance = 10000, bDistance = 10000; int distance; if (closestUnit is MeleeUnit) { MeleeUnit M = (MeleeUnit)closestUnit; xDis = Math.Abs((PosX - M.PosX) * (PosX - M.PosX)); yDis = Math.Abs((PosY - M.PosY) * (PosY - M.PosY)); uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } else if (closestUnit is RangedUnit) { RangedUnit R = (RangedUnit)closestUnit; xDis = Math.Abs((PosX - R.PosX) * (PosX - R.PosX)); yDis = Math.Abs((PosY - R.PosY) * (PosY - R.PosY)); uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } else if (closestUnit is WizzardUnit) { WizzardUnit R = (WizzardUnit)closestUnit; xDis = Math.Abs((PosX - R.PosX) * (PosX - R.PosX)); yDis = Math.Abs((PosY - R.PosY) * (PosY - R.PosY)); uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } if (closestBuilding is FactoryBuildings) { FactoryBuildings FB = (FactoryBuildings)closestBuilding; xDis = Math.Abs((PosX - FB.PosX) * (PosX - FB.PosX)); yDis = Math.Abs((PosY - FB.PosY) * (PosY - FB.PosY)); bDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } else if (closestBuilding is ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)closestBuilding; xDis = Math.Abs((PosX - RB.PosX) * (PosX - RB.PosX)); yDis = Math.Abs((PosY - RB.PosY) * (PosY - RB.PosY)); bDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } if (units[0] != null) { if (uDistance < bDistance) { distance = uDistance; enemyType = 0; } else { distance = bDistance; enemyType = 1; } } else { distance = bDistance; enemyType = 1; } //Checks to see if they are below 25% health so they move rather than attacking if (Health > MaxHealth * 0.25) { if (distance <= AttackRange) { IsAttacking = true; Combat(enemyType); } else { IsAttacking = false; Move(enemyType); } } else { Move(enemyType); } }