Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class AI
- {
- public class AIMove
- {
- public ArmyView Army;
- public TileView Target;
- public AIMove(ArmyView army, TileView target)
- {
- Army = army;
- Target = target;
- }
- }
- public static AIMove RunAI(List<ArmyView> allies, List<ArmyView> enemies, TileView[,] world)
- {
- foreach (var ally in allies)
- {
- if (!ally.CanMove) continue;
- //move towards closest enemy
- ArmyView closestEnemy = null;
- int closestDistance = int.MaxValue;
- foreach (var enemy in enemies)
- {
- //can find path to target?
- if (PathFinder.FindPath(world, ally.CurrentTile.Position, enemy.CurrentTile.Position, ally.TeamID) == null)
- continue;
- //check if closest target
- float distance = Distance(ally, enemy);
- if (distance < closestDistance)
- {
- distance = closestDistance;
- closestEnemy = enemy;
- }
- }
- return new AIMove(ally, closestEnemy.CurrentTile);
- }
- return null;
- }
- private static float Distance(ArmyView army1, ArmyView army2)
- {
- return Vector2.Distance(army1.Position, army2.Position);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement