using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 寻路管理
///
public class WayFindingManager : MonoSingleton
{
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 listRoot;
private List listDeviceDoorName;
private void Start()
{
listRoot = new List();
listDeviceDoorName = new List();
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;
}
public void NavigatorServer(MachineObj serverItem)
{
// if(listRoot!=null)
for (int i = 0; i < listRoot.Count; i++)
{
listRoot[i].gameObject.SetActive(false);
}
listRoot.Clear();
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));
List listPos = new List();
listPos.Add(pos1);
listPos.Add(pos2);
listPos.Add(new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z));
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));
List listpos = new List();
listpos.Add(pos1);
listpos.Add(pos2);
listpos.Add(pos3);
listpos.Add(pos4);
listpos.Add(new Vector3(serverItem.transform.localPosition.x,-1, serverItem.transform.localPosition.z));
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));
List listpos = new List();
listpos.Add(pos1);
listpos.Add(pos2);
listpos.Add(pos3);
listpos.Add(pos4);
listpos.Add(new Vector3(serverItem.transform.localPosition.x, -1, serverItem.transform.localPosition.z));
Bezier.Instance.SetRoute(listpos);
CreateRoute(listpos, serverItem, true);
}
}
}
public void NavigatorServer(List 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) listpos = new List();
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 listentrance = new List();
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 listentrancePos = new List();
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 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());
// 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().SettingName(machineObj.deviceParameters.deviceName);
}
}
private void CreateRoute(List 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());
}
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();
for(int i = 0;i1)
{
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);
}
}
}
}