diff options
author | chai <215380520@qq.com> | 2024-05-23 10:08:29 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-05-23 10:08:29 +0800 |
commit | 8722a9920c1f6119bf6e769cba270e63097f8e25 (patch) | |
tree | 2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/TurnBased/SingleNodeBlocker.cs | |
parent | 3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff) |
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/TurnBased/SingleNodeBlocker.cs')
-rw-r--r-- | Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/TurnBased/SingleNodeBlocker.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/TurnBased/SingleNodeBlocker.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/TurnBased/SingleNodeBlocker.cs new file mode 100644 index 0000000..496de8e --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/TurnBased/SingleNodeBlocker.cs @@ -0,0 +1,70 @@ +using UnityEngine; + +namespace Pathfinding { + /// <summary> + /// Blocks single nodes in a graph. + /// + /// This is useful in turn based games where you want + /// units to avoid all other units while pathfinding + /// but not be blocked by itself. + /// + /// Note: To use this with a movement script, you have to assign the BlockManager's traversal provider to either <see cref="Seeker.traversalProvider"/> or <see cref="FollowerEntity.pathfindingSettings.traversalProvider"/>. + /// + /// See: TurnBasedAI for example usage + /// + /// See: BlockManager + /// See: turnbased (view in online documentation for working links) + /// See: traversal_provider (view in online documentation for working links) + /// </summary> + [HelpURL("https://arongranberg.com/astar/documentation/stable/singlenodeblocker.html")] + public class SingleNodeBlocker : VersionedMonoBehaviour { + public GraphNode lastBlocked { get; private set; } + public BlockManager manager; + + /// <summary> + /// Block node closest to the position of this object. + /// + /// Will unblock the last node that was reserved (if any) + /// </summary> + public void BlockAtCurrentPosition () { + BlockAt(transform.position); + } + + /// <summary> + /// Block node closest to the specified position. + /// + /// Will unblock the last node that was reserved (if any) + /// </summary> + public void BlockAt (Vector3 position) { + Unblock(); + var node = AstarPath.active.GetNearest(position, NNConstraint.None).node; + if (node != null) { + Block(node); + } + } + + /// <summary> + /// Block specified node. + /// + /// Will unblock the last node that was reserved (if any) + /// </summary> + public void Block (GraphNode node) { + if (node == null) + throw new System.ArgumentNullException("node"); + + manager.InternalBlock(node, this); + lastBlocked = node; + } + + /// <summary>Unblock the last node that was blocked (if any)</summary> + public void Unblock () { + if (lastBlocked == null || lastBlocked.Destroyed) { + lastBlocked = null; + return; + } + + manager.InternalUnblock(lastBlocked, this); + lastBlocked = null; + } + } +} |