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.

169 lines
4.0 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. Vector3 centreofMass = Vector3.zero;
  85. int CountedHorses = 0;
  86. foreach (PlayerController horse in Herd)
  87. {
  88. if (horse.isGrounded || Time.time < 5)
  89. {
  90. bound.Encapsulate(horse.transform.position);
  91. centreofMass += horse.transform.position;
  92. CountedHorses++;
  93. }
  94. }
  95. Centre.position = centreofMass/CountedHorses;
  96. Centre.localScale = bound.size * 0.9f;
  97. }
  98. else
  99. {
  100. GameState.LoseState();
  101. }
  102. }
  103. public void RemoveHorse(PlayerController horse)
  104. {
  105. Herd.Remove(horse);
  106. Destroy(horse.gameObject);
  107. }
  108. private bool SpawnPositionValid(Vector3 position, Quaternion rotation, Bounds bound)
  109. {
  110. Collider[] colliders = Physics.OverlapBox(position, bound.extents, rotation);
  111. Debug.DrawLine(position, position + Vector3.up);
  112. foreach (Collider col in colliders)
  113. {
  114. if (col.GetComponentInChildren<PlayerController>())
  115. return false;
  116. if (col.GetComponentInParent<PlayerController>())
  117. return false;
  118. }
  119. return true;
  120. }
  121. }