StandingPeopleStreet.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class StandingPeopleStreet : 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. public List<Vector3> spawnPoints = new List<Vector3>();
  12. [HideInInspector] public int peopleCount;
  13. [HideInInspector] public bool isCircle;
  14. [HideInInspector] public float circleDiametr = 1;
  15. [HideInInspector] public bool showSurface = true;
  16. public enum TestEnum{Rectangle, Circle};
  17. [Tooltip("Type of surface / Тип поверхности")] public TestEnum SurfaceType;
  18. [HideInInspector]
  19. public GameObject par;
  20. [HideInInspector] public float highToSpawn;
  21. public void OnDrawGizmos()
  22. {
  23. if (!isCircle)
  24. surface.transform.localScale = new Vector3(planeSize.x, 1, planeSize.y);
  25. else
  26. surface.transform.localScale = new Vector3(circleDiametr, 1, circleDiametr);
  27. }
  28. public void SpawnRectangleSurface()
  29. {
  30. if(surface != null) DestroyImmediate(surface);
  31. var plane = Instantiate(planePrefab, transform.position, Quaternion.identity) as GameObject;
  32. surface = plane;
  33. isCircle = false;
  34. plane.transform.eulerAngles = new Vector3(
  35. plane.transform.eulerAngles.x,
  36. plane.transform.eulerAngles.y,
  37. plane.transform.eulerAngles.z
  38. );
  39. plane.transform.position += new Vector3(0, 0.01f, 0);
  40. plane.transform.parent = transform;
  41. plane.name = "surface";
  42. }
  43. public void SpawnCircleSurface()
  44. {
  45. if(surface != null) DestroyImmediate(surface);
  46. var circle = Instantiate(circlePrefab, transform.position, Quaternion.identity) as GameObject;
  47. isCircle = true;
  48. circle.transform.eulerAngles = new Vector3(
  49. circle.transform.eulerAngles.x,
  50. circle.transform.eulerAngles.y,
  51. circle.transform.eulerAngles.z
  52. );
  53. circle.transform.position += new Vector3(0, 0.01f, 0);
  54. circle.transform.parent = transform;
  55. circle.name = "surface";
  56. surface = circle;
  57. }
  58. public void RemoveButton()
  59. {
  60. if(par != null)
  61. DestroyImmediate(par);
  62. par = null;
  63. }
  64. public void PopulateButton()
  65. {
  66. RemoveButton();
  67. GameObject parGo = new GameObject();
  68. par = parGo;
  69. parGo.transform.parent = gameObject.transform;
  70. parGo.name = "people";
  71. spawnPoints.Clear();
  72. SpawnPeople(peopleCount);
  73. }
  74. void SpawnPeople (int _peopleCount)
  75. {
  76. int three = Random.Range(0, (int) (_peopleCount) / 3) * 3;
  77. int two = Random.Range(0, (int) (_peopleCount - three) / 2) * 2;
  78. int one = _peopleCount - three - two;
  79. int[] peoplePrefabIndexes = CommonUtils.GetRandomPrefabIndexes(peopleCount, ref peoplePrefabs);
  80. int peoplePrefabCount = 0;
  81. for(int i = 0; i < one; i++)
  82. {
  83. Vector3 randomPosition;
  84. if(!isCircle)
  85. randomPosition = RandomRectanglePosition();
  86. else
  87. randomPosition = RandomCirclePosition();
  88. if (randomPosition != Vector3.zero)
  89. {
  90. RaycastHit hit;
  91. GameObject obj = null;
  92. if (Physics.Raycast(randomPosition + Vector3.up * highToSpawn, Vector3.down, out hit, Mathf.Infinity))
  93. {
  94. obj = Instantiate(peoplePrefabs[peoplePrefabIndexes[peoplePrefabCount]], new Vector3(randomPosition.x, hit.point.y, randomPosition.z), Quaternion.identity) as GameObject;
  95. peoplePrefabCount++;
  96. }
  97. else
  98. {
  99. continue;
  100. }
  101. obj.AddComponent<PeopleController>();
  102. spawnPoints.Add(obj.transform.position);
  103. obj.transform.localEulerAngles = new Vector3(obj.transform.rotation.x, Random.Range(1, 359), obj.transform.rotation.z);
  104. obj.GetComponent<PeopleController>().animNames = new string[2]{"idle1", "idle2"};
  105. obj.transform.parent = par.transform;
  106. }
  107. }
  108. for(int i = 0; i < two / 2; i++)
  109. {
  110. Vector3 randomPosition;
  111. if(!isCircle)
  112. randomPosition = RandomRectanglePosition();
  113. else
  114. randomPosition = RandomCirclePosition();
  115. if(randomPosition != Vector3.zero)
  116. {
  117. Vector3 randomPos1 = Vector3.zero;
  118. Vector3 randomPos2 = Vector3.zero;
  119. for(int f = 0; f < 100; f++)
  120. {
  121. for(int z = 0; z < 10; z++)
  122. {
  123. randomPos1 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
  124. if(IsRandomPositionFree(randomPos1, Vector3.zero, Vector3.zero))
  125. break;
  126. else randomPos1 = Vector3.zero;
  127. }
  128. for(int x = 0; x < 10; x++)
  129. {
  130. randomPos2 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
  131. if(IsRandomPositionFree(randomPos2, randomPos1, Vector3.zero))
  132. break;
  133. else randomPos2 = Vector3.zero;
  134. }
  135. if(randomPos1 != Vector3.zero && randomPos2 != Vector3.zero)
  136. {
  137. spawnPoints.Add(randomPos1);
  138. spawnPoints.Add(randomPos2);
  139. break;
  140. }
  141. else
  142. {
  143. randomPos1 = Vector3.zero;
  144. randomPos2 = Vector3.zero;
  145. }
  146. }
  147. if(randomPos1 != Vector3.zero && randomPos2 != Vector3.zero)
  148. {
  149. int prefabNum = Random.Range(0, peoplePrefabs.Length);
  150. RaycastHit hit;
  151. GameObject obj = null;
  152. if (Physics.Raycast(randomPos1 + Vector3.up * highToSpawn, Vector3.down, out hit, Mathf.Infinity))
  153. {
  154. obj = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos1.x, hit.point.y, randomPos1.z), Quaternion.identity) as GameObject;
  155. peoplePrefabCount++;
  156. }
  157. else
  158. {
  159. continue;
  160. }
  161. obj.AddComponent<PeopleController>();
  162. obj.GetComponent<PeopleController>().animNames = new string[3]{"talk1", "talk2", "listen"};
  163. obj.transform.parent = par.transform;
  164. prefabNum = Random.Range(0, peoplePrefabs.Length);
  165. RaycastHit hit1;
  166. GameObject obs = null;
  167. if (Physics.Raycast(randomPos2 + Vector3.up * highToSpawn, Vector3.down, out hit1, Mathf.Infinity))
  168. {
  169. obs = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos2.x, hit1.point.y, randomPos2.z), Quaternion.identity) as GameObject;
  170. }
  171. else
  172. {
  173. continue;
  174. }
  175. obs.AddComponent<PeopleController>();
  176. obs.GetComponent<PeopleController>().animNames = new string[3]{"talk1", "talk2", "listen"};
  177. obs.transform.parent = par.transform;
  178. obs.GetComponent<PeopleController>().SetTarget(obj.transform.position);
  179. obj.GetComponent<PeopleController>().SetTarget(obs.transform.position);
  180. }
  181. }
  182. }
  183. for(int i = 0; i < three / 3; i++)
  184. {
  185. Vector3 randomPosition;
  186. if(!isCircle)
  187. randomPosition = RandomRectanglePosition();
  188. else
  189. randomPosition = RandomCirclePosition();
  190. if(randomPosition != Vector3.zero)
  191. {
  192. int prefabNum = Random.Range(0, peoplePrefabs.Length);
  193. Vector3 randomPos0 = Vector3.zero;
  194. Vector3 randomPos1 = Vector3.zero;
  195. Vector3 randomPos2 = Vector3.zero;
  196. for(int f = 0; f < 100; f++)
  197. {
  198. int z;
  199. int x;
  200. int c;
  201. for(z = 0; z < 10; z++)
  202. {
  203. randomPos0 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
  204. if(IsRandomPositionFree(randomPos0, Vector3.zero, Vector3.zero))
  205. {
  206. break;
  207. }
  208. else randomPos0 = Vector3.zero;
  209. }
  210. for(x = 0; x < 10; x++)
  211. {
  212. if(randomPos0 != Vector3.zero)
  213. {
  214. randomPos1 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
  215. if(IsRandomPositionFree(randomPos1, randomPos0, Vector3.zero))
  216. {
  217. break;
  218. }
  219. else
  220. {
  221. randomPos1 = Vector3.zero;
  222. }
  223. }
  224. else randomPos1 = Vector3.zero;
  225. }
  226. for(c = 0; c < 10; c++)
  227. {
  228. if(randomPos1 != Vector3.zero && randomPos0 != Vector3.zero)
  229. {
  230. randomPos2 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
  231. if(IsRandomPositionFree(randomPos2, randomPos0, randomPos1))
  232. {
  233. break;
  234. }
  235. else
  236. {
  237. randomPos2 = Vector3.zero;
  238. }
  239. }
  240. else randomPos2 = Vector3.zero;
  241. }
  242. if(randomPos0 != Vector3.zero && randomPos1 != Vector3.zero && randomPos2 != Vector3.zero)
  243. {
  244. spawnPoints.Add(randomPos0);
  245. spawnPoints.Add(randomPos1);
  246. spawnPoints.Add(randomPos2);
  247. break;
  248. }
  249. else
  250. {
  251. randomPos0 = Vector3.zero;
  252. randomPos1 = Vector3.zero;
  253. randomPos2 = Vector3.zero;
  254. }
  255. }
  256. if(randomPos0 != Vector3.zero)
  257. {
  258. if (randomPos0 != Vector3.zero)
  259. {
  260. RaycastHit hit;
  261. GameObject obj = null;
  262. if (Physics.Raycast(randomPos0 + Vector3.up * highToSpawn, Vector3.down, out hit, Mathf.Infinity))
  263. {
  264. obj = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos0.x, hit.point.y, randomPos0.z), Quaternion.identity) as GameObject;
  265. peoplePrefabCount++;
  266. }
  267. else
  268. {
  269. continue;
  270. }
  271. obj.AddComponent<PeopleController>();
  272. obj.GetComponent<PeopleController>().SetTarget(randomPosition);
  273. obj.GetComponent<PeopleController>().animNames = new string[3] { "talk1", "talk2", "listen" };
  274. obj.transform.parent = par.transform;
  275. }
  276. prefabNum = Random.Range(0, peoplePrefabs.Length);
  277. if (randomPos0 != Vector3.zero)
  278. {
  279. RaycastHit hit1;
  280. GameObject obs = null;
  281. if (Physics.Raycast(randomPos1 + Vector3.up * highToSpawn, Vector3.down, out hit1, Mathf.Infinity))
  282. {
  283. obs = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos1.x, hit1.point.y, randomPos1.z), Quaternion.identity) as GameObject;
  284. peoplePrefabCount++;
  285. }
  286. else
  287. {
  288. continue;
  289. }
  290. obs.AddComponent<PeopleController>();
  291. obs.GetComponent<PeopleController>().SetTarget(randomPosition);
  292. obs.GetComponent<PeopleController>().animNames = new string[3] { "talk1", "talk2", "listen" };
  293. obs.transform.parent = par.transform;
  294. }
  295. prefabNum = Random.Range(0, peoplePrefabs.Length);
  296. if (randomPos0 != Vector3.zero)
  297. {
  298. RaycastHit hit2;
  299. GameObject obl = null;
  300. if (Physics.Raycast(randomPos2 + Vector3.up * highToSpawn, Vector3.down, out hit2, Mathf.Infinity))
  301. {
  302. obl = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos2.x, hit2.point.y, randomPos2.z), Quaternion.identity) as GameObject;
  303. peoplePrefabCount++;
  304. }
  305. else
  306. {
  307. continue;
  308. }
  309. obl.AddComponent<PeopleController>();
  310. obl.GetComponent<PeopleController>().SetTarget(randomPosition);
  311. obl.GetComponent<PeopleController>().animNames = new string[3] { "talk1", "talk2", "listen" };
  312. obl.transform.parent = par.transform;
  313. }
  314. }
  315. }
  316. }
  317. }
  318. Vector3 RandomRectanglePosition ()
  319. {
  320. Vector3 randomPosition = new Vector3(0, 0, 0);
  321. for(int i = 0; i < 10; i++)
  322. {
  323. randomPosition.x = surface.transform.position.x - GetRealPlaneSize().x / 2 + 0.3f + Random.Range(0.0f, GetRealPlaneSize().x - 0.6f);
  324. randomPosition.z = surface.transform.position.z - GetRealPlaneSize().y / 2 + 0.3f + Random.Range(0.0f, GetRealPlaneSize().y - 0.6f);
  325. randomPosition.y = surface.transform.position.y;
  326. if(IsRandomPositionFree(randomPosition, Vector3.zero, Vector3.zero))
  327. return randomPosition;
  328. }
  329. return Vector3.zero;
  330. }
  331. Vector3 RandomCirclePosition ()
  332. {
  333. Vector3 center = surface.transform.position;
  334. float radius = GetRealPlaneSize().x / 2;
  335. for(int i = 0; i < 10; i++)
  336. {
  337. float randomRadius = Random.value * radius;
  338. float ang = Random.value * 360;
  339. Vector3 pos;
  340. pos.x = center.x + randomRadius * Mathf.Sin(ang * Mathf.Deg2Rad);
  341. pos.y = center.y;
  342. pos.z = center.z + randomRadius * Mathf.Cos(ang * Mathf.Deg2Rad);
  343. if(Vector3.Distance(pos, center) < GetRealPlaneSize().x / 2 - 0.3f && IsRandomPositionFree(pos, Vector3.zero, Vector3.zero))
  344. return pos;
  345. }
  346. return Vector3.zero;
  347. }
  348. bool IsRandomPositionFree (Vector3 pos, Vector3 helpPoint1, Vector3 helpPoint2)
  349. {
  350. for(int i = 0; i < spawnPoints.Count; i++)
  351. {
  352. if(spawnPoints[i].x - 0.5f < pos.x && spawnPoints[i].x + 0.5f > pos.x && spawnPoints[i].z - 0.5f < pos.z && spawnPoints[i].z + 0.5f > pos.z)
  353. return false;
  354. }
  355. if(helpPoint1 != Vector3.zero)
  356. {
  357. if(helpPoint1.x - 0.6f < pos.x && helpPoint1.x + 0.6f > pos.x && helpPoint1.z - 0.6f < pos.z && helpPoint1.z + 0.6f > pos.z)
  358. {
  359. return false;
  360. }
  361. if(!isCircle)
  362. {
  363. if(
  364. !(helpPoint1.x + 0.3f > surface.transform.position.x - GetRealPlaneSize().x / 2) &&
  365. !(helpPoint1.x - 0.3f < surface.transform.position.x + GetRealPlaneSize().x / 2) &&
  366. !(helpPoint1.z + 0.3f > surface.transform.position.z - GetRealPlaneSize().y / 2) &&
  367. !(helpPoint1.z - 0.3f < surface.transform.position.z + GetRealPlaneSize().y / 2)
  368. )
  369. return false;
  370. }
  371. else
  372. {
  373. if(Vector3.Distance(helpPoint1, surface.transform.position) >= GetRealPlaneSize().x / 2 - 0.3f)
  374. return false;
  375. }
  376. }
  377. if(helpPoint2 != Vector3.zero)
  378. {
  379. if(helpPoint2.x - 0.6f < pos.x && helpPoint2.x + 0.6f > pos.x && helpPoint2.z - 0.6f < pos.z && helpPoint2.z + 0.6f > pos.z)
  380. {
  381. return false;
  382. }
  383. if(!isCircle)
  384. {
  385. if(
  386. !(helpPoint2.x + 0.3f > surface.transform.position.x - GetRealPlaneSize().x / 2) &&
  387. !(helpPoint2.x - 0.3f < surface.transform.position.x + GetRealPlaneSize().x / 2) &&
  388. !(helpPoint2.z + 0.3f > surface.transform.position.z - GetRealPlaneSize().y / 2) &&
  389. !(helpPoint2.z - 0.3f < surface.transform.position.z + GetRealPlaneSize().y / 2)
  390. )
  391. return false;
  392. }
  393. else
  394. {
  395. if(Vector3.Distance(helpPoint2, surface.transform.position) >= GetRealPlaneSize().x / 2 - 0.3f)
  396. return false;
  397. }
  398. }
  399. return true;
  400. }
  401. Vector2 GetRealPlaneSize()
  402. {
  403. Vector3 meshSize = surface.GetComponent<MeshRenderer>().bounds.size;
  404. return new Vector2(meshSize.x, meshSize.z);
  405. }
  406. Vector2 GetRealPeopleModelSize()
  407. {
  408. Vector3 meshSize = peoplePrefabs[1].GetComponent<MeshRenderer>().bounds.size;
  409. return new Vector2(meshSize.x, meshSize.z);
  410. }
  411. }