PeopleWalkPath.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. public class PeopleWalkPath : WalkPath
  5. {
  6. public enum EnumMove { Walk, Run };
  7. public enum EnumDir { Forward, Backward, HugLeft, HugRight, WeaveLeft, WeaveRight };
  8. [HideInInspector] [Tooltip("Type of movement / Тип движения")] [SerializeField] private EnumMove _moveType;
  9. [Tooltip("Direction of movement / Направление движения. Левостороннее, правостороннее, итд.")] [SerializeField] private EnumDir direction;
  10. [HideInInspector] [Tooltip("Speed of walk / Скорость ходьбы")] [SerializeField] private float walkSpeed = 1;
  11. [HideInInspector] [Tooltip("Speed of run / Скорость бега")] [SerializeField] private float runSpeed = 4;
  12. [HideInInspector] public bool isWalk;
  13. [HideInInspector] [SerializeField] [Tooltip("Set your animation speed? / Установить свою скорость анимации?")] private bool _overrideDefaultAnimationMultiplier = true;
  14. [HideInInspector] [SerializeField] [Tooltip("Speed animation of walking / Скорость анимации ходьбы")] private float _customWalkAnimationMultiplier = 1.1f;
  15. [HideInInspector] [SerializeField] [Tooltip("Running animation speed / Скорость анимации бега")] private float _customRunAnimationMultiplier = 0.3f;
  16. public override void DrawCurved(bool withDraw)
  17. {
  18. if (numberOfWays< 1) numberOfWays = 1;
  19. if (lineSpacing< 0.6f) lineSpacing = 0.6f;
  20. _forward = new bool[numberOfWays];
  21. isWalk = (_moveType.ToString() == "Walk") ? true : false;
  22. for (int w = 0; w<numberOfWays; w++)
  23. {
  24. if (direction.ToString() == "Forward")
  25. {
  26. _forward[w] = true;
  27. }
  28. else if (direction.ToString() == "Backward")
  29. {
  30. _forward[w] = false;
  31. }
  32. else if (direction.ToString() == "HugLeft")
  33. {
  34. if ((w + 2) % 2 == 0)
  35. _forward[w] = true;
  36. else
  37. _forward[w] = false;
  38. }
  39. else if (direction.ToString() == "HugRight")
  40. {
  41. if ((w + 2) % 2 == 0)
  42. _forward[w] = false;
  43. else
  44. _forward[w] = true;
  45. }
  46. else if (direction.ToString() == "WeaveLeft")
  47. {
  48. if (w == 1 || w == 2 || (w - 1) % 4 == 0 || (w - 2) % 4 == 0)
  49. _forward[w] = false;
  50. else _forward[w] = true;
  51. }
  52. else if (direction.ToString() == "WeaveRight")
  53. {
  54. if (w == 1 || w == 2 || (w - 1) % 4 == 0 || (w - 2) % 4 == 0)
  55. _forward[w] = true;
  56. else _forward[w] = false;
  57. }
  58. }
  59. if (pathPoint.Count< 2) return;
  60. points = new Vector3[numberOfWays, pathPoint.Count + 2];
  61. pointLength[0] = pathPoint.Count + 2;
  62. for (int i = 0; i<pathPointTransform.Count; i++)
  63. {
  64. Vector3 vectorStart;
  65. Vector3 vectorEnd;
  66. if (i == 0)
  67. {
  68. if (loopPath)
  69. {
  70. vectorStart = pathPointTransform[pathPointTransform.Count - 1].transform.position - pathPointTransform[i].transform.position;
  71. }
  72. else
  73. {
  74. vectorStart = Vector3.zero;
  75. }
  76. vectorEnd = pathPointTransform[i].transform.position - pathPointTransform[i + 1].transform.position;
  77. }
  78. else if (i == pathPointTransform.Count - 1)
  79. {
  80. vectorStart = pathPointTransform[i - 1].transform.position - pathPointTransform[i].transform.position;
  81. if (loopPath)
  82. {
  83. vectorEnd = pathPointTransform[i].transform.position - pathPointTransform[0].transform.position;
  84. }
  85. else
  86. {
  87. vectorEnd = Vector3.zero;
  88. }
  89. }
  90. else
  91. {
  92. vectorStart = pathPointTransform[i - 1].transform.position - pathPointTransform[i].transform.position;
  93. vectorEnd = pathPointTransform[i].transform.position - pathPointTransform[i + 1].transform.position;
  94. }
  95. Vector3 vectorShift = Vector3.Normalize((Quaternion.Euler(0, 90, 0) * (vectorStart + vectorEnd)));
  96. points[0, i + 1] = numberOfWays % 2 == 1 ? pathPointTransform[i].transform.position : pathPointTransform[i].transform.position + vectorShift* lineSpacing / 2;
  97. if (numberOfWays > 1) points[1, i + 1] = points[0, i + 1] - vectorShift* lineSpacing;
  98. for (int w = 1; w<numberOfWays; w++)
  99. {
  100. points[w, i + 1] = points[0, i + 1] + vectorShift* lineSpacing * (float) (Math.Pow(-1, w)) * ((w + 1) / 2);
  101. }
  102. }
  103. for (int w = 0; w<numberOfWays; w++)
  104. {
  105. points[w, 0] = points[w, 1];
  106. points[w, pointLength[0] - 1] = points[w, pointLength[0] - 2];
  107. }
  108. if (withDraw)
  109. {
  110. for (int w = 0; w<numberOfWays; w++)
  111. {
  112. if (loopPath)
  113. {
  114. Gizmos.color = (_forward[w] ? Color.green : Color.red);
  115. Gizmos.DrawLine(points[w, 0], points[w, pathPoint.Count]);
  116. }
  117. for (int i = 1; i<pathPoint.Count; i++)
  118. {
  119. Gizmos.color = (_forward[w] ? Color.green : Color.red);
  120. Gizmos.DrawLine(points[w, i + 1], points[w, i]);
  121. }
  122. }
  123. }
  124. }
  125. public override void SpawnOnePeople(int w, bool forward, float walkSpeed, float runSpeed)
  126. {
  127. int prefabNum = UnityEngine.Random.Range(0, peoplePrefabs.Length);
  128. var people = gameObject;
  129. if (!forward)
  130. people = Instantiate(peoplePrefabs[prefabNum], points[w, pointLength[0] - 2], Quaternion.identity) as GameObject;
  131. else
  132. people = Instantiate(peoplePrefabs[prefabNum], points[w, 1], Quaternion.identity) as GameObject;
  133. var _movePath = people.AddComponent<MovePath>();
  134. _movePath.randXFinish = UnityEngine.Random.Range(-randXPos, randXPos);
  135. _movePath.randZFinish = UnityEngine.Random.Range(-randZPos, randZPos);
  136. people.transform.parent = par.transform;
  137. _movePath.walkPath = gameObject;
  138. string animName;
  139. if (isWalk)
  140. animName = "walk";
  141. else
  142. animName = "run";
  143. _movePath.InitializeAnimation(_overrideDefaultAnimationMultiplier, _customWalkAnimationMultiplier, _customRunAnimationMultiplier);
  144. if (!forward)
  145. {
  146. _movePath.MyStart(w, pointLength[0] - 2, animName, loopPath, forward, walkSpeed, runSpeed);
  147. people.transform.LookAt(points[w, pointLength[0] - 3]);
  148. }
  149. else
  150. {
  151. _movePath.MyStart(w, 1, animName, loopPath, forward, walkSpeed, runSpeed);
  152. people.transform.LookAt(points[w, 2]);
  153. }
  154. }
  155. public override void SpawnPeople()
  156. {
  157. List<GameObject> pfb = new List<GameObject>(peoplePrefabs);
  158. for (int i = pfb.Count - 1; i >= 0; i--)
  159. {
  160. if (pfb[i] == null)
  161. {
  162. pfb.RemoveAt(i);
  163. }
  164. }
  165. peoplePrefabs = pfb.ToArray();
  166. if (points == null) DrawCurved(false);
  167. if (par == null)
  168. {
  169. par = new GameObject();
  170. par.transform.parent = gameObject.transform;
  171. par.name = "people";
  172. }
  173. int pathPointCount;
  174. if (!loopPath)
  175. {
  176. pathPointCount = pointLength[0] - 2;
  177. }
  178. else
  179. {
  180. pathPointCount = pointLength[0] - 1;
  181. }
  182. if (pathPointCount < 2) return;
  183. var pCount = loopPath ? pointLength[0] - 1 : pointLength[0] - 2;
  184. for (int wayIndex = 0; wayIndex < numberOfWays; wayIndex++)
  185. {
  186. _distances = new float[pCount];
  187. float pathLength = 0f;
  188. for (int i = 1; i < pCount; i++)
  189. {
  190. Vector3 vector;
  191. if (loopPath && i == pCount - 1)
  192. {
  193. vector = points[wayIndex, 1] - points[wayIndex, pCount];
  194. }
  195. else
  196. {
  197. vector = points[wayIndex, i + 1] - points[wayIndex, i];
  198. }
  199. pathLength += vector.magnitude;
  200. _distances[i] = pathLength;
  201. }
  202. bool forward = false;
  203. switch (direction.ToString())
  204. {
  205. case "Forward":
  206. forward = true;
  207. break;
  208. case "Backward":
  209. forward = false;
  210. break;
  211. case "HugLeft":
  212. forward = (wayIndex + 2) % 2 == 0;
  213. break;
  214. case "HugRight":
  215. forward = (wayIndex + 2) % 2 != 0;
  216. break;
  217. case "WeaveLeft":
  218. forward = wayIndex != 1 && wayIndex != 2 && (wayIndex - 1) % 4 != 0 && (wayIndex - 2) % 4 != 0;
  219. break;
  220. case "WeaveRight":
  221. forward = wayIndex == 1 || wayIndex == 2 || (wayIndex - 1) % 4 == 0 || (wayIndex - 2) % 4 == 0;
  222. break;
  223. }
  224. int peopleCount = Mathf.FloorToInt((Density * pathLength) / _minimalObjectLength);
  225. float segmentLen = _minimalObjectLength + (pathLength - (peopleCount * _minimalObjectLength)) / peopleCount;
  226. int[] pickList = CommonUtils.GetRandomPrefabIndexes(peopleCount, ref peoplePrefabs);
  227. Vector3[] pointArray = new Vector3[_distances.Length];
  228. for (int i = 1; i < _distances.Length; i++)
  229. {
  230. pointArray[i - 1] = points[wayIndex, i];
  231. }
  232. pointArray[_distances.Length - 1] = loopPath ? points[wayIndex, 1] : points[wayIndex, _distances.Length];
  233. for (int peopleIndex = 0; peopleIndex < peopleCount; peopleIndex++)
  234. {
  235. var people = gameObject;
  236. var randomShift = UnityEngine.Random.Range(-segmentLen / 3f, segmentLen / 3f) + (wayIndex * segmentLen);
  237. var finalRandomDistance = (peopleIndex + 1) * segmentLen + randomShift;
  238. Vector3 routePosition = GetRoutePosition(pointArray, finalRandomDistance, pCount, loopPath);
  239. float XPos = UnityEngine.Random.Range(-randXPos, randXPos);
  240. float ZPos = UnityEngine.Random.Range(-randZPos, randZPos);
  241. routePosition = new Vector3(routePosition.x + XPos, routePosition.y, routePosition.z + ZPos);
  242. Vector3 or;
  243. RaycastHit[] rrr = Physics.RaycastAll(or = new Vector3(routePosition.x, routePosition.y + 10000, routePosition.z), Vector3.down, Mathf.Infinity);
  244. float dist = 0;
  245. int bestCandidate = 0;
  246. rrr = Physics.RaycastAll(or = new Vector3(routePosition.x, routePosition.y + 10000, routePosition.z), Vector3.down, Mathf.Infinity);
  247. for (int i = 0; i < rrr.Length; i++)
  248. {
  249. if (dist < Vector3.Distance(rrr[0].point, or))
  250. {
  251. bestCandidate = i;
  252. dist = Vector3.Distance(rrr[0].point, or);
  253. }
  254. }
  255. if (rrr.Length > 0)
  256. {
  257. routePosition.y = rrr[bestCandidate].point.y;
  258. }
  259. people = Instantiate(peoplePrefabs[pickList[peopleIndex]], routePosition, Quaternion.identity) as GameObject;
  260. var movePath = people.AddComponent<MovePath>();
  261. movePath.randXFinish = XPos;
  262. movePath.randZFinish = ZPos;
  263. people.transform.parent = par.transform;
  264. movePath.walkPath = gameObject;
  265. string animName;
  266. if (isWalk)
  267. animName = "walk";
  268. else
  269. animName = "run";
  270. movePath.MyStart(wayIndex, GetRoutePoint((peopleIndex + 1) * segmentLen + randomShift, wayIndex, pCount, forward, loopPath), animName, loopPath, forward, walkSpeed, runSpeed);
  271. movePath.InitializeAnimation(_overrideDefaultAnimationMultiplier, _customWalkAnimationMultiplier, _customRunAnimationMultiplier);
  272. movePath.SetLookPosition();
  273. }
  274. }
  275. }
  276. }