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.

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