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.

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