Files
JJBB/Assets/Project/Script/Player/AutoSearch/AutoSearchPath.cs
2024-08-23 15:49:34 +08:00

109 lines
3.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************************************************************
* 文件名: AutoSearchPath.cs
* 全路径: \Script\Player\AutoSearch\AutoSearchPath.cs
* 创建人: 李嘉
* 创建时间2014-01-02
*
* 功能说明:自动寻路路径类,只保存数据,而不进行逻辑控制
* 保存了路径中所有的点在List中
* 保存了自动寻路结束后的回调事件和目标名称
* 索引可能会重复,但是路径一定按照插入的顺序排列
* 所以当完成一个点的时候可以直接将List中的头元素删除
* 修改记录:
*********************************************************************************/
using System.Collections.Generic;
using Games.Events;
using UnityEngine;
public class AutoSearchPath
{
public delegate void FinishCallBack(object param);
public const float stopRange = 1f;
public const float toNpcStopRange = 2f;
public float autoSearchRadius;
public object callBackParam;
public FinishCallBack finishCallBack;
//自动寻路目标ServerID
//自动寻路目标名字
//自动寻路结束事件
//当到达最后一个点之后触发
public AutoSearchPath()
{
AutoSearchPosCache = new List<AutoSearchPoint>();
}
//自动寻路点缓存
public List<AutoSearchPoint> AutoSearchPosCache { get; private set; }
public string AutoSearchTargetName { get; set; }
public int AutoSearchTargetID { get; set; }
public int AutoSearchTargetRoleID { get; set; } //寻路的中途发现更近的NPC可以切换到更近的这个
public GameEvent FinishCallBackEvent { get; set; }
public void AddPathPoint(AutoSearchPoint point)
{
AutoSearchPosCache.Add(point);
}
public void ResetPath()
{
AutoSearchPosCache.Clear();
if (null != FinishCallBackEvent)
FinishCallBackEvent.Reset();
AutoSearchTargetName = string.Empty;
autoSearchRadius = 0.0f;
finishCallBack = null;
callBackParam = null;
AutoSearchTargetRoleID = -1;
AutoSearchTargetID = -1;
}
//找到场景sceneId中第一个需要达到的点
public AutoSearchPoint GetPathPosition(int sceneId)
{
if (GameManager.gameManager.ActiveScene.IsCopyScene() //旧场景是副本(当前场景)
&& !GameManager.gameManager.ActiveScene.IsCopyScene(sceneId)) //新场景(将要切换的场景)不是副本
return new AutoSearchPoint(); //不返回空,需要自动寻路
for (var i = 0; i < AutoSearchPosCache.Count; ++i)
if (AutoSearchPosCache[i].SceneID == sceneId)
return AutoSearchPosCache[i];
//Debug.LogError("Return null");
return null;
}
//是否已经抵达路径点
public bool IsFinish(Vector3 pos)
{
return AutoSearchPosCache.Count < 2 && IsReachPoint(pos);
}
//是否到达寻路路径中的下一个目标点
//按照设计就是当前索引为0的点
public bool IsReachPoint(Vector3 pos)
{
if (AutoSearchPosCache.Count <= 0)
return false;
//先判断场景ID
if (AutoSearchPosCache[0].SceneID == GameManager.gameManager.RunningScene)
{
var destDistance = string.IsNullOrEmpty(AutoSearchTargetName) ? stopRange : toNpcStopRange;
destDistance = Mathf.Max(destDistance, autoSearchRadius);
var destPos = new Vector2(AutoSearchPosCache[0].PosX, AutoSearchPosCache[0].PosZ);
if ((destPos - pos.RemoveY()).sqrMagnitude < destDistance.ToSquare())
return true;
}
return false;
}
}