Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GameManager : MonoBehaviour
- {
- public GameObject ToSpawn;
- public int StartCount = 10;
- int IDset = 0;
- public float SpawnRadius = 50;
- public bool SpawnRepeated = false;
- public float RepeatedSpawnTime = 1;
- float RepeateTimer = 0;
- public Transform SeekTarget;
- public Transform FleeTarget;
- public List <FlockAgent> TheAgents;
- Vector3 TempV;
- Transform MyTransform;
- public bool ScreenWrap = true;
- Vector3 BottomLeft;
- Vector3 TopRight;
- Vector2 ScreenDist;
- float ExtraEdge = 5;
- int UpdateCounter = 0;
- // Use this for initialization
- void Start ()
- {
- MyTransform = transform;
- //TheAgents = new List<Agent>();
- for (int i = 0; i < StartCount; ++i)
- {
- SpawnUnit();
- }
- float Depth = (Camera.main.transform.position - MyTransform.position).magnitude;
- BottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0,0,Depth));
- TopRight = Camera.main.ViewportToWorldPoint(new Vector3(1,1,Depth));
- ScreenDist.x = (Mathf.Abs(BottomLeft.x)) + (Mathf.Abs( TopRight.x));
- ScreenDist.y = (Mathf.Abs(BottomLeft.z)) + (Mathf.Abs( TopRight.z));
- }
- // Update is called once per frame
- void Update ()
- {
- if (SpawnRepeated)
- {
- RepeateTimer += Time.deltaTime;
- if (RepeateTimer > RepeatedSpawnTime)
- {
- RepeateTimer = 0;
- SpawnUnit();
- StartCount++;
- }
- }
- for (int i = 0; i < StartCount; ++i)
- {
- if (ScreenWrap)
- {
- if (TheAgents[i].Is2D)
- {
- TempV = TheAgents[i].MyTransform.position;
- if (TempV.x + ExtraEdge < BottomLeft.x)
- {
- TheAgents[i].ResetGroup();
- TheAgents[i].MyTransform.position += new Vector3(ScreenDist.x+ExtraEdge, 0, 0);
- }
- if (TempV.x - ExtraEdge > TopRight.x)
- {
- TheAgents[i].ResetGroup();
- TheAgents[i].MyTransform.position -= new Vector3(ScreenDist.x+ExtraEdge, 0, 0);
- }
- if (TempV.z + ExtraEdge < BottomLeft.z)
- {
- TheAgents[i].ResetGroup();
- TheAgents[i].MyTransform.position += new Vector3(0, 0, ScreenDist.y+ExtraEdge);
- }
- if (TempV.z - ExtraEdge > TopRight.z)
- {
- TheAgents[i].ResetGroup();
- TheAgents[i].MyTransform.position -= new Vector3(0, 0, ScreenDist.y+ExtraEdge);
- }
- }
- else
- {
- if(CircleCollsison(MyTransform.position,1,TheAgents[i].MyTransform.position,150))
- {
- TempV = MyTransform.position - TheAgents[i].MyTransform.position;
- TheAgents[i].MyTransform.position += TempV*2;
- }
- }
- }
- }
- for (int C = 0; C < 5; C++)
- {
- TheAgents[UpdateCounter].ResetGroup();
- for (int ii = 0 + 1; ii < StartCount; ++ii)
- {
- if (CircleCollsison(TheAgents[UpdateCounter].MyTransform.position, TheAgents[UpdateCounter].GetMaxRange() + 5,
- TheAgents[ii].MyTransform.position, 0))
- {
- TheAgents[UpdateCounter].Neighbours.Add(TheAgents[ii]);
- TheAgents[ii].Neighbours.Add(TheAgents[UpdateCounter]);
- }
- }
- UpdateCounter++;
- if (UpdateCounter >= StartCount)
- {
- UpdateCounter = 0;
- }
- }
- }
- void SpawnUnit()
- {
- TempV = Random.insideUnitSphere * SpawnRadius;
- TempV.y = 0;
- TheAgents.Add((Instantiate(ToSpawn, MyTransform.position + TempV, Quaternion.identity)
- as GameObject).GetComponent<FlockAgent>());
- TheAgents[TheAgents.Count - 1].ID = IDset++;
- }
- bool CircleCollsison(Vector3 P1, float R1,Vector3 P2, float R2)
- {
- return (P1 - P2).sqrMagnitude < ((R1+R2)*(R1+R2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement