WayFindingManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 寻路管理
  6. /// </summary>
  7. public class WayFindingManager : MonoSingleton<WayFindingManager>
  8. {
  9. public Transform leftLine;
  10. public Transform rightLine;
  11. public Transform player;
  12. public Transform serverItem;
  13. private Transform oldServerItem;
  14. public LineRenderer line;
  15. public GameObject route;
  16. public GameObject deviceName;
  17. public Transform lookPoint;
  18. public List<Route> listRoot;
  19. private List<GameObject> listDeviceDoorName;
  20. private void Start()
  21. {
  22. listRoot = new List<Route>();
  23. listDeviceDoorName = new List<GameObject>();
  24. StartCoroutine(SetHighRoute());
  25. }
  26. private void Update()
  27. {
  28. // player.position = OpenXRCamera.Instance.head.position;
  29. //if (serverItem != null&&Vector3.Distance(new Vector3(serverItem.position.x, 0, serverItem.position.z), new Vector3(OpenXRCamera.Instance.head.position.x, 0, OpenXRCamera.Instance.head.position.z)) < 1f)
  30. //{
  31. // line.positionCount = 0;
  32. // oldServerItem = serverItem;
  33. // serverItem = null;
  34. // // MachineRoomManager.Instance.RandomRange();
  35. //}
  36. }
  37. public void SetServerItem( Transform serverItem)
  38. {
  39. this.serverItem = serverItem;
  40. if ((oldServerItem != null && serverItem.localPosition.z == oldServerItem.localPosition.z)) // 再一次生成的位置和当前的机柜在同一行
  41. {
  42. line.positionCount = 2;
  43. line.SetPosition(0, new Vector3(oldServerItem.localPosition.x, 0, oldServerItem.localPosition.z+MachineRoomManager.Instance.data.cabinetW/2.0f+MachineRoomManager.Instance.data.intervalL/2.0f));
  44. line.SetPosition(1, new Vector3(serverItem.localPosition.x, 0, serverItem.localPosition.z + MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f));
  45. }
  46. else
  47. {
  48. float leftDis = Vector3.Distance(new Vector3(player.localPosition.x, 0, player.localPosition.z), new Vector3(leftLine.localPosition.x, 0, leftLine.localPosition.z));
  49. float rightDis = Vector3.Distance(new Vector3(player.localPosition.x, 0, player.localPosition.z), new Vector3(rightLine.localPosition.x, 0, rightLine.localPosition.z));
  50. if (Mathf.Abs(leftDis) < Mathf.Abs(rightDis))
  51. {
  52. line.positionCount = 4;
  53. line.SetPosition(0, new Vector3(player.localPosition.x, 0, player.localPosition.z));
  54. line.SetPosition(1, new Vector3(leftLine.localPosition.x, 0, player.localPosition.z));
  55. line.SetPosition(2, new Vector3(leftLine.localPosition.x, 0, serverItem.localPosition.z + MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f));
  56. line.SetPosition(3, new Vector3(serverItem.localPosition.x, 0, serverItem.localPosition.z + MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f));
  57. }
  58. else
  59. {
  60. line.positionCount = 4;
  61. line.SetPosition(0, new Vector3(player.localPosition.x, 0, player.localPosition.z));
  62. line.SetPosition(1, new Vector3(rightLine.localPosition.x, 0, player.localPosition.z));
  63. line.SetPosition(2, new Vector3(rightLine.localPosition.x, 0, serverItem.localPosition.z));
  64. line.SetPosition(3, new Vector3(serverItem.localPosition.x, 0, serverItem.localPosition.z));
  65. }
  66. }
  67. }
  68. public void SetLine( Vector3 leftPos, Vector3 rightPos)
  69. {
  70. leftLine.localPosition = leftPos;
  71. rightLine.localPosition = rightPos;
  72. }
  73. public void NavigatorServer(MachineObj serverItem)
  74. {
  75. // if(listRoot!=null)
  76. for (int i = 0; i < listRoot.Count; i++)
  77. {
  78. listRoot[i].gameObject.SetActive(false);
  79. }
  80. listRoot.Clear();
  81. for (int i = 0; i < listDeviceDoorName.Count; i++)
  82. {
  83. listDeviceDoorName[i].SetActive(false);
  84. }
  85. listDeviceDoorName.Clear();
  86. this.serverItem = serverItem.transform;
  87. //if (Mathf.Abs(player.localPosition.z- serverItem.transform.localPosition.z) < (MachineRoomManager.Instance.data.intervalL/2.0f+MachineRoomManager.Instance.data.cabinetW/2.0f)) // 我和当前导览的位置在同一行
  88. Debug.Log(serverItem.isSingle +" "+ player.localPosition.z +" "+serverItem.transform.localPosition.z+ " " + Mathf.Abs(player.localPosition.z - serverItem.transform.localPosition.z) + " " + MachineRoomManager.Instance.data.intervalL);
  89. if (serverItem.isSingle ? player.localPosition.z > serverItem.transform.localPosition.z : player.localPosition.z < serverItem.transform.localPosition.z && (Mathf.Abs(player.localPosition.z - serverItem.transform.localPosition.z-0.01f) < MachineRoomManager.Instance.data.intervalL+ MachineRoomManager.Instance.data.cabinetW/2.0f))
  90. {
  91. Debug.Log("DGJ ===> tong h");
  92. Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
  93. Vector3 pos2 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  94. List<Vector3> listPos = new List<Vector3>();
  95. listPos.Add(pos1);
  96. listPos.Add(pos2);
  97. listPos.Add(new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z));
  98. Bezier.Instance.SetRoute(listPos);
  99. CreateRoute(listPos,serverItem,true);
  100. }
  101. else
  102. {
  103. float leftDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(leftLine.localPosition.x, -1, leftLine.localPosition.z));
  104. float rightDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(rightLine.localPosition.x, -1, rightLine.localPosition.z));
  105. if (Mathf.Abs(leftDis) < Mathf.Abs(rightDis))
  106. {
  107. Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
  108. Vector3 pos2 = new Vector3(leftLine.localPosition.x, -1, player.localPosition.z);
  109. //Vector3 pos3 = new Vector3(leftLine.localPosition.x, 0, serverItem.navigatorPoint.localPosition.z + (serverItem.isSingle ? -1 : 1) * MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f);
  110. //Vector3 pos4 = new Vector3(serverItem.navigatorPoint.localPosition.x, 0, serverItem.navigatorPoint.localPosition.z + (serverItem.isSingle ? -1 : 1) * MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f);
  111. Vector3 pos3 = new Vector3(leftLine.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  112. Vector3 pos4 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  113. List<Vector3> listpos = new List<Vector3>();
  114. listpos.Add(pos1);
  115. listpos.Add(pos2);
  116. listpos.Add(pos3);
  117. listpos.Add(pos4);
  118. listpos.Add(new Vector3(serverItem.transform.localPosition.x,-1, serverItem.transform.localPosition.z));
  119. Bezier.Instance.SetRoute(listpos);
  120. CreateRoute(listpos,serverItem,true);
  121. }
  122. else
  123. {
  124. //line.positionCount = 4;
  125. //line.SetPosition(0, new Vector3(player.localPosition.x, 0, player.localPosition.z));
  126. //line.SetPosition(1, new Vector3(rightLine.localPosition.x, 0, player.localPosition.z));
  127. //line.SetPosition(2, new Vector3(rightLine.localPosition.x, 0, serverItem.localPosition.z));
  128. //line.SetPosition(3, new Vector3(serverItem.localPosition.x, 0, serverItem.localPosition.z));
  129. Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
  130. Vector3 pos2 = new Vector3(rightLine.localPosition.x, -1, player.localPosition.z);
  131. //Vector3 pos3 = new Vector3(rightLine.localPosition.x, 0, serverItem.navigatorPoint.localPosition.z + (serverItem.isSingle ? -1 : 1) * MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f);
  132. //Vector3 pos4 = new Vector3(serverItem.navigatorPoint.localPosition.x, 0, serverItem.navigatorPoint.localPosition.z + (serverItem.isSingle ? -1 : 1) * MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f);
  133. Vector3 pos3 = new Vector3(rightLine.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  134. Vector3 pos4 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  135. List<Vector3> listpos = new List<Vector3>();
  136. listpos.Add(pos1);
  137. listpos.Add(pos2);
  138. listpos.Add(pos3);
  139. listpos.Add(pos4);
  140. listpos.Add(new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z));
  141. Bezier.Instance.SetRoute(listpos);
  142. CreateRoute(listpos, serverItem, true);
  143. }
  144. }
  145. }
  146. public void NavigatorServer(List<MachineObj> listServerItem)
  147. {
  148. for (int i = 0; i < listRoot.Count; i++)
  149. {
  150. listRoot[i].transform.parent.gameObject.SetActive(false);
  151. }
  152. listRoot.Clear();
  153. float leftDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(leftLine.localPosition.x, 0, leftLine.localPosition.z));
  154. float rightDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(rightLine.localPosition.x, 0, rightLine.localPosition.z));
  155. Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
  156. Vector3 pos2;
  157. if(Mathf.Abs( leftDis)<Mathf.Abs( rightDis))
  158. {
  159. pos2 = new Vector3(leftLine.localPosition.x, -1, player.localPosition.z);
  160. }
  161. else
  162. {
  163. pos2 = new Vector3(rightLine.localPosition.x, -1, player.localPosition.z);
  164. }
  165. List<Vector3> listpos = new List<Vector3>();
  166. listpos.Add(pos1);
  167. listpos.Add(pos2);
  168. listpos.Add(new Vector3(rightLine.localPosition.x, -1, player.localPosition.z + 10000));
  169. CreateRoute(listpos,null,false); // 依据Player 生成两个固定点 当前Player点 和 出口点
  170. List<Vector3> listentrance = new List<Vector3>();
  171. for (int i = 0; i < listServerItem.Count; i++)
  172. {
  173. listpos.Clear();
  174. listpos.Add(new Vector3(listServerItem[i].transform.localPosition.x, -1, listServerItem[i].transform.localPosition.z + ((listServerItem[i].isSingle ? 1 : -1) * 1)));
  175. listpos.Add(listServerItem[i].transform.localPosition);
  176. CreateRoute(listpos,listServerItem[i],true); // 根据机位生成 机位点
  177. Vector3 entrance = new Vector3(Mathf.Abs(leftDis) < Mathf.Abs(rightDis)?leftLine.localPosition.x:rightLine.localPosition.x, -1, listServerItem[i].transform.localPosition.z + ((listServerItem[i].isSingle ? 1 : -1) * 1));
  178. bool finish = true;
  179. for (int j = 0; j < listentrance.Count; j++)
  180. {
  181. if(entrance == listentrance[i])
  182. {
  183. finish = false;
  184. break;
  185. }
  186. }
  187. if(finish)
  188. {
  189. if (Mathf.Abs(player.localPosition.z - listServerItem[i].transform.localPosition.z) < (MachineRoomManager.Instance.data.intervalL / 2.0f + MachineRoomManager.Instance.data.cabinetW / 2.0f)) // 我和当前导览的位置在同一行
  190. {
  191. }
  192. else
  193. {
  194. List<Vector3> listentrancePos = new List<Vector3>();
  195. listentrancePos.Add(entrance);
  196. listentrancePos.Add(new Vector3(listServerItem[i].transform.localPosition.x, -1, listServerItem[i].transform.localPosition.z + ((listServerItem[i].isSingle ? 1 : -1) * 1)));
  197. CreateRoute(listentrancePos, listServerItem[i], false); // 生成当前机位的 入口点
  198. }
  199. }
  200. }
  201. }
  202. private void CreateRoute(List<Vector3> listPos, MachineObj machineObj, bool isdevice)
  203. {
  204. //for (int i = 0; i < listPos.Count - 1; i++)
  205. //{
  206. // GameObject route1 = GameObject.Instantiate(route, MachineRoomManager.Instance.transform);
  207. // route1.transform.localPosition = listPos[i];
  208. // lookPoint.localPosition = listPos[i + 1];
  209. // route1.transform.LookAt(lookPoint);
  210. // route1.SetActive(true);
  211. // listRoot.Add(route1.transform.GetChild(0).GetComponent<Route>());
  212. // listRoot[i].SetRoute(route1.transform.position, lookPoint.position, 0,lookPoint);
  213. // GameObject obj = new GameObject(i.ToString());
  214. // obj.transform.position = listPos[i];
  215. //}
  216. if (isdevice && machineObj)
  217. {
  218. lookPoint.localPosition = listPos[listPos.Count - 1];
  219. GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
  220. devicedoorName.transform.localPosition = listPos[listPos.Count - 2];
  221. devicedoorName.transform.LookAt(lookPoint);
  222. devicedoorName.transform.localPosition = machineObj.transform.localPosition + new Vector3(0, 0, 0);
  223. listDeviceDoorName.Add(devicedoorName);
  224. devicedoorName.GetComponent<SettingDeviceName>().SettingName(machineObj.deviceParameters.deviceName);
  225. }
  226. }
  227. private void CreateRoute(List<Vector3> listPos, bool isDevice)
  228. {
  229. for (int i = 0; i < listPos.Count - 1; i++)
  230. {
  231. GameObject route1 = GameObject.Instantiate(route, MachineRoomManager.Instance.transform);
  232. route1.transform.localPosition = listPos[i];
  233. lookPoint.localPosition = listPos[i + 1];
  234. route1.transform.LookAt(lookPoint);
  235. route1.SetActive(true);
  236. listRoot.Add(route1.transform.GetChild(0).GetComponent<Route>());
  237. }
  238. if (isDevice)
  239. {
  240. GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
  241. devicedoorName.transform.localPosition = listPos[listPos.Count - 2];
  242. lookPoint.localPosition += new Vector3(0, 0, 10);
  243. devicedoorName.transform.LookAt(lookPoint);
  244. listDeviceDoorName.Add(devicedoorName);
  245. }
  246. }
  247. public void CloseWayFind()
  248. {
  249. for (int i = 0; i < listRoot.Count; i++)
  250. {
  251. listRoot[i].transform.parent.gameObject.SetActive(false);
  252. }
  253. listRoot.Clear();
  254. for(int i = 0;i<listDeviceDoorName.Count;i++)
  255. {
  256. listDeviceDoorName[i].gameObject.SetActive(false);
  257. }
  258. listDeviceDoorName.Clear();
  259. }
  260. private IEnumerator SetHighRoute()
  261. {
  262. while(true)
  263. {
  264. yield return new WaitForSeconds(0.2f);
  265. if(listRoot!=null&& listRoot.Count>1)
  266. {
  267. Route route = null;
  268. route = listRoot[0];
  269. for (int i = 1; i < listRoot.Count; i++)
  270. {
  271. // listRoot[i].setHighLight(true);
  272. //if (Mathf.Abs(Vector3.Distance(player.transform.position, listRoot[i].transform.position)) < Mathf.Abs(Vector3.Distance(player.transform.position, route.transform.position)))
  273. //{
  274. // route.setHighLight(false);
  275. // route = listRoot[i];
  276. //}
  277. }
  278. // route.setHighLight(true);
  279. }
  280. }
  281. }
  282. }