227 lines
6.3 KiB
C#
227 lines
6.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
using System.Collections;
|
|
using GCGame.Table;
|
|
|
|
public class SignPostObjInfo
|
|
{
|
|
private GameObject _SignPostObj = null;
|
|
private SignPostObjInfo _Next;
|
|
|
|
public GameObject SignPostObj
|
|
{
|
|
get { return _SignPostObj; }
|
|
}
|
|
|
|
public void ClearUp()
|
|
{
|
|
if (_SignPostObj != null)
|
|
{
|
|
_SignPostObj.SetActive(false);
|
|
GameObject.Destroy(_SignPostObj);
|
|
_SignPostObj = null;
|
|
}
|
|
if (_Next != null)
|
|
_Next.ClearUp();
|
|
}
|
|
|
|
public void LookAt(SignPostObjInfo other)
|
|
{
|
|
if (_SignPostObj != null && other != null && other.SignPostObj != null)
|
|
_SignPostObj.transform.LookAt(other.SignPostObj.transform);
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
if (_SignPostObj != null)
|
|
_SignPostObj.SetActive(false);
|
|
if (_Next != null)
|
|
_Next.Hide();
|
|
}
|
|
|
|
public SignPostObjInfo SetPosiotn(Vector3 NewPos)
|
|
{
|
|
if (_SignPostObj == null)
|
|
{
|
|
_SignPostObj = Singleton<SignPostManager>.Instance.Instantiate();
|
|
}
|
|
if (_SignPostObj != null)
|
|
{
|
|
_SignPostObj.SetActive(true);
|
|
_SignPostObj.transform.position = Games.Scene.ActiveScene.GetTerrainPosition(NewPos);
|
|
if (_Next == null)
|
|
_Next = new SignPostObjInfo();
|
|
return _Next;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public class SignPost
|
|
{
|
|
private SignPost _Next; //路径的下一个点
|
|
private SignPost _Last; //路径的前一个点
|
|
private Vector3 _Position = Vector3.zero;
|
|
private int _SignPostTabID = -1;
|
|
private SignPostObjInfo _SignInfoRoot = null;
|
|
|
|
public SignPost(int TabID)
|
|
{
|
|
_SignPostTabID = -1;
|
|
Tab_SignPost sign = TableManager.GetSignPostByID(TabID, 0);
|
|
if (sign == null || sign.SceneID != GameManager.gameManager.RunningScene)
|
|
return;
|
|
_SignPostTabID = TabID;
|
|
}
|
|
|
|
public int SignPostID
|
|
{
|
|
get { return _SignPostTabID; }
|
|
}
|
|
|
|
public Vector3 Position
|
|
{
|
|
get { return _Position; }
|
|
}
|
|
|
|
public bool CreateSign(SignPost Last,Vector3 TargetDestant)
|
|
{
|
|
Tab_SignPost Signtab = TableManager.GetSignPostByID(_SignPostTabID, 0);
|
|
if (Signtab == null)
|
|
return false;
|
|
_Last = Last;
|
|
_Position.x = Signtab.PosX;
|
|
_Position.z = Signtab.PosZ;
|
|
|
|
if (Games.Scene.ActiveScene.GetTerrainPosition(ref _Position) == false)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (Signtab.Next!=-1)
|
|
{
|
|
SignPost point = new SignPost(Signtab.Next);
|
|
if (point.CreateSign(this, TargetDestant))
|
|
_Next = point;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void ShowSign(bool needShow = false)
|
|
{
|
|
if(needShow == true)
|
|
{
|
|
if(_Last!=null)
|
|
{
|
|
if (_SignInfoRoot == null)
|
|
_SignInfoRoot = new SignPostObjInfo();
|
|
if (ShowSign(_Last.Position, Position, _SignInfoRoot) && _Next!=null)
|
|
{
|
|
_Next.ShowSign(true);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Vector3 SelfPos = Position;
|
|
SelfPos.y = 0;
|
|
Vector3 DstPos = SignPostManager.Instance.GhattyCurrDestent;
|
|
DstPos.y = 0;
|
|
float dis = Vector3.Distance(SelfPos, DstPos);
|
|
if (dis < 5)
|
|
{
|
|
if (_SignInfoRoot == null)
|
|
_SignInfoRoot = new SignPostObjInfo();
|
|
if (ShowSign(SignPostManager.Instance.GhattyCurrPosition, Position, _SignInfoRoot) && _Next!=null)
|
|
{
|
|
if (CrossServerMap.Instance != null && Singleton<SignPostManager>.Instance.GhattyCurrPathID!=-1)
|
|
{
|
|
CrossServerMap.Instance.EndSet();
|
|
CrossServerMap.Instance.StartSet();
|
|
}
|
|
|
|
_Next.ShowSign(true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (_SignInfoRoot != null)
|
|
_SignInfoRoot.ClearUp();
|
|
if (_Next != null)
|
|
_Next.ShowSign();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private bool ShowSign(Vector3 Start,Vector3 End, SignPostObjInfo Current)
|
|
{
|
|
UnityEngine.AI.NavMeshPath path = new NavMeshPath();
|
|
if (UnityEngine.AI.NavMesh.CalculatePath(Start, End, -1, path) == false)
|
|
return false;
|
|
return ShowSign(path.corners, Current);
|
|
}
|
|
|
|
private bool ShowSign(Vector3[] corners, SignPostObjInfo Current)
|
|
{
|
|
int CountPoint = 0;
|
|
var distance = 0f;
|
|
Vector3 lastPos = Vector3.zero;
|
|
Vector3 Pos = Vector3.zero;
|
|
SignPostObjInfo Last = null;
|
|
for (int i = 0; i < corners.Length; i++)
|
|
{
|
|
if (i + 1 >= corners.Length)
|
|
break;
|
|
var start = corners[i];
|
|
var end = corners[i + 1];
|
|
var length = Vector3.Distance(start, end);
|
|
while (distance < length)
|
|
{
|
|
// 最后一个点把长度保留到下一段路径
|
|
Pos = Vector3.Lerp(start, end, distance / length);
|
|
Pos = Games.Scene.ActiveScene.GetTerrainPosition(Pos);
|
|
|
|
if (CrossServerMap.Instance != null && Singleton<SignPostManager>.Instance.GhattyCurrPathID != -1)
|
|
CrossServerMap.Instance.GharryPath(Pos);
|
|
|
|
SignPostObjInfo Next = Current.SetPosiotn(Pos);
|
|
if (Next == null)
|
|
{
|
|
return false;
|
|
}
|
|
if (Last != null)
|
|
Last.LookAt(Current);
|
|
Last = Current;
|
|
Current = Next;
|
|
distance = distance + 3;
|
|
CountPoint++;
|
|
}
|
|
distance -= length;
|
|
}
|
|
if (Last != null)
|
|
Last.Hide();
|
|
return true;
|
|
}
|
|
public void ClearUp()
|
|
{
|
|
if (_SignInfoRoot != null)
|
|
_SignInfoRoot.ClearUp();
|
|
|
|
if (_Next != null)
|
|
_Next.ClearUp();
|
|
|
|
_Next = null;
|
|
_Last = null;
|
|
_Position = Vector3.zero;
|
|
_SignPostTabID = -1;
|
|
_SignInfoRoot = null;
|
|
|
|
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
|
|
}
|
|
} |