daily pastebin goal
35%
SHARE
TWEET

Desert Strife AI - Version 1

Huvaakoodia Sep 6th, 2016 140 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class AI
  6. {
  7.     public class AIMove
  8.     {
  9.         public ArmyView Army;
  10.         public TileView Target;
  11.  
  12.         public AIMove(ArmyView army, TileView target)
  13.         {
  14.             Army = army;
  15.             Target = target;
  16.         }
  17.     }
  18.  
  19.     public static AIMove RunAI(List<ArmyView> allies, List<ArmyView> enemies, TileView[,] world)
  20.     {
  21.         foreach (var ally in allies)
  22.         {
  23.             if (!ally.CanMove) continue;
  24.  
  25.             //move towards closest enemy
  26.             ArmyView closestEnemy = null;
  27.             int closestDistance = int.MaxValue;
  28.             foreach (var enemy in enemies)
  29.             {
  30.                 //can find path to target?
  31.                 if (PathFinder.FindPath(world, ally.CurrentTile.Position, enemy.CurrentTile.Position, ally.TeamID) == null)
  32.                     continue;
  33.  
  34.                 //check if closest target
  35.                 float distance = Distance(ally, enemy);
  36.                 if (distance < closestDistance)
  37.                 {
  38.                     distance = closestDistance;
  39.                     closestEnemy = enemy;
  40.                 }
  41.             }
  42.             return new AIMove(ally, closestEnemy.CurrentTile);
  43.         }
  44.         return null;
  45.     }
  46.  
  47.     private static float Distance(ArmyView army1, ArmyView army2)
  48.     {
  49.         return Vector2.Distance(army1.Position, army2.Position);
  50.     }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top