summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/HexagonalTurnBased/TurnBasedDoor.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-05-23 10:08:29 +0800
committerchai <215380520@qq.com>2024-05-23 10:08:29 +0800
commit8722a9920c1f6119bf6e769cba270e63097f8e25 (patch)
tree2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/HexagonalTurnBased/TurnBasedDoor.cs
parent3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff)
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/HexagonalTurnBased/TurnBasedDoor.cs')
-rw-r--r--Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/HexagonalTurnBased/TurnBasedDoor.cs72
1 files changed, 72 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/HexagonalTurnBased/TurnBasedDoor.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/HexagonalTurnBased/TurnBasedDoor.cs
new file mode 100644
index 0000000..efe96fb
--- /dev/null
+++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/HexagonalTurnBased/TurnBasedDoor.cs
@@ -0,0 +1,72 @@
+using UnityEngine;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace Pathfinding.Examples {
+ /// <summary>Helper script in the example scene 'Turn Based'</summary>
+ [RequireComponent(typeof(Animator))]
+ [RequireComponent(typeof(SingleNodeBlocker))]
+ [HelpURL("https://arongranberg.com/astar/documentation/stable/turnbaseddoor.html")]
+ public class TurnBasedDoor : MonoBehaviour {
+ Animator animator;
+ SingleNodeBlocker blocker;
+
+ bool open;
+
+ void Awake () {
+ animator = GetComponent<Animator>();
+ blocker = GetComponent<SingleNodeBlocker>();
+ }
+
+ void Start () {
+ // Make sure the door starts out blocked
+ blocker.BlockAtCurrentPosition();
+ animator.CrossFade("close", 0.2f);
+ }
+
+ public void Close () {
+ StartCoroutine(WaitAndClose());
+ }
+
+ IEnumerator WaitAndClose () {
+ var selector = new List<SingleNodeBlocker>() { blocker };
+ var node = AstarPath.active.GetNearest(transform.position).node;
+
+ // Wait while there is another SingleNodeBlocker occupying the same node as the door
+ // this is likely another unit which is standing on the door node, and then we cannot
+ // close the door
+ if (blocker.manager.NodeContainsAnyExcept(node, selector)) {
+ // Door is blocked
+ animator.CrossFade("blocked", 0.2f);
+ }
+
+ while (blocker.manager.NodeContainsAnyExcept(node, selector)) {
+ yield return null;
+ }
+
+ open = false;
+ animator.CrossFade("close", 0.2f);
+ blocker.BlockAtCurrentPosition();
+ }
+
+ public void Open () {
+ // Stop WaitAndClose if it is running
+ StopAllCoroutines();
+
+ // Play the open door animation
+ animator.CrossFade("open", 0.2f);
+ open = true;
+
+ // Unblock the door node so that units can traverse it again
+ blocker.Unblock();
+ }
+
+ public void Toggle () {
+ if (open) {
+ Close();
+ } else {
+ Open();
+ }
+ }
+ }
+}