Advertisement
Wolvenspud

FlockAgent - GameManager

Nov 22nd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GameManager : MonoBehaviour
  6. {
  7.     public GameObject ToSpawn;
  8.     public int StartCount = 10;
  9.     int IDset = 0;
  10.  
  11.     public float SpawnRadius = 50;
  12.     public bool SpawnRepeated = false;
  13.     public float RepeatedSpawnTime = 1;
  14.     float RepeateTimer = 0;
  15.  
  16.     public Transform SeekTarget;
  17.     public Transform FleeTarget;
  18.  
  19.     public List <FlockAgent> TheAgents;
  20.  
  21.     Vector3 TempV;
  22.     Transform MyTransform;
  23.  
  24.     public bool ScreenWrap = true;
  25.     Vector3 BottomLeft;
  26.     Vector3 TopRight;
  27.     Vector2 ScreenDist;
  28.     float ExtraEdge = 5;
  29.     int UpdateCounter = 0;
  30.     // Use this for initialization
  31.     void Start ()
  32.     {
  33.         MyTransform = transform;
  34.         //TheAgents = new List<Agent>();
  35.         for (int i = 0; i < StartCount; ++i)
  36.         {
  37.             SpawnUnit();
  38.         }
  39.  
  40.         float Depth = (Camera.main.transform.position - MyTransform.position).magnitude;
  41.  
  42.         BottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0,0,Depth));
  43.        
  44.         TopRight = Camera.main.ViewportToWorldPoint(new Vector3(1,1,Depth));
  45.         ScreenDist.x = (Mathf.Abs(BottomLeft.x)) + (Mathf.Abs( TopRight.x));
  46.         ScreenDist.y = (Mathf.Abs(BottomLeft.z)) + (Mathf.Abs( TopRight.z));
  47.     }
  48.  
  49.     // Update is called once per frame
  50.     void Update ()
  51.     {
  52.         if (SpawnRepeated)
  53.         {
  54.             RepeateTimer += Time.deltaTime;
  55.             if (RepeateTimer > RepeatedSpawnTime)
  56.             {
  57.                 RepeateTimer = 0;
  58.                 SpawnUnit();
  59.                 StartCount++;
  60.             }
  61.         }
  62.  
  63.         for (int i = 0; i < StartCount; ++i)
  64.         {
  65.             if (ScreenWrap)
  66.             {
  67.                 if (TheAgents[i].Is2D)
  68.                 {
  69.                     TempV = TheAgents[i].MyTransform.position;
  70.                     if (TempV.x + ExtraEdge < BottomLeft.x)
  71.                     {
  72.                         TheAgents[i].ResetGroup();
  73.                         TheAgents[i].MyTransform.position += new Vector3(ScreenDist.x+ExtraEdge, 0, 0);
  74.                     }
  75.                     if (TempV.x - ExtraEdge > TopRight.x)
  76.                     {
  77.                         TheAgents[i].ResetGroup();
  78.                         TheAgents[i].MyTransform.position -= new Vector3(ScreenDist.x+ExtraEdge, 0, 0);
  79.                     }
  80.                     if (TempV.z + ExtraEdge < BottomLeft.z)
  81.                     {
  82.                         TheAgents[i].ResetGroup();
  83.                         TheAgents[i].MyTransform.position += new Vector3(0, 0, ScreenDist.y+ExtraEdge);
  84.                     }
  85.                     if (TempV.z - ExtraEdge > TopRight.z)
  86.                     {
  87.                         TheAgents[i].ResetGroup();
  88.                         TheAgents[i].MyTransform.position -= new Vector3(0, 0, ScreenDist.y+ExtraEdge);
  89.                     }
  90.                 }
  91.                 else
  92.                 {
  93.                     if(CircleCollsison(MyTransform.position,1,TheAgents[i].MyTransform.position,150))
  94.                     {
  95.                         TempV = MyTransform.position - TheAgents[i].MyTransform.position;
  96.                         TheAgents[i].MyTransform.position += TempV*2;
  97.  
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.      
  103.         for (int C = 0; C < 5; C++)
  104.         {
  105.             TheAgents[UpdateCounter].ResetGroup();
  106.             for (int ii = 0 + 1; ii < StartCount; ++ii)
  107.             {
  108.                 if (CircleCollsison(TheAgents[UpdateCounter].MyTransform.position, TheAgents[UpdateCounter].GetMaxRange() + 5,
  109.                         TheAgents[ii].MyTransform.position, 0))
  110.                 {
  111.                     TheAgents[UpdateCounter].Neighbours.Add(TheAgents[ii]);
  112.                     TheAgents[ii].Neighbours.Add(TheAgents[UpdateCounter]);
  113.                 }
  114.             }
  115.        
  116.             UpdateCounter++;
  117.             if (UpdateCounter >= StartCount)
  118.             {
  119.                 UpdateCounter = 0;
  120.             }
  121.         }
  122.     }
  123.  
  124.     void SpawnUnit()
  125.     {
  126.         TempV = Random.insideUnitSphere * SpawnRadius;
  127.         TempV.y = 0;
  128.         TheAgents.Add((Instantiate(ToSpawn, MyTransform.position + TempV, Quaternion.identity)
  129.             as GameObject).GetComponent<FlockAgent>());
  130.         TheAgents[TheAgents.Count - 1].ID = IDset++;
  131.        
  132.     }
  133.  
  134.     bool CircleCollsison(Vector3 P1, float R1,Vector3 P2, float R2)
  135.     {
  136.         return (P1 - P2).sqrMagnitude < ((R1+R2)*(R1+R2));
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement