|
@ -19,19 +19,27 @@ public class HerdController : MonoBehaviour |
|
|
[SerializeField] |
|
|
[SerializeField] |
|
|
private float WaitTime; |
|
|
private float WaitTime; |
|
|
|
|
|
|
|
|
|
|
|
[SerializeField] |
|
|
|
|
|
private Transform Centre; |
|
|
|
|
|
|
|
|
|
|
|
private float lastTime; |
|
|
private List<PlayerController> Herd; |
|
|
private List<PlayerController> Herd; |
|
|
|
|
|
private Vector2 recievedInput; |
|
|
|
|
|
|
|
|
void Start() |
|
|
void Start() |
|
|
{ |
|
|
{ |
|
|
SpawnHerd(); |
|
|
SpawnHerd(); |
|
|
StartCoroutine(MoveRoutine()); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Recieved movement input from player
|
|
|
//Recieved movement input from player
|
|
|
private void OnMovement(InputValue value) |
|
|
private void OnMovement(InputValue value) |
|
|
{ |
|
|
{ |
|
|
Vector2 input = value.Get<Vector2>(); |
|
|
Vector2 input = value.Get<Vector2>(); |
|
|
|
|
|
if (input.magnitude > 0) |
|
|
|
|
|
input = input.normalized; |
|
|
|
|
|
|
|
|
Herd.ForEach(p => p.SetMovement(input)); |
|
|
Herd.ForEach(p => p.SetMovement(input)); |
|
|
|
|
|
recievedInput = input; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -50,7 +58,7 @@ public class HerdController : MonoBehaviour |
|
|
|
|
|
|
|
|
for (int i = 0; i < HerdCount; i++) |
|
|
for (int i = 0; i < HerdCount; i++) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int SpawnAttempt = 0; |
|
|
int SpawnAttempt = 0; |
|
@ -62,16 +70,16 @@ public class HerdController : MonoBehaviour |
|
|
if (SpawnPoint != null) |
|
|
if (SpawnPoint != null) |
|
|
position += SpawnPoint.position; |
|
|
position += SpawnPoint.position; |
|
|
|
|
|
|
|
|
if (SpawnPositionValid(position,rotation, bound)) |
|
|
|
|
|
|
|
|
if (SpawnPositionValid(position, rotation, bound)) |
|
|
{ |
|
|
{ |
|
|
GameObject newObject = Instantiate(Prefab, position, rotation, transform); |
|
|
GameObject newObject = Instantiate(Prefab, position, rotation, transform); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Herd.Add(newObject.GetComponent<PlayerController>()); |
|
|
Herd.Add(newObject.GetComponent<PlayerController>()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SpawnAttempt++; |
|
|
SpawnAttempt++; |
|
|
if (SpawnAttempt % 10 == 0) |
|
|
if (SpawnAttempt % 10 == 0) |
|
@ -84,28 +92,32 @@ public class HerdController : MonoBehaviour |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log("Total Spawned: " + Herd.Count); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
Debug.Log("Total Spawned: " + Herd.Count); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public IEnumerator MoveRoutine() |
|
|
|
|
|
|
|
|
public void FixedUpdate() |
|
|
{ |
|
|
{ |
|
|
foreach (PlayerController pc in Herd) |
|
|
|
|
|
|
|
|
if (recievedInput.magnitude > 0 && lastTime + WaitTime < Time.time) |
|
|
{ |
|
|
{ |
|
|
pc.MoveObject(WaitTime); |
|
|
|
|
|
|
|
|
Herd.ForEach(p => p.MoveObject(WaitTime)); |
|
|
|
|
|
lastTime = Time.time; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
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); |
|
|
Debug.DrawLine(position, position + Vector3.up); |
|
|
|
|
|
|
|
|
foreach(Collider col in colliders) |
|
|
|
|
|
|
|
|
foreach (Collider col in colliders) |
|
|
{ |
|
|
{ |
|
|
if (col.GetComponentInChildren<PlayerController>()) |
|
|
if (col.GetComponentInChildren<PlayerController>()) |
|
|
return false; |
|
|
return false; |
|
|