WayFindingManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. leftLine.localPosition = new Vector3(player.localPosition.x, leftLine.localPosition.y, leftLine.localPosition.z);
  73. }
  74. public void NavigatorServer(MachineObj serverItem)
  75. {
  76. // if(listRoot!=null)
  77. for (int i = 0; i < listRoot.Count; i++)
  78. {
  79. listRoot[i].gameObject.SetActive(false);
  80. }
  81. listRoot.Clear();
  82. Bezier.Instance.RouteClear();
  83. for (int i = 0; i < listDeviceDoorName.Count; i++)
  84. {
  85. listDeviceDoorName[i].SetActive(false);
  86. }
  87. listDeviceDoorName.Clear();
  88. this.serverItem = serverItem.transform;
  89. //if (Mathf.Abs(player.localPosition.z- serverItem.transform.localPosition.z) < (MachineRoomManager.Instance.data.intervalL/2.0f+MachineRoomManager.Instance.data.cabinetW/2.0f)) // 我和当前导览的位置在同一行
  90. Debug.Log(serverItem.isSingle +" "+ player.localPosition.z +" "+serverItem.transform.localPosition.z+ " " + Mathf.Abs(player.localPosition.z - serverItem.transform.localPosition.z) + " " + MachineRoomManager.Instance.data.intervalL);
  91. 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))
  92. {
  93. Debug.Log("DGJ ===> tong h");
  94. Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
  95. Vector3 pos2 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  96. Vector3 pos3 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z);
  97. lookPoint.localPosition = pos1;
  98. pos1 = lookPoint.position;
  99. lookPoint.localPosition = pos2;
  100. pos2 = lookPoint.position;
  101. lookPoint.localPosition = pos3;
  102. pos3 = lookPoint.position;
  103. lookPoint.localPosition = pos3;
  104. GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
  105. devicedoorName.transform.localPosition = pos2;
  106. devicedoorName.transform.LookAt(lookPoint);
  107. devicedoorName.transform.localPosition = serverItem.transform.localPosition + new Vector3(0, 0, 0);
  108. listDeviceDoorName.Add(devicedoorName);
  109. devicedoorName.GetComponent<SettingDeviceName>().SettingName(serverItem.deviceParameters.deviceName);
  110. List<Vector3> listPos = new List<Vector3>();
  111. listPos.Add(pos1);
  112. listPos.Add(pos2);
  113. listPos.Add(pos3);
  114. Bezier.Instance.SetRoute(listPos);
  115. // CreateRoute(listPos,serverItem,true);
  116. }
  117. else
  118. {
  119. float leftDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(leftLine.localPosition.x, -1, leftLine.localPosition.z));
  120. float rightDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(rightLine.localPosition.x, -1, rightLine.localPosition.z));
  121. if (Mathf.Abs(leftDis) < Mathf.Abs(rightDis))
  122. {
  123. Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
  124. Vector3 pos2 = new Vector3(leftLine.localPosition.x, -1, player.localPosition.z);
  125. //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);
  126. //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);
  127. Vector3 pos3 = new Vector3(leftLine.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  128. Vector3 pos4 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  129. Vector3 pos5 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z);
  130. lookPoint.localPosition = pos5;
  131. GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
  132. devicedoorName.transform.localPosition = pos4;
  133. devicedoorName.transform.LookAt(lookPoint);
  134. devicedoorName.transform.localPosition = serverItem.transform.localPosition + new Vector3(0, 0, 0);
  135. listDeviceDoorName.Add(devicedoorName);
  136. devicedoorName.GetComponent<SettingDeviceName>().SettingName(serverItem.deviceParameters.deviceName);
  137. lookPoint.localPosition = pos1;
  138. pos1 = lookPoint.position;
  139. lookPoint.localPosition = pos2;
  140. pos2 = lookPoint.position;
  141. lookPoint.localPosition = pos3;
  142. pos3 = lookPoint.position;
  143. lookPoint.localPosition = pos4;
  144. pos4 = lookPoint.position;
  145. lookPoint.localPosition = pos5;
  146. pos5 = lookPoint.position;
  147. List<Vector3> listpos = new List<Vector3>();
  148. listpos.Add(pos1);
  149. listpos.Add(pos2);
  150. listpos.Add(pos3);
  151. listpos.Add(pos4);
  152. listpos.Add(pos5);
  153. Bezier.Instance.SetRoute(listpos);
  154. // CreateRoute(listpos,serverItem,true);
  155. }
  156. else
  157. {
  158. //line.positionCount = 4;
  159. //line.SetPosition(0, new Vector3(player.localPosition.x, 0, player.localPosition.z));
  160. //line.SetPosition(1, new Vector3(rightLine.localPosition.x, 0, player.localPosition.z));
  161. //line.SetPosition(2, new Vector3(rightLine.localPosition.x, 0, serverItem.localPosition.z));
  162. //line.SetPosition(3, new Vector3(serverItem.localPosition.x, 0, serverItem.localPosition.z));
  163. Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
  164. Vector3 pos2 = new Vector3(rightLine.localPosition.x, -1, player.localPosition.z);
  165. //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);
  166. //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);
  167. Vector3 pos3 = new Vector3(rightLine.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  168. Vector3 pos4 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
  169. Vector3 pos5 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z);
  170. lookPoint.localPosition = pos5;
  171. GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
  172. devicedoorName.transform.localPosition = pos4;
  173. devicedoorName.transform.LookAt(lookPoint);
  174. devicedoorName.transform.localPosition = serverItem.transform.localPosition + new Vector3(0, 0, 0);
  175. listDeviceDoorName.Add(devicedoorName);
  176. devicedoorName.GetComponent<SettingDeviceName>().SettingName(serverItem.deviceParameters.deviceName);
  177. lookPoint.localPosition = pos1;
  178. pos1 = lookPoint.position;
  179. lookPoint.localPosition = pos2;
  180. pos2 = lookPoint.position;
  181. lookPoint.localPosition = pos3;
  182. pos3 = lookPoint.position;
  183. lookPoint.localPosition = pos4;
  184. pos4 = lookPoint.position;
  185. lookPoint.localPosition = pos5;
  186. pos5 = lookPoint.position;
  187. List<Vector3> listpos = new List<Vector3>();
  188. listpos.Add(pos1);
  189. listpos.Add(pos2);
  190. listpos.Add(pos3);
  191. listpos.Add(pos4);
  192. listpos.Add(pos5);
  193. Bezier.Instance.SetRoute(listpos);
  194. // CreateRoute(listpos, serverItem, true);
  195. }
  196. }
  197. }
  198. public void NavigatorServer(List<MachineObj> listServerItem)
  199. {
  200. for (int i = 0; i < listRoot.Count; i++)
  201. {
  202. listRoot[i].transform.parent.gameObject.SetActive(false);
  203. }
  204. listRoot.Clear();
  205. float leftDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(leftLine.localPosition.x, 0, leftLine.localPosition.z));
  206. float rightDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(rightLine.localPosition.x, 0, rightLine.localPosition.z));
  207. Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
  208. Vector3 pos2;
  209. if(Mathf.Abs( leftDis)<Mathf.Abs( rightDis))
  210. {
  211. pos2 = new Vector3(leftLine.localPosition.x, -1, player.localPosition.z);
  212. }
  213. else
  214. {
  215. pos2 = new Vector3(rightLine.localPosition.x, -1, player.localPosition.z);
  216. }
  217. List<Vector3> listpos = new List<Vector3>();
  218. listpos.Add(pos1);
  219. listpos.Add(pos2);
  220. listpos.Add(new Vector3(rightLine.localPosition.x, -1, player.localPosition.z + 10000));
  221. CreateRoute(listpos,null,false); // 依据Player 生成两个固定点 当前Player点 和 出口点
  222. List<Vector3> listentrance = new List<Vector3>();
  223. for (int i = 0; i < listServerItem.Count; i++)
  224. {
  225. listpos.Clear();
  226. listpos.Add(new Vector3(listServerItem[i].transform.localPosition.x, -1, listServerItem[i].transform.localPosition.z + ((listServerItem[i].isSingle ? 1 : -1) * 1)));
  227. listpos.Add(listServerItem[i].transform.localPosition);
  228. CreateRoute(listpos,listServerItem[i],true); // 根据机位生成 机位点
  229. 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));
  230. bool finish = true;
  231. for (int j = 0; j < listentrance.Count; j++)
  232. {
  233. if(entrance == listentrance[i])
  234. {
  235. finish = false;
  236. break;
  237. }
  238. }
  239. if(finish)
  240. {
  241. if (Mathf.Abs(player.localPosition.z - listServerItem[i].transform.localPosition.z) < (MachineRoomManager.Instance.data.intervalL / 2.0f + MachineRoomManager.Instance.data.cabinetW / 2.0f)) // 我和当前导览的位置在同一行
  242. {
  243. }
  244. else
  245. {
  246. List<Vector3> listentrancePos = new List<Vector3>();
  247. listentrancePos.Add(entrance);
  248. listentrancePos.Add(new Vector3(listServerItem[i].transform.localPosition.x, -1, listServerItem[i].transform.localPosition.z + ((listServerItem[i].isSingle ? 1 : -1) * 1)));
  249. CreateRoute(listentrancePos, listServerItem[i], false); // 生成当前机位的 入口点
  250. }
  251. }
  252. }
  253. }
  254. private void CreateRoute(List<Vector3> listPos, MachineObj machineObj, bool isdevice)
  255. {
  256. //for (int i = 0; i < listPos.Count - 1; i++)
  257. //{
  258. // GameObject route1 = GameObject.Instantiate(route, MachineRoomManager.Instance.transform);
  259. // route1.transform.localPosition = listPos[i];
  260. // lookPoint.localPosition = listPos[i + 1];
  261. // route1.transform.LookAt(lookPoint);
  262. // route1.SetActive(true);
  263. // listRoot.Add(route1.transform.GetChild(0).GetComponent<Route>());
  264. // listRoot[i].SetRoute(route1.transform.position, lookPoint.position, 0,lookPoint);
  265. // GameObject obj = new GameObject(i.ToString());
  266. // obj.transform.position = listPos[i];
  267. //}
  268. if (isdevice && machineObj)
  269. {
  270. lookPoint.localPosition = listPos[listPos.Count - 1];
  271. GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
  272. devicedoorName.transform.localPosition = listPos[listPos.Count - 2];
  273. devicedoorName.transform.LookAt(lookPoint);
  274. devicedoorName.transform.localPosition = machineObj.transform.localPosition + new Vector3(0, 0, 0);
  275. listDeviceDoorName.Add(devicedoorName);
  276. devicedoorName.GetComponent<SettingDeviceName>().SettingName(machineObj.deviceParameters.deviceName);
  277. }
  278. }
  279. private void CreateRoute(List<Vector3> listPos, bool isDevice)
  280. {
  281. for (int i = 0; i < listPos.Count - 1; i++)
  282. {
  283. GameObject route1 = GameObject.Instantiate(route, MachineRoomManager.Instance.transform);
  284. route1.transform.localPosition = listPos[i];
  285. lookPoint.localPosition = listPos[i + 1];
  286. route1.transform.LookAt(lookPoint);
  287. route1.SetActive(true);
  288. listRoot.Add(route1.transform.GetChild(0).GetComponent<Route>());
  289. }
  290. if (isDevice)
  291. {
  292. GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
  293. devicedoorName.transform.localPosition = listPos[listPos.Count - 2];
  294. lookPoint.localPosition += new Vector3(0, 0, 10);
  295. devicedoorName.transform.LookAt(lookPoint);
  296. listDeviceDoorName.Add(devicedoorName);
  297. }
  298. }
  299. public void CloseWayFind()
  300. {
  301. for (int i = 0; i < listRoot.Count; i++)
  302. {
  303. listRoot[i].transform.parent.gameObject.SetActive(false);
  304. }
  305. listRoot.Clear();
  306. Bezier.Instance.RouteClear();
  307. for(int i = 0;i<listDeviceDoorName.Count;i++)
  308. {
  309. listDeviceDoorName[i].gameObject.SetActive(false);
  310. }
  311. listDeviceDoorName.Clear();
  312. }
  313. private IEnumerator SetHighRoute()
  314. {
  315. while(true)
  316. {
  317. yield return new WaitForSeconds(0.2f);
  318. if(listRoot!=null&& listRoot.Count>1)
  319. {
  320. Route route = null;
  321. route = listRoot[0];
  322. for (int i = 1; i < listRoot.Count; i++)
  323. {
  324. // listRoot[i].setHighLight(true);
  325. //if (Mathf.Abs(Vector3.Distance(player.transform.position, listRoot[i].transform.position)) < Mathf.Abs(Vector3.Distance(player.transform.position, route.transform.position)))
  326. //{
  327. // route.setHighLight(false);
  328. // route = listRoot[i];
  329. //}
  330. }
  331. // route.setHighLight(true);
  332. }
  333. }
  334. }
  335. }