Advertisement
Guest User

Abstract AI

a guest
Apr 23rd, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. //PUT THE AI BELOW THE SPELL SCRIPT OTHERWISE SPELLS WILL NOT WORK!
  5. //ALL SPELL NAMES MUST MATCH THE NAME OF THE PREFAB THEY ARE ATTACHED TO EXACTLY!
  6. namespace Assets.Scripts
  7. {
  8.     public abstract class AbstractAI : MonoBehaviour
  9.     {
  10.  
  11.         //Various Variables used throughout the script
  12.         GameObject Player;
  13.         Transform Target;
  14.         //bool SpellGet = false;
  15.         //bool SpellsGot = false;
  16.         bool CanCast = false;
  17.         bool Casting = false;
  18.         bool Cast = false;
  19.         bool CanAttack = false;
  20.         Transform[] SpellsArray;
  21.         Transform[] DeliveryMethodArray;
  22.         //These are the values to be changed with each enemy
  23.         float MinimumRange;
  24.         float CurrentRange;
  25.         float CurrentMoveSpeed;
  26.         int CurrentHealth;
  27.         float StrikeRange;
  28.         float SpellRange;
  29.         //These are the starting values, they are to be returned when a debuff ends
  30.         int MaxHealth;
  31.         float MaxMoveSpeed;
  32.         float MaxRange;
  33.         //AI Booleans
  34.         bool Offensive = false;
  35.         bool Defensive = false;
  36.         bool Misc = false;
  37.         bool SendMessage = false;
  38.  
  39.         // Use this for initialization
  40.         void Start()
  41.         {
  42.             //Puts all of the children on this gameObject into an object array (Used in the spells section of the script)
  43.             SpellsArray = new Transform[transform.childCount];
  44.             //Max Stats Set
  45.             SpellRange = 10;
  46.             StrikeRange = 5;
  47.             MaxHealth = 10;
  48.             MaxMoveSpeed = 10;
  49.             MaxRange = 25;
  50.             //Current Stats Set
  51.             CurrentHealth = MaxHealth;
  52.             CurrentMoveSpeed = MaxMoveSpeed;
  53.             CurrentRange = MaxRange;
  54.             //Transforms and GO Set
  55.             Player = GameObject.FindGameObjectWithTag("Player");
  56.             Target = Player.transform;
  57.             SpellsArray = PopulateSpellArray();
  58.             DeliveryMethodArray = DeliveryMethodHandling(SpellsArray);
  59.         }
  60.  
  61.         // Update is called once per frame
  62.         void Update()
  63.         {
  64.             //--------------------------------------------------------- BASIC EVENTS AND STATISTICS
  65.             //Range Check
  66.             CurrentRange = Vector2.Distance(new Vector2(transform.position.x, transform.position.y), new Vector2(Target.position.x, Target.position.y));
  67.  
  68.  
  69.             //On Death
  70.             if (CurrentHealth == 0)
  71.             {
  72.                 Destroy(gameObject);
  73.             }
  74.             //----------------------------------------------------------NORMALIZED PATTERN AND LOGIC
  75.             //Normalized Range Logic
  76.             if (MinimumRange < CurrentRange)
  77.             {
  78.  
  79.             }
  80.  
  81.             if (MinimumRange > CurrentRange)
  82.             {
  83.                 gameObject.transform.position = new Vector2(Mathf.MoveTowards(transform.position.x, Target.position.x, CurrentMoveSpeed), 0);
  84.             }
  85.  
  86.  
  87.             //-------------------------------------------------------------ATTACKING LOGIC
  88.             //Melee Range Check
  89.             if (CurrentRange < StrikeRange)
  90.             {
  91.                 CanAttack = true;
  92.             }
  93.             else
  94.             {
  95.                 CanAttack = false;
  96.             }
  97.             //Spell Range Check
  98.             if (CurrentRange < SpellRange)
  99.             {
  100.                 CanCast = true;
  101.             }
  102.             else
  103.             {
  104.                 CanCast = false;
  105.             }
  106.             //----------------------------------------------------------------- AI WINDOW HANDLING
  107.             //Offensive Window (0)
  108.             if (Offensive == true && DeliveryMethodArray[0] != null)
  109.             {
  110.                 DeliveryMethodArray[0].SendMessage("SendMessageTrue");
  111.             }
  112.             //Defensive Window (1)
  113.             if (Defensive == true && DeliveryMethodArray[1] != null)
  114.             {
  115.                 DeliveryMethodArray[1].SendMessage("SendMessageTrue");
  116.             }
  117.             //Misc Window      (2)
  118.             if (Misc == true && DeliveryMethodArray[2] != null)
  119.             {
  120.                 DeliveryMethodArray[2].SendMessage("SendMessageTrue");
  121.             }
  122.         }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.         //---------------------------------------------------------------------------METHOD HANDLING--------------------------------------------------------------------------------
  136.         public Transform[] PopulateSpellArray()
  137.         {
  138.             Transform[] ChildArray = new Transform[transform.childCount];
  139.             for (int i = 0; i < transform.childCount; i++)
  140.             {
  141.                 ChildArray[i] = transform.GetChild(i);
  142.             }
  143.             return ChildArray;
  144.         }
  145.         //This takes all of the delivery methods from the children of the gameObject and puts them into a transform array. Each Delivery method can then be called upon to spawn its
  146.         //Missile
  147.         public Transform[] DeliveryMethodHandling(Transform[] TA)
  148.         {
  149.             List<Transform> DeliveryList = new List<Transform>();
  150.             Transform[] DeliveryArray = new Transform[transform.childCount];
  151.             foreach (Transform T in TA)
  152.             {
  153.                 if (!DeliveryList.Contains(T))
  154.                 {
  155.                     DeliveryList.Add(T);
  156.                 }
  157.  
  158.             }
  159.             for (int i = 0; i < transform.childCount; i++)
  160.             {
  161.                 if (!DeliveryArray[i] == DeliveryList[i])
  162.                 {
  163.                     DeliveryArray[i] = DeliveryList[i];
  164.                 }
  165.             }
  166.             return DeliveryArray;
  167.         }
  168.  
  169.  
  170.  
  171.         class MessageHandling : AbstractDelivery
  172.         {
  173.             public void SendMessageTrue()
  174.             {
  175.                 MessageRecieved = true;
  176.             }
  177.         }
  178.        
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement