123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.Profiling;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
-
-
-
- [AddComponentMenu("Pathfinding/Seeker")]
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_seeker.php")]
- public class Seeker : VersionedMonoBehaviour {
-
-
-
-
-
-
- public bool drawGizmos = true;
-
-
-
-
-
-
-
-
-
-
-
- public bool detailedGizmos;
-
- [HideInInspector]
- public StartEndModifier startEndModifier = new StartEndModifier();
-
-
-
-
-
-
- [HideInInspector]
- public int traversableTags = -1;
-
-
-
-
-
-
-
-
-
- [HideInInspector]
- public int[] tagPenalties = new int[32];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [HideInInspector]
- public GraphMask graphMask = GraphMask.everything;
-
- [UnityEngine.Serialization.FormerlySerializedAs("graphMask")]
- int graphMaskCompatibility = -1;
-
-
-
-
-
- public OnPathDelegate pathCallback;
-
- public OnPathDelegate preProcessPath;
-
- public OnPathDelegate postProcessPath;
-
- [System.NonSerialized]
- List<Vector3> lastCompletedVectorPath;
-
- [System.NonSerialized]
- List<GraphNode> lastCompletedNodePath;
-
- [System.NonSerialized]
- protected Path path;
-
- [System.NonSerialized]
- private Path prevPath;
-
- private readonly OnPathDelegate onPathDelegate;
-
- private readonly OnPathDelegate onPartialPathDelegate;
-
- private OnPathDelegate tmpPathCallback;
-
- protected uint lastPathID;
-
- readonly List<IPathModifier> modifiers = new List<IPathModifier>();
- public enum ModifierPass {
- PreProcess,
-
- PostProcess = 2,
- }
- public Seeker () {
- onPathDelegate = OnPathComplete;
- onPartialPathDelegate = OnPartialPathComplete;
- }
-
- protected override void Awake () {
- base.Awake();
- startEndModifier.Awake(this);
- }
-
-
-
-
-
-
- public Path GetCurrentPath () {
- return path;
- }
-
-
-
-
-
-
-
-
-
-
- public void CancelCurrentPathRequest (bool pool = true) {
- if (!IsDone()) {
- path.FailWithError("Canceled by script (Seeker.CancelCurrentPathRequest)");
- if (pool) {
-
-
-
-
- path.Claim(path);
- path.Release(path);
- }
- }
- }
-
-
-
-
-
-
-
-
- public void OnDestroy () {
- ReleaseClaimedPath();
- startEndModifier.OnDestroy(this);
- }
-
-
-
-
-
-
-
-
-
-
- void ReleaseClaimedPath () {
- if (prevPath != null) {
- prevPath.Release(this, true);
- prevPath = null;
- }
- }
-
- public void RegisterModifier (IPathModifier modifier) {
- modifiers.Add(modifier);
-
- modifiers.Sort((a, b) => a.Order.CompareTo(b.Order));
- }
-
- public void DeregisterModifier (IPathModifier modifier) {
- modifiers.Remove(modifier);
- }
-
-
-
-
-
-
-
- public void PostProcess (Path path) {
- RunModifiers(ModifierPass.PostProcess, path);
- }
-
- public void RunModifiers (ModifierPass pass, Path path) {
- if (pass == ModifierPass.PreProcess) {
- if (preProcessPath != null) preProcessPath(path);
- for (int i = 0; i < modifiers.Count; i++) modifiers[i].PreProcess(path);
- } else if (pass == ModifierPass.PostProcess) {
- Profiler.BeginSample("Running Path Modifiers");
-
- if (postProcessPath != null) postProcessPath(path);
-
- for (int i = 0; i < modifiers.Count; i++) modifiers[i].Apply(path);
- Profiler.EndSample();
- }
- }
-
-
-
-
-
-
-
-
-
-
- public bool IsDone () {
- return path == null || path.PipelineState >= PathState.Returned;
- }
-
-
-
-
-
- void OnPathComplete (Path path) {
- OnPathComplete(path, true, true);
- }
-
-
-
-
- void OnPathComplete (Path p, bool runModifiers, bool sendCallbacks) {
- if (p != null && p != path && sendCallbacks) {
- return;
- }
- if (this == null || p == null || p != path)
- return;
- if (!path.error && runModifiers) {
-
- RunModifiers(ModifierPass.PostProcess, path);
- }
- if (sendCallbacks) {
- p.Claim(this);
- lastCompletedNodePath = p.path;
- lastCompletedVectorPath = p.vectorPath;
-
- if (tmpPathCallback != null) {
- tmpPathCallback(p);
- }
-
- if (pathCallback != null) {
- pathCallback(p);
- }
-
-
-
-
-
-
-
- if (prevPath != null) {
- prevPath.Release(this, true);
- }
- prevPath = p;
- }
- }
-
-
-
-
- void OnPartialPathComplete (Path p) {
- OnPathComplete(p, true, false);
- }
-
- void OnMultiPathComplete (Path p) {
- OnPathComplete(p, false, true);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- [System.Obsolete("Use ABPath.Construct(start, end, null) instead")]
- public ABPath GetNewPath (Vector3 start, Vector3 end) {
-
- return ABPath.Construct(start, end, null);
- }
-
-
-
-
-
-
- public Path StartPath (Vector3 start, Vector3 end) {
- return StartPath(start, end, null);
- }
-
-
-
-
-
-
-
-
-
- public Path StartPath (Vector3 start, Vector3 end, OnPathDelegate callback) {
- return StartPath(ABPath.Construct(start, end, null), callback);
- }
-
-
-
-
-
-
-
-
-
-
- public Path StartPath (Vector3 start, Vector3 end, OnPathDelegate callback, GraphMask graphMask) {
- return StartPath(ABPath.Construct(start, end, null), callback, graphMask);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Path StartPath (Path p, OnPathDelegate callback = null) {
-
-
-
-
-
- if (p.nnConstraint.graphMask == -1) p.nnConstraint.graphMask = graphMask;
- StartPathInternal(p, callback);
- return p;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public Path StartPath (Path p, OnPathDelegate callback, GraphMask graphMask) {
- p.nnConstraint.graphMask = graphMask;
- StartPathInternal(p, callback);
- return p;
- }
-
- void StartPathInternal (Path p, OnPathDelegate callback) {
- var mtp = p as MultiTargetPath;
- if (mtp != null) {
-
- var callbacks = new OnPathDelegate[mtp.targetPoints.Length];
- for (int i = 0; i < callbacks.Length; i++) {
- callbacks[i] = onPartialPathDelegate;
- }
- mtp.callbacks = callbacks;
- p.callback += OnMultiPathComplete;
- } else {
- p.callback += onPathDelegate;
- }
- p.enabledTags = traversableTags;
- p.tagPenalties = tagPenalties;
-
- if (path != null && path.PipelineState <= PathState.Processing && path.CompleteState != PathCompleteState.Error && lastPathID == path.pathID) {
- path.FailWithError("Canceled path because a new one was requested.\n"+
- "This happens when a new path is requested from the seeker when one was already being calculated.\n" +
- "For example if a unit got a new order, you might request a new path directly instead of waiting for the now" +
- " invalid path to be calculated. Which is probably what you want.\n" +
- "If you are getting this a lot, you might want to consider how you are scheduling path requests.");
-
- }
-
- path = p;
- tmpPathCallback = callback;
-
- lastPathID = path.pathID;
-
- RunModifiers(ModifierPass.PreProcess, path);
-
- AstarPath.StartPath(path);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public MultiTargetPath StartMultiTargetPath (Vector3 start, Vector3[] endPoints, bool pathsForAll, OnPathDelegate callback = null, int graphMask = -1) {
- MultiTargetPath p = MultiTargetPath.Construct(start, endPoints, null, null);
- p.pathsForAll = pathsForAll;
- StartPath(p, callback, graphMask);
- return p;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public MultiTargetPath StartMultiTargetPath (Vector3[] startPoints, Vector3 end, bool pathsForAll, OnPathDelegate callback = null, int graphMask = -1) {
- MultiTargetPath p = MultiTargetPath.Construct(startPoints, end, null, null);
- p.pathsForAll = pathsForAll;
- StartPath(p, callback, graphMask);
- return p;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [System.Obsolete("You can use StartPath instead of this method now. It will behave identically.")]
- public MultiTargetPath StartMultiTargetPath (MultiTargetPath p, OnPathDelegate callback = null, int graphMask = -1) {
- StartPath(p, callback, graphMask);
- return p;
- }
-
- public void OnDrawGizmos () {
- if (lastCompletedNodePath == null || !drawGizmos) {
- return;
- }
- if (detailedGizmos) {
- Gizmos.color = new Color(0.7F, 0.5F, 0.1F, 0.5F);
- if (lastCompletedNodePath != null) {
- for (int i = 0; i < lastCompletedNodePath.Count-1; i++) {
- Gizmos.DrawLine((Vector3)lastCompletedNodePath[i].position, (Vector3)lastCompletedNodePath[i+1].position);
- }
- }
- }
- Gizmos.color = new Color(0, 1F, 0, 1F);
- if (lastCompletedVectorPath != null) {
- for (int i = 0; i < lastCompletedVectorPath.Count-1; i++) {
- Gizmos.DrawLine(lastCompletedVectorPath[i], lastCompletedVectorPath[i+1]);
- }
- }
- }
- protected override int OnUpgradeSerializedData (int version, bool unityThread) {
- if (graphMaskCompatibility != -1) {
- Debug.Log("Loaded " + graphMaskCompatibility + " " + graphMask.value);
- graphMask = graphMaskCompatibility;
- graphMaskCompatibility = -1;
- }
- return base.OnUpgradeSerializedData(version, unityThread);
- }
- }
- }
|