123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 寻路管理
- /// </summary>
- public class WayFindingManager : MonoSingleton<WayFindingManager>
- {
- public Transform leftLine;
- public Transform rightLine;
- public Transform player;
- public Transform serverItem;
- private Transform oldServerItem;
- public LineRenderer line;
- public GameObject route;
- public GameObject deviceName;
- public Transform lookPoint;
- public List<Route> listRoot;
- private List<GameObject> listDeviceDoorName;
- private void Start()
- {
- listRoot = new List<Route>();
- listDeviceDoorName = new List<GameObject>();
- StartCoroutine(SetHighRoute());
- }
- private void Update()
- {
- // player.position = OpenXRCamera.Instance.head.position;
- //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)
- //{
- // line.positionCount = 0;
- // oldServerItem = serverItem;
- // serverItem = null;
- // // MachineRoomManager.Instance.RandomRange();
- //}
- }
- public void SetServerItem( Transform serverItem)
- {
- this.serverItem = serverItem;
- if ((oldServerItem != null && serverItem.localPosition.z == oldServerItem.localPosition.z)) // 再一次生成的位置和当前的机柜在同一行
- {
- line.positionCount = 2;
- line.SetPosition(0, new Vector3(oldServerItem.localPosition.x, 0, oldServerItem.localPosition.z+MachineRoomManager.Instance.data.cabinetW/2.0f+MachineRoomManager.Instance.data.intervalL/2.0f));
- line.SetPosition(1, new Vector3(serverItem.localPosition.x, 0, serverItem.localPosition.z + MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f));
-
- }
- else
- {
- float leftDis = Vector3.Distance(new Vector3(player.localPosition.x, 0, player.localPosition.z), new Vector3(leftLine.localPosition.x, 0, leftLine.localPosition.z));
- float rightDis = Vector3.Distance(new Vector3(player.localPosition.x, 0, player.localPosition.z), new Vector3(rightLine.localPosition.x, 0, rightLine.localPosition.z));
- if (Mathf.Abs(leftDis) < Mathf.Abs(rightDis))
- {
- line.positionCount = 4;
- line.SetPosition(0, new Vector3(player.localPosition.x, 0, player.localPosition.z));
- line.SetPosition(1, new Vector3(leftLine.localPosition.x, 0, player.localPosition.z));
- line.SetPosition(2, new Vector3(leftLine.localPosition.x, 0, serverItem.localPosition.z + MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f));
- line.SetPosition(3, new Vector3(serverItem.localPosition.x, 0, serverItem.localPosition.z + MachineRoomManager.Instance.data.cabinetW / 2.0f + MachineRoomManager.Instance.data.intervalL / 2.0f));
- }
- else
- {
- line.positionCount = 4;
- line.SetPosition(0, new Vector3(player.localPosition.x, 0, player.localPosition.z));
- line.SetPosition(1, new Vector3(rightLine.localPosition.x, 0, player.localPosition.z));
- line.SetPosition(2, new Vector3(rightLine.localPosition.x, 0, serverItem.localPosition.z));
- line.SetPosition(3, new Vector3(serverItem.localPosition.x, 0, serverItem.localPosition.z));
- }
- }
-
-
- }
- public void SetLine( Vector3 leftPos, Vector3 rightPos)
- {
- leftLine.localPosition = leftPos;
- rightLine.localPosition = rightPos;
- leftLine.localPosition = new Vector3(player.localPosition.x, leftLine.localPosition.y, leftLine.localPosition.z);
- }
- public void NavigatorServer(MachineObj serverItem)
- {
- // if(listRoot!=null)
- for (int i = 0; i < listRoot.Count; i++)
- {
- listRoot[i].gameObject.SetActive(false);
- }
- listRoot.Clear();
- Bezier.Instance.RouteClear();
- for (int i = 0; i < listDeviceDoorName.Count; i++)
- {
- listDeviceDoorName[i].SetActive(false);
- }
- listDeviceDoorName.Clear();
-
- this.serverItem = serverItem.transform;
- //if (Mathf.Abs(player.localPosition.z- serverItem.transform.localPosition.z) < (MachineRoomManager.Instance.data.intervalL/2.0f+MachineRoomManager.Instance.data.cabinetW/2.0f)) // 我和当前导览的位置在同一行
- Debug.Log(serverItem.isSingle +" "+ player.localPosition.z +" "+serverItem.transform.localPosition.z+ " " + Mathf.Abs(player.localPosition.z - serverItem.transform.localPosition.z) + " " + MachineRoomManager.Instance.data.intervalL);
- 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))
- {
- Debug.Log("DGJ ===> tong h");
- Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
- Vector3 pos2 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
- Vector3 pos3 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z);
- lookPoint.localPosition = pos1;
- pos1 = lookPoint.position;
- lookPoint.localPosition = pos2;
- pos2 = lookPoint.position;
- lookPoint.localPosition = pos3;
- pos3 = lookPoint.position;
- lookPoint.localPosition = pos3;
- GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
- devicedoorName.transform.localPosition = pos2;
- devicedoorName.transform.LookAt(lookPoint);
- devicedoorName.transform.localPosition = serverItem.transform.localPosition + new Vector3(0, 0, 0);
- listDeviceDoorName.Add(devicedoorName);
- devicedoorName.GetComponent<SettingDeviceName>().SettingName(serverItem.deviceParameters.deviceName);
- List<Vector3> listPos = new List<Vector3>();
- listPos.Add(pos1);
- listPos.Add(pos2);
- listPos.Add(pos3);
- Bezier.Instance.SetRoute(listPos);
- // CreateRoute(listPos,serverItem,true);
-
- }
- else
- {
- float leftDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(leftLine.localPosition.x, -1, leftLine.localPosition.z));
- float rightDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(rightLine.localPosition.x, -1, rightLine.localPosition.z));
- if (Mathf.Abs(leftDis) < Mathf.Abs(rightDis))
- {
- Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
- Vector3 pos2 = new Vector3(leftLine.localPosition.x, -1, player.localPosition.z);
- //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);
- //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);
- Vector3 pos3 = new Vector3(leftLine.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
- Vector3 pos4 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
- Vector3 pos5 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z);
- lookPoint.localPosition = pos5;
- GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
- devicedoorName.transform.localPosition = pos4;
- devicedoorName.transform.LookAt(lookPoint);
- devicedoorName.transform.localPosition = serverItem.transform.localPosition + new Vector3(0, 0, 0);
- listDeviceDoorName.Add(devicedoorName);
- devicedoorName.GetComponent<SettingDeviceName>().SettingName(serverItem.deviceParameters.deviceName);
- lookPoint.localPosition = pos1;
- pos1 = lookPoint.position;
- lookPoint.localPosition = pos2;
- pos2 = lookPoint.position;
- lookPoint.localPosition = pos3;
- pos3 = lookPoint.position;
- lookPoint.localPosition = pos4;
- pos4 = lookPoint.position;
- lookPoint.localPosition = pos5;
- pos5 = lookPoint.position;
- List<Vector3> listpos = new List<Vector3>();
- listpos.Add(pos1);
- listpos.Add(pos2);
- listpos.Add(pos3);
- listpos.Add(pos4);
- listpos.Add(pos5);
- Bezier.Instance.SetRoute(listpos);
- // CreateRoute(listpos,serverItem,true);
-
- }
- else
- {
- //line.positionCount = 4;
- //line.SetPosition(0, new Vector3(player.localPosition.x, 0, player.localPosition.z));
- //line.SetPosition(1, new Vector3(rightLine.localPosition.x, 0, player.localPosition.z));
- //line.SetPosition(2, new Vector3(rightLine.localPosition.x, 0, serverItem.localPosition.z));
- //line.SetPosition(3, new Vector3(serverItem.localPosition.x, 0, serverItem.localPosition.z));
- Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
- Vector3 pos2 = new Vector3(rightLine.localPosition.x, -1, player.localPosition.z);
- //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);
- //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);
- Vector3 pos3 = new Vector3(rightLine.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
- Vector3 pos4 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z + ((serverItem.isSingle ? 1 : -1) * 1));
- Vector3 pos5 = new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z);
- lookPoint.localPosition = pos5;
- GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
- devicedoorName.transform.localPosition = pos4;
- devicedoorName.transform.LookAt(lookPoint);
- devicedoorName.transform.localPosition = serverItem.transform.localPosition + new Vector3(0, 0, 0);
- listDeviceDoorName.Add(devicedoorName);
- devicedoorName.GetComponent<SettingDeviceName>().SettingName(serverItem.deviceParameters.deviceName);
- lookPoint.localPosition = pos1;
- pos1 = lookPoint.position;
- lookPoint.localPosition = pos2;
- pos2 = lookPoint.position;
- lookPoint.localPosition = pos3;
- pos3 = lookPoint.position;
- lookPoint.localPosition = pos4;
- pos4 = lookPoint.position;
- lookPoint.localPosition = pos5;
- pos5 = lookPoint.position;
- List<Vector3> listpos = new List<Vector3>();
- listpos.Add(pos1);
- listpos.Add(pos2);
- listpos.Add(pos3);
- listpos.Add(pos4);
- listpos.Add(pos5);
- Bezier.Instance.SetRoute(listpos);
- // CreateRoute(listpos, serverItem, true);
-
- }
- }
- }
-
- public void NavigatorServer(List<MachineObj> listServerItem)
- {
- for (int i = 0; i < listRoot.Count; i++)
- {
- listRoot[i].transform.parent.gameObject.SetActive(false);
- }
- listRoot.Clear();
-
- float leftDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(leftLine.localPosition.x, 0, leftLine.localPosition.z));
- float rightDis = Vector3.Distance(new Vector3(player.localPosition.x, -1, player.localPosition.z), new Vector3(rightLine.localPosition.x, 0, rightLine.localPosition.z));
- Vector3 pos1 = new Vector3(player.localPosition.x, -1, player.localPosition.z);
- Vector3 pos2;
- if(Mathf.Abs( leftDis)<Mathf.Abs( rightDis))
- {
- pos2 = new Vector3(leftLine.localPosition.x, -1, player.localPosition.z);
- }
- else
- {
- pos2 = new Vector3(rightLine.localPosition.x, -1, player.localPosition.z);
- }
- List<Vector3> listpos = new List<Vector3>();
- listpos.Add(pos1);
- listpos.Add(pos2);
- listpos.Add(new Vector3(rightLine.localPosition.x, -1, player.localPosition.z + 10000));
- CreateRoute(listpos,null,false); // 依据Player 生成两个固定点 当前Player点 和 出口点
- List<Vector3> listentrance = new List<Vector3>();
- for (int i = 0; i < listServerItem.Count; i++)
- {
- listpos.Clear();
- listpos.Add(new Vector3(listServerItem[i].transform.localPosition.x, -1, listServerItem[i].transform.localPosition.z + ((listServerItem[i].isSingle ? 1 : -1) * 1)));
- listpos.Add(listServerItem[i].transform.localPosition);
- CreateRoute(listpos,listServerItem[i],true); // 根据机位生成 机位点
- 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));
- bool finish = true;
- for (int j = 0; j < listentrance.Count; j++)
- {
- if(entrance == listentrance[i])
- {
- finish = false;
- break;
- }
- }
- if(finish)
- {
- if (Mathf.Abs(player.localPosition.z - listServerItem[i].transform.localPosition.z) < (MachineRoomManager.Instance.data.intervalL / 2.0f + MachineRoomManager.Instance.data.cabinetW / 2.0f)) // 我和当前导览的位置在同一行
- {
- }
- else
- {
- List<Vector3> listentrancePos = new List<Vector3>();
- listentrancePos.Add(entrance);
- listentrancePos.Add(new Vector3(listServerItem[i].transform.localPosition.x, -1, listServerItem[i].transform.localPosition.z + ((listServerItem[i].isSingle ? 1 : -1) * 1)));
- CreateRoute(listentrancePos, listServerItem[i], false); // 生成当前机位的 入口点
- }
-
- }
- }
- }
- private void CreateRoute(List<Vector3> listPos, MachineObj machineObj, bool isdevice)
- {
- //for (int i = 0; i < listPos.Count - 1; i++)
- //{
- // GameObject route1 = GameObject.Instantiate(route, MachineRoomManager.Instance.transform);
- // route1.transform.localPosition = listPos[i];
- // lookPoint.localPosition = listPos[i + 1];
- // route1.transform.LookAt(lookPoint);
- // route1.SetActive(true);
- // listRoot.Add(route1.transform.GetChild(0).GetComponent<Route>());
- // listRoot[i].SetRoute(route1.transform.position, lookPoint.position, 0,lookPoint);
- // GameObject obj = new GameObject(i.ToString());
- // obj.transform.position = listPos[i];
- //}
-
- if (isdevice && machineObj)
- {
- lookPoint.localPosition = listPos[listPos.Count - 1];
- GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
- devicedoorName.transform.localPosition = listPos[listPos.Count - 2];
- devicedoorName.transform.LookAt(lookPoint);
- devicedoorName.transform.localPosition = machineObj.transform.localPosition + new Vector3(0, 0, 0);
- listDeviceDoorName.Add(devicedoorName);
- devicedoorName.GetComponent<SettingDeviceName>().SettingName(machineObj.deviceParameters.deviceName);
- }
- }
- private void CreateRoute(List<Vector3> listPos, bool isDevice)
- {
- for (int i = 0; i < listPos.Count - 1; i++)
- {
- GameObject route1 = GameObject.Instantiate(route, MachineRoomManager.Instance.transform);
- route1.transform.localPosition = listPos[i];
- lookPoint.localPosition = listPos[i + 1];
- route1.transform.LookAt(lookPoint);
- route1.SetActive(true);
- listRoot.Add(route1.transform.GetChild(0).GetComponent<Route>());
- }
- if (isDevice)
- {
- GameObject devicedoorName = GameObject.Instantiate(deviceName, MachineRoomManager.Instance.transform);
- devicedoorName.transform.localPosition = listPos[listPos.Count - 2];
- lookPoint.localPosition += new Vector3(0, 0, 10);
- devicedoorName.transform.LookAt(lookPoint);
- listDeviceDoorName.Add(devicedoorName);
- }
- }
- public void CloseWayFind()
- {
- for (int i = 0; i < listRoot.Count; i++)
- {
- listRoot[i].transform.parent.gameObject.SetActive(false);
- }
- listRoot.Clear();
- Bezier.Instance.RouteClear();
- for(int i = 0;i<listDeviceDoorName.Count;i++)
- {
- listDeviceDoorName[i].gameObject.SetActive(false);
- }
- listDeviceDoorName.Clear();
- }
- private IEnumerator SetHighRoute()
- {
- while(true)
- {
- yield return new WaitForSeconds(0.2f);
- if(listRoot!=null&& listRoot.Count>1)
- {
- Route route = null;
- route = listRoot[0];
- for (int i = 1; i < listRoot.Count; i++)
- {
- // listRoot[i].setHighLight(true);
- //if (Mathf.Abs(Vector3.Distance(player.transform.position, listRoot[i].transform.position)) < Mathf.Abs(Vector3.Distance(player.transform.position, route.transform.position)))
- //{
-
- // route.setHighLight(false);
- // route = listRoot[i];
- //}
- }
- // route.setHighLight(true);
- }
- }
- }
- }
|