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.

229 lines
5.4 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
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. public Transform Centre;
  19. private float lastTime;
  20. public List<PlayerController> Herd;
  21. private Vector2 recievedInput;
  22. public GameStateController GameState;
  23. public BallController Ball;
  24. void Start()
  25. {
  26. GameState = FindObjectOfType<GameStateController>();
  27. SpawnHerd();
  28. }
  29. //Recieved movement input from player
  30. private void OnMovement(InputValue value)
  31. {
  32. Vector2 input = value.Get<Vector2>();
  33. if (input.magnitude > 0)
  34. input = input.normalized;
  35. Herd.ForEach(p => p.SetMovement(input));
  36. recievedInput = input;
  37. Ball.GetInput(input);
  38. }
  39. private void OnReset(InputValue value)
  40. {
  41. UnityEngine.SceneManagement.SceneManager.LoadScene("CaseyTest");
  42. }
  43. private void OnSprint(InputValue value)
  44. {
  45. int input = (int)value.Get<float>();
  46. Debug.Log("Sprint: " + input);
  47. if (input == 1)
  48. {
  49. Ball.EatHorse = true;
  50. Ball.transform.position = Centre.position;
  51. }
  52. else
  53. {
  54. Ball.EatHorse = false;
  55. }
  56. }
  57. private Vector3 GetCentre()
  58. {
  59. Vector3 centreofMass = Vector3.zero;
  60. int CountedHorses = 0;
  61. foreach (PlayerController horse in Herd)
  62. {
  63. if (horse.isGrounded || Time.time < 5)
  64. {
  65. centreofMass += horse.transform.position;
  66. CountedHorses++;
  67. }
  68. }
  69. if (CountedHorses != 0)
  70. {
  71. return centreofMass / CountedHorses;
  72. }
  73. else
  74. {
  75. return Vector3.one;
  76. }
  77. }
  78. [ContextMenu("Spawn")]
  79. private void SpawnHerd()
  80. {
  81. float radius = 0;
  82. GameObject prefabExample = Instantiate(Prefab);
  83. Bounds bound = prefabExample.GetBounds();
  84. Debug.Log(bound.size);
  85. Destroy(prefabExample);
  86. if (Herd != null)
  87. Herd.ForEach(p => Destroy(p));
  88. Herd = new List<PlayerController>();
  89. for (int i = 0; i < HerdCount; i++)
  90. {
  91. int SpawnAttempt = 0;
  92. while (true)
  93. {
  94. Vector3 position = Vector3.ProjectOnPlane(Random.onUnitSphere, Vector3.up) * radius;
  95. Quaternion rotation = Quaternion.identity;
  96. if (SpawnPoint != null)
  97. position += SpawnPoint.position;
  98. if (SpawnPositionValid(position, rotation, bound))
  99. {
  100. GameObject newObject = Instantiate(Prefab, position, rotation, transform);
  101. foreach (MeshRenderer mr in newObject.GetComponentsInChildren<MeshRenderer>())
  102. {
  103. mr.material.color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1);
  104. }
  105. Herd.Add(newObject.GetComponent<PlayerController>());
  106. break;
  107. }
  108. SpawnAttempt++;
  109. if (SpawnAttempt % 10 == 0)
  110. {
  111. radius += bound.size.magnitude;
  112. }
  113. if (SpawnAttempt == 100)
  114. break;
  115. }
  116. }
  117. Debug.Log("Total Spawned: " + Herd.Count);
  118. }
  119. public void FixedUpdate()
  120. {
  121. if (recievedInput.magnitude > 0 && lastTime + WaitTime < Time.time)
  122. {
  123. Herd.ForEach(p => p.MoveObject(WaitTime));
  124. lastTime = Time.time;
  125. }
  126. if (Herd?.Count > 0)
  127. {
  128. Bounds bound = new Bounds(Herd[0].transform.position, Vector3.zero);
  129. Vector3 centreofMass = Vector3.zero;
  130. int CountedHorses = 0;
  131. foreach (PlayerController horse in Herd)
  132. {
  133. if (horse.isGrounded || Time.time < 5)
  134. {
  135. bound.Encapsulate(horse.transform.position);
  136. centreofMass += horse.transform.position;
  137. CountedHorses++;
  138. }
  139. }
  140. if (CountedHorses != 0)
  141. {
  142. Centre.position = centreofMass / CountedHorses;
  143. Centre.localScale = bound.size * 0.75f;
  144. }
  145. }
  146. else
  147. {
  148. GameState.LoseState();
  149. }
  150. }
  151. public void RemoveHorse(PlayerController horse)
  152. {
  153. Herd.Remove(horse);
  154. Destroy(horse.gameObject);
  155. }
  156. public int HerdAmount()
  157. {
  158. return Herd.Count();
  159. }
  160. private bool SpawnPositionValid(Vector3 position, Quaternion rotation, Bounds bound)
  161. {
  162. Collider[] colliders = Physics.OverlapBox(position, bound.extents, rotation);
  163. Debug.DrawLine(position, position + Vector3.up);
  164. foreach (Collider col in colliders)
  165. {
  166. if (col.GetComponentInChildren<PlayerController>())
  167. return false;
  168. if (col.GetComponentInParent<PlayerController>())
  169. return false;
  170. }
  171. return true;
  172. }
  173. }