|
|
@ -19,6 +19,8 @@ public class HerdController : MonoBehaviour |
|
|
|
[SerializeField] |
|
|
|
private float WaitTime; |
|
|
|
|
|
|
|
[SerializeField] |
|
|
|
private Transform Centre; |
|
|
|
|
|
|
|
private float lastTime; |
|
|
|
private List<PlayerController> Herd; |
|
|
@ -27,7 +29,6 @@ public class HerdController : MonoBehaviour |
|
|
|
void Start() |
|
|
|
{ |
|
|
|
SpawnHerd(); |
|
|
|
StartCoroutine(MoveRoutine()); |
|
|
|
} |
|
|
|
|
|
|
|
//Recieved movement input from player
|
|
|
@ -37,7 +38,7 @@ public class HerdController : MonoBehaviour |
|
|
|
if (input.magnitude > 0) |
|
|
|
input = input.normalized; |
|
|
|
|
|
|
|
Herd.ForEach(p => p.SetMovement(input)); |
|
|
|
Herd.ForEach(p => p.SetMovement(input)); |
|
|
|
recievedInput = input; |
|
|
|
} |
|
|
|
|
|
|
@ -57,7 +58,7 @@ public class HerdController : MonoBehaviour |
|
|
|
|
|
|
|
for (int i = 0; i < HerdCount; i++) |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int SpawnAttempt = 0; |
|
|
@ -69,16 +70,16 @@ public class HerdController : MonoBehaviour |
|
|
|
if (SpawnPoint != null) |
|
|
|
position += SpawnPoint.position; |
|
|
|
|
|
|
|
if (SpawnPositionValid(position,rotation, bound)) |
|
|
|
if (SpawnPositionValid(position, rotation, bound)) |
|
|
|
{ |
|
|
|
GameObject newObject = Instantiate(Prefab, position, rotation, transform); |
|
|
|
|
|
|
|
|
|
|
|
Herd.Add(newObject.GetComponent<PlayerController>()); |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SpawnAttempt++; |
|
|
|
if (SpawnAttempt % 10 == 0) |
|
|
@ -91,7 +92,7 @@ public class HerdController : MonoBehaviour |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
Debug.Log("Total Spawned: " + Herd.Count); |
|
|
|
} |
|
|
@ -100,33 +101,23 @@ public class HerdController : MonoBehaviour |
|
|
|
{ |
|
|
|
if (recievedInput.magnitude > 0 && lastTime + WaitTime < Time.time) |
|
|
|
{ |
|
|
|
Herd.ForEach(p => p.MoveObject()); |
|
|
|
Debug.Log("Time waited: " + (Time.time - lastTime)); |
|
|
|
Herd.ForEach(p => p.MoveObject(WaitTime)); |
|
|
|
lastTime = Time.time; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public IEnumerator MoveRoutine() |
|
|
|
{ |
|
|
|
foreach (PlayerController pc in Herd) |
|
|
|
{ |
|
|
|
pc.MoveObject(WaitTime); |
|
|
|
} |
|
|
|
yield return new WaitForSeconds(WaitTime); |
|
|
|
|
|
|
|
StartCoroutine(MoveRoutine()); |
|
|
|
if (Centre != null) |
|
|
|
Centre.position = Herd.Aggregate(new Vector3(0, 0, 0), (s, v) => s + v.transform.position) / (float)Herd.Count; |
|
|
|
} |
|
|
|
|
|
|
|
private bool SpawnPositionValid(Vector3 position,Quaternion rotation ,Bounds bound) |
|
|
|
|
|
|
|
private bool SpawnPositionValid(Vector3 position, Quaternion rotation, Bounds bound) |
|
|
|
{ |
|
|
|
Collider[] colliders = Physics.OverlapBox(position, bound.extents,rotation); |
|
|
|
Collider[] colliders = Physics.OverlapBox(position, bound.extents, rotation); |
|
|
|
|
|
|
|
Debug.DrawLine(position, position + Vector3.up); |
|
|
|
|
|
|
|
foreach(Collider col in colliders) |
|
|
|
foreach (Collider col in colliders) |
|
|
|
{ |
|
|
|
if (col.GetComponentInChildren<PlayerController>()) |
|
|
|
return false; |
|
|
|