summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/TurnBased/SingleNodeBlocker.cs
blob: 496de8e39b07c6efd59a6f2c80ca4dfb5eea831d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
		}
	}
}