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.

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