上海虹口龙之梦项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
7.0 KiB

using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PathFinding : MonoBehaviour
{
public Transform PathParent;
public List<Transform> PathList = new List<Transform>();
public int NowIndex = 0;
public int NowFloor = 4;
// Start is called before the first frame update
void Start()
{
foreach (Transform item in PathParent)
{
if (item != PathParent)
PathList.Add(item);
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
List<LTData> alld = new List<LTData>();
LTData data = new LTData();
Vector3 p1 = new Vector3(-17.47f, -17.5f, 41.83f);//1.7
Vector3 p2 = new Vector3(-17.47f, -15.9f, 37.48f);//1.4
Vector3 p3 = new Vector3(-15.48f, -15.9f, 37.48f);//1.4
Vector3 p4 = new Vector3(-15.48f, -14.3f, 41.83f);//1.7
data.FloorId = 4;
data.LTName = "LT65";
data.LTTopCode = "1";
data.LTMidCode = "2";
data.LTBottomCode = "3";
data.PTop = p1;
data.PTop_Half = p2;
data.PHalf_Bottom = p3;
data.PBottom = p4;
data.PWidthRange = 0.8f;
data.PHalfWidthRange = 0.4f;
data.IsTop_Bottom = false;
alld.Add(data);
alld.Add(data);
string adstr = JsonConvert.SerializeObject(alld);
Debug.Log(adstr);
}
if (Input.GetKeyDown(KeyCode.T))
{
LTData ltdata = null;
foreach (var item in Config.LTPostions)
{
if (item.FloorId == NowFloor)
{
ltdata = item;
break;
}
}
if (NowIndex == 0)
{
LocationData data = new LocationData();
data.targetNo = 102;
data.xAxis = PathList[0].transform.position.x;
data.yAxis = PathList[0].transform.position.y;
data.zAxis = PathList[0].transform.position.z;
data.stations = PathList[0].name;
var agent = GetComponent<LocationSimulator>().CreateAgent(data);
agent.enabled = false;
agent.GetComponent<NavMeshAgent>().enabled = false;
GetComponent<LocationSimulator>().agentControllers.Add(data.targetNo, agent);
Vector3 pos = ConversionTransform(data, ltdata);
GetComponent<LocationSimulator>().agentControllers[data.targetNo].transform.position = pos;
}
else if (NowIndex < PathList.Count)
{
LocationData data = new LocationData();
data.targetNo = 102;
data.stations = PathList[NowIndex].name;
data.xAxis = PathList[NowIndex].transform.position.x;
data.yAxis = PathList[NowIndex].transform.position.y;
data.zAxis = PathList[NowIndex].transform.position.z;
Vector3 pos = ConversionTransform(data, ltdata);
GetComponent<LocationSimulator>().agentControllers[data.targetNo].transform.position = pos;
}
else
{
NowIndex = 0;
}
NowIndex++;
}
}
/// <summary>
/// 根据数据转换到Unity坐标
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public Vector3 ConversionTransform(LocationData data, LTData ltData)
{
if (ltData == null)
return Vector3.zero;
//取最高处作为pos.y方便向下打射线
float y = ltData.PTop.y > ltData.PBottom.y ? ltData.PTop.y : ltData.PBottom.y;
//对方传入转换后的数据
var pos = new Vector3((float)data.xAxis, y, (float)data.zAxis);
if (data.stations.Contains(ltData.LTBottomCode) && !data.stations.Contains(ltData.LTMidCode))
{ //下面地面
pos.y = ltData.PBottom.y;
}
else if (data.stations.Contains(ltData.LTBottomCode) && data.stations.Contains(ltData.LTMidCode))
{ //楼梯下半段
if (pos.x < ltData.PBottom.x - ltData.PWidthRange || pos.x > ltData.PBottom.x + ltData.PWidthRange)
{
pos.x = ltData.PBottom.x;
}
if (pos.z > ltData.PBottom.z)
{
pos.z = ltData.PBottom.z;
}
if (pos.z < ltData.PHalf_Bottom.z)
{
pos.z = ltData.PHalf_Bottom.z;
}
pos.y = SetY(pos, ltData.LTName);
if (pos.y == 0)
pos.y = ltData.PBottom.y;
}
else if (data.stations.Contains(ltData.LTTopCode) && data.stations.Contains(ltData.LTMidCode))
{ //楼梯上半段
if (pos.x < ltData.PTop.x - ltData.PWidthRange || pos.x > ltData.PTop.x + ltData.PWidthRange)
{
pos.x = ltData.PTop.x;
}
if (pos.z > ltData.PTop.z)
{
pos.z = ltData.PTop.z;
}
if (pos.z < ltData.PTop_Half.z)
{
pos.z = ltData.PTop_Half.z;
}
pos.y = SetY(pos, ltData.LTName);
if (pos.y == 0)
pos.y = ltData.PTop.y;
}
else if (data.stations.Contains(ltData.LTMidCode) &&
!data.stations.Contains(ltData.LTTopCode) &&
!data.stations.Contains(ltData.LTBottomCode))
{
//楼梯间过度
if (pos.z < ltData.PTop_Half.z - ltData.PHalfWidthRange || pos.z > ltData.PTop_Half.z + ltData.PHalfWidthRange)
{
pos.z = ltData.PTop_Half.z;
}
if (pos.x > ltData.PTop_Half.x)
{
pos.x = ltData.PTop_Half.x;
}
if (pos.x < ltData.PHalf_Bottom.x)
{
pos.x = ltData.PHalf_Bottom.x;
}
pos.y = ltData.PTop_Half.y;
}
else if (data.stations.Contains(ltData.LTTopCode) && !data.stations.Contains(ltData.LTMidCode))
{//上面地面
pos.y = ltData.PTop.y;
}
else
{//其他基站则在正常高度
if (ltData.IsTop_Bottom)
pos.y = ltData.PTop.y;
else
pos.y = ltData.PBottom.y;
}
return pos;
}
/// <summary>
/// 确保角色始终在地面上
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public float SetY(Vector3 position, string ltname)
{
var hitlist = Physics.RaycastAll(position, Vector3.down, 100f, ~(1 << (LayerMask.NameToLayer("Defaut") | LayerMask.NameToLayer("Hidden"))));
if (hitlist.Length > 0)
{
foreach (var item in hitlist)
{
if (item.transform.name == ltname)
return item.point.y;
}
}
return 0f;
}
}