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.

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