using GPCanvas.Runtime.BehaviourTree.Tasks.Actions.Sleep; using MoSQ; using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical; using Pathfinding; using Pathfinding.RVO; using System.Collections; using System.Collections.Generic; using UnityEditor.Build.Pipeline; using UnityEngine; public class AStarPathFindComponent : MonoBehaviour { private bool bUseFollowerEntity = false; private AstarPath astarPath; private RecastGraph graph; private Dictionary entities = new Dictionary(); private Dictionary destinationSetters = new Dictionary(); private Dictionary agents = new Dictionary(); void Start() { astarPath = AstarPath.active; if (astarPath != null) { graph = astarPath.graphs[0] as RecastGraph; } } public void ReCheckMapPathFind() { if (graph == null) { return; } graph.SnapBoundsToScene(); astarPath.Scan(); } public void ClearNpc(int id) { if (bUseFollowerEntity) { if (entities.ContainsKey(id)) { entities.Remove(id); } if (destinationSetters.ContainsKey(id)) { destinationSetters.Remove(id); } } else { if (agents.ContainsKey(id)) { agents.Remove(id); } } } public void AddNpc(int id, Transform tra, float radius, float height, bool enableLocalAvoidance, float stopDistance) { if (bUseFollowerEntity) { if (!entities.ContainsKey(id)) { var fentity = tra.GetOrAddComponent(); entities.Add(id, fentity); fentity.radius = radius; fentity.height = height; fentity.enableLocalAvoidance = enableLocalAvoidance; var setters = tra.GetOrAddComponent(); destinationSetters.Add(id, setters); } } else { if (!agents.ContainsKey(id)) { var agent = tra.GetOrAddComponent(); agents.Add(id, agent); } } } //直接移动到一个固定的坐标 public void MoveNpcTo(int id, float x, float y, float z, float speed) { if (bUseFollowerEntity) { if (entities.TryGetValue(id, out var fentity)) { fentity.maxSpeed = speed; fentity.destination = new Vector3(x, y, z); } } else { if (agents.TryGetValue(id, out var agent)) { agent.maxSpeed = speed; agent.SetTarget(new Vector3(x, y, z)); } } } //跟随一个可移动的目标 public void SetNpcTargetIdTo(int npcId, int targetNpcId) { if (bUseFollowerEntity) { if (entities.TryGetValue(npcId, out var fentity) && entities.TryGetValue(targetNpcId, out var targetEntity)) { fentity.SetDestination(targetEntity.position); } } } public Vector3 GetNpcPosition(int id) { if (bUseFollowerEntity) { if (entities.TryGetValue(id, out var fentity)) { return fentity.position; } } else { if (agents.TryGetValue(id, out var agent)) { return agent.GetPosition(); } } return Vector3.zero; } public bool IsMoveFinish(int id) { if (bUseFollowerEntity) { if (entities.TryGetValue(id, out var fentity)) { return fentity.reachedEndOfPath || fentity.reachedDestination /*|| Vector3.Distance(fentity.position, fentity.endOfPath) < fentity.stopDistance*/; } } else { if (agents.TryGetValue(id, out var agent)) { return agent.IsStopped(); } } return true; } }