StandingPeopleConcert.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class StandingPeopleConcert : MonoBehaviour {
  5. [HideInInspector] public GameObject planePrefab;
  6. [HideInInspector] public GameObject circlePrefab;
  7. [HideInInspector] public GameObject surface;
  8. [HideInInspector] public Vector2 planeSize = new Vector2(1, 1);
  9. [Tooltip("People prefabs / Префабы людей")] public GameObject[] peoplePrefabs = new GameObject[0];
  10. [HideInInspector]
  11. List<Vector3> spawnPoints = new List<Vector3>();
  12. [HideInInspector] public GameObject target;
  13. [HideInInspector] public int peopleCount;
  14. [HideInInspector] public bool isCircle;
  15. [HideInInspector] public float circleDiametr = 1;
  16. [HideInInspector] public bool showSurface = true;
  17. public enum TestEnum{Rectangle, Circle};
  18. [Tooltip("Type of surface / Тип поверхности")] public TestEnum SurfaceType;
  19. [HideInInspector]
  20. public GameObject par;
  21. [HideInInspector] public bool looking;
  22. [HideInInspector] public float damping = 5;
  23. [HideInInspector] public float highToSpawn;
  24. public void OnDrawGizmos()
  25. {
  26. if (!isCircle)
  27. surface.transform.localScale = new Vector3(planeSize.x, 1, planeSize.y);
  28. else
  29. surface.transform.localScale = new Vector3(circleDiametr, 1, circleDiametr);
  30. }
  31. public void SpawnRectangleSurface()
  32. {
  33. if (surface != null) DestroyImmediate(surface);
  34. var plane = Instantiate(planePrefab, transform.position, Quaternion.identity) as GameObject;
  35. surface = plane;
  36. isCircle = false;
  37. plane.transform.eulerAngles = new Vector3(
  38. plane.transform.eulerAngles.x,
  39. plane.transform.eulerAngles.y,
  40. plane.transform.eulerAngles.z
  41. );
  42. plane.transform.position += new Vector3(0, 0.01f, 0);
  43. plane.transform.parent = transform;
  44. plane.name = "surface";
  45. }
  46. public void SpawnCircleSurface()
  47. {
  48. if(surface != null) DestroyImmediate(surface);
  49. var circle = Instantiate(circlePrefab, transform.position, Quaternion.identity) as GameObject;
  50. isCircle = true;
  51. circle.transform.eulerAngles = new Vector3(
  52. circle.transform.eulerAngles.x,
  53. circle.transform.eulerAngles.y,
  54. circle.transform.eulerAngles.z
  55. );
  56. circle.transform.position += new Vector3(0, 0.01f, 0);
  57. circle.transform.parent = transform;
  58. circle.name = "surface";
  59. surface = circle;
  60. }
  61. public void RemoveButton()
  62. {
  63. if(par != null)
  64. DestroyImmediate(par);
  65. }
  66. public void PopulateButton()
  67. {
  68. RemoveButton();
  69. GameObject parGo = new GameObject();
  70. par = parGo;
  71. parGo.transform.parent = gameObject.transform;
  72. parGo.name = "people";
  73. spawnPoints.Clear();
  74. SpawnPeople(peopleCount);
  75. }
  76. void SpawnPeople (int _peopleCount)
  77. {
  78. int[] peoplePrefabIndexes = CommonUtils.GetRandomPrefabIndexes(_peopleCount, ref peoplePrefabs);
  79. for (int i = 0; i < _peopleCount; i++)
  80. {
  81. Vector3 randomPosition;
  82. if (!isCircle)
  83. randomPosition = RandomRectanglePosition();
  84. else
  85. randomPosition = RandomCirclePosition();
  86. if (randomPosition != Vector3.zero)
  87. {
  88. GameObject people = peoplePrefabs[peoplePrefabIndexes[i]];
  89. RaycastHit hit;
  90. GameObject obj = null;
  91. if (Physics.Raycast(randomPosition + Vector3.up * highToSpawn, Vector3.down, out hit, Mathf.Infinity))
  92. {
  93. obj = Instantiate(people, new Vector3(randomPosition.x, hit.point.y, randomPosition.z), Quaternion.Euler(people.transform.rotation.x, transform.eulerAngles.y, people.transform.rotation.z)) as GameObject;
  94. }
  95. else
  96. {
  97. continue;
  98. }
  99. PeopleController pc = obj.AddComponent<PeopleController>();
  100. spawnPoints.Add(obj.transform.position);
  101. if (target != null)
  102. {
  103. pc.SetTarget(target.transform.position);
  104. if (looking)
  105. {
  106. pc.target = target.transform;
  107. pc.damping = damping;
  108. }
  109. }
  110. pc.animNames = new string[4] { "idle1", "idle2", "cheer", "claphands" };
  111. obj.transform.parent = par.transform;
  112. }
  113. }
  114. }
  115. Vector3 RandomRectanglePosition ()
  116. {
  117. Vector3 randomPosition = new Vector3(0, 0, 0);
  118. for(int i = 0; i < 10; i++)
  119. {
  120. randomPosition.x = surface.transform.position.x - GetRealPlaneSize().x / 2 + Random.Range(0.0f, GetRealPlaneSize().x - 0.6f);
  121. randomPosition.z = surface.transform.position.z - GetRealPlaneSize().y / 2 + Random.Range(0.0f, GetRealPlaneSize().y - 0.6f);
  122. randomPosition.y = surface.transform.position.y;
  123. if(IsRandomPositionFree(randomPosition))
  124. return randomPosition;
  125. }
  126. return Vector3.zero;
  127. }
  128. Vector3 RandomCirclePosition ()
  129. {
  130. Vector3 center = surface.transform.position;
  131. float radius = GetRealPlaneSize().x / 2;
  132. for(int i = 0; i < 10; i++)
  133. {
  134. float randomRadius = Random.value * radius;
  135. float ang = Random.value * 360;
  136. Vector3 pos;
  137. pos.x = center.x + randomRadius * Mathf.Sin(ang * Mathf.Deg2Rad);
  138. pos.y = center.y;
  139. pos.z = center.z + randomRadius * Mathf.Cos(ang * Mathf.Deg2Rad);
  140. if (IsRandomPositionFree(pos))
  141. return pos;
  142. }
  143. return Vector3.zero;
  144. }
  145. bool IsRandomPositionFree (Vector3 pos)
  146. {
  147. for(int i = 0; i < spawnPoints.Count; i++)
  148. {
  149. if(spawnPoints[i].x - 0.6f < pos.x && spawnPoints[i].x + 1 > pos.x && spawnPoints[i].z - 0.5f < pos.z && spawnPoints[i].z + 0.6f > pos.z)
  150. return false;
  151. }
  152. return true;
  153. }
  154. Vector2 GetRealPlaneSize()
  155. {
  156. Vector3 meshSize = surface.GetComponent<MeshRenderer>().bounds.size;
  157. return new Vector2(meshSize.x, meshSize.z);
  158. }
  159. Vector2 GetRealPeopleModelSize()
  160. {
  161. Vector3 meshSize = peoplePrefabs[1].GetComponent<MeshRenderer>().bounds.size;
  162. return new Vector2(meshSize.x, meshSize.z);
  163. }
  164. }