You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.InputSystem;
  6. public class HerdController : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private GameObject Prefab;
  10. [SerializeField]
  11. private int HerdCount = 50;
  12. [SerializeField]
  13. private Transform SpawnPoint;
  14. [SerializeField]
  15. private float WaitTime;
  16. private float lastTime;
  17. private List<PlayerController> Herd;
  18. private Vector2 recievedInput;
  19. void Start()
  20. {
  21. SpawnHerd();
  22. StartCoroutine(MoveRoutine());
  23. }
  24. //Recieved movement input from player
  25. private void OnMovement(InputValue value)
  26. {
  27. Vector2 input = value.Get<Vector2>();
  28. if (input.magnitude > 0)
  29. input = input.normalized;
  30. Herd.ForEach(p => p.SetMovement(input));
  31. recievedInput = input;
  32. }
  33. [ContextMenu("Spawn")]
  34. private void SpawnHerd()
  35. {
  36. float radius = 0;
  37. GameObject prefabExample = Instantiate(Prefab);
  38. Bounds bound = prefabExample.GetBounds();
  39. Debug.Log(bound.size);
  40. Destroy(prefabExample);
  41. if (Herd != null)
  42. Herd.ForEach(p => Destroy(p));
  43. Herd = new List<PlayerController>();
  44. for (int i = 0; i < HerdCount; i++)
  45. {
  46. int SpawnAttempt = 0;
  47. while (true)
  48. {
  49. Vector3 position = Vector3.ProjectOnPlane(Random.onUnitSphere, Vector3.up) * radius;
  50. Quaternion rotation = Quaternion.identity;
  51. if (SpawnPoint != null)
  52. position += SpawnPoint.position;
  53. if (SpawnPositionValid(position,rotation, bound))
  54. {
  55. GameObject newObject = Instantiate(Prefab, position, rotation, transform);
  56. Herd.Add(newObject.GetComponent<PlayerController>());
  57. break;
  58. }
  59. SpawnAttempt++;
  60. if (SpawnAttempt % 10 == 0)
  61. {
  62. radius += bound.size.magnitude;
  63. }
  64. if (SpawnAttempt == 100)
  65. break;
  66. }
  67. }
  68. Debug.Log("Total Spawned: " + Herd.Count);
  69. }
  70. public void FixedUpdate()
  71. {
  72. if (recievedInput.magnitude > 0 && lastTime + WaitTime < Time.time)
  73. {
  74. Herd.ForEach(p => p.MoveObject());
  75. Debug.Log("Time waited: " + (Time.time - lastTime));
  76. lastTime = Time.time;
  77. }
  78. }
  79. public IEnumerator MoveRoutine()
  80. {
  81. foreach (PlayerController pc in Herd)
  82. {
  83. pc.MoveObject(WaitTime);
  84. }
  85. yield return new WaitForSeconds(WaitTime);
  86. StartCoroutine(MoveRoutine());
  87. }
  88. private bool SpawnPositionValid(Vector3 position,Quaternion rotation ,Bounds bound)
  89. {
  90. Collider[] colliders = Physics.OverlapBox(position, bound.extents,rotation);
  91. Debug.DrawLine(position, position + Vector3.up);
  92. foreach(Collider col in colliders)
  93. {
  94. if (col.GetComponentInChildren<PlayerController>())
  95. return false;
  96. if (col.GetComponentInParent<PlayerController>())
  97. return false;
  98. }
  99. return true;
  100. }
  101. }