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.

180 lines
4.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
  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. foreach (MeshRenderer mr in newObject.GetComponentsInChildren<MeshRenderer>())
  61. {
  62. mr.material.color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1);
  63. }
  64. Herd.Add(newObject.GetComponent<PlayerController>());
  65. break;
  66. }
  67. SpawnAttempt++;
  68. if (SpawnAttempt % 10 == 0)
  69. {
  70. radius += bound.size.magnitude;
  71. }
  72. if (SpawnAttempt == 100)
  73. break;
  74. }
  75. }
  76. Debug.Log("Total Spawned: " + Herd.Count);
  77. }
  78. public void FixedUpdate()
  79. {
  80. if (recievedInput.magnitude > 0 && lastTime + WaitTime < Time.time)
  81. {
  82. Herd.ForEach(p => p.MoveObject(WaitTime));
  83. lastTime = Time.time;
  84. }
  85. if (Herd?.Count > 0)
  86. {
  87. Bounds bound = new Bounds(Herd[0].transform.position, Vector3.zero);
  88. Vector3 centreofMass = Vector3.zero;
  89. int CountedHorses = 0;
  90. foreach (PlayerController horse in Herd)
  91. {
  92. if (horse.isGrounded || Time.time < 5)
  93. {
  94. bound.Encapsulate(horse.transform.position);
  95. centreofMass += horse.transform.position;
  96. CountedHorses++;
  97. }
  98. }
  99. if (CountedHorses != 0)
  100. {
  101. Centre.position = centreofMass / CountedHorses;
  102. Centre.localScale = bound.size * 0.75f;
  103. }
  104. }
  105. else
  106. {
  107. GameState.LoseState();
  108. }
  109. }
  110. public void RemoveHorse(PlayerController horse)
  111. {
  112. Herd.Remove(horse);
  113. Destroy(horse.gameObject);
  114. }
  115. public int HerdAmount()
  116. {
  117. return Herd.Count();
  118. }
  119. private bool SpawnPositionValid(Vector3 position, Quaternion rotation, Bounds bound)
  120. {
  121. Collider[] colliders = Physics.OverlapBox(position, bound.extents, rotation);
  122. Debug.DrawLine(position, position + Vector3.up);
  123. foreach (Collider col in colliders)
  124. {
  125. if (col.GetComponentInChildren<PlayerController>())
  126. return false;
  127. if (col.GetComponentInParent<PlayerController>())
  128. return false;
  129. }
  130. return true;
  131. }
  132. }