123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using UnityEngine;
- using System.Collections.Generic;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_dynamic_grid_obstacle.php")]
- public class DynamicGridObstacle : GraphModifier {
-
- Collider coll;
-
- Collider2D coll2D;
-
- Transform tr;
-
- public float updateError = 1;
-
-
-
-
-
-
-
- public float checkTime = 0.2F;
-
- Bounds prevBounds;
-
- Quaternion prevRotation;
-
- bool prevEnabled;
- float lastCheckTime = -9999;
- Queue<GraphUpdateObject> pendingGraphUpdates = new Queue<GraphUpdateObject>();
- Bounds bounds {
- get {
- if (coll != null) {
- return coll.bounds;
- } else {
- var b = coll2D.bounds;
-
-
- b.extents += new Vector3(0, 0, 10000);
- return b;
- }
- }
- }
- bool colliderEnabled {
- get {
- return coll != null ? coll.enabled : coll2D.enabled;
- }
- }
- protected override void Awake () {
- base.Awake();
- coll = GetComponent<Collider>();
- coll2D = GetComponent<Collider2D>();
- tr = transform;
- if (coll == null && coll2D == null && Application.isPlaying) {
- throw new System.Exception("A collider or 2D collider must be attached to the GameObject(" + gameObject.name + ") for the DynamicGridObstacle to work");
- }
- prevBounds = bounds;
- prevRotation = tr.rotation;
-
- prevEnabled = false;
- }
- public override void OnPostScan () {
-
-
- if (coll == null) Awake();
-
-
- if (coll != null) prevEnabled = colliderEnabled;
- }
- void Update () {
- if (!Application.isPlaying) return;
- if (coll == null && coll2D == null) {
- Debug.LogError("Removed collider from DynamicGridObstacle", this);
- enabled = false;
- return;
- }
-
-
-
- while (pendingGraphUpdates.Count > 0 && pendingGraphUpdates.Peek().stage != GraphUpdateStage.Pending) {
- pendingGraphUpdates.Dequeue();
- }
- if (AstarPath.active == null || AstarPath.active.isScanning || Time.realtimeSinceStartup - lastCheckTime < checkTime || !Application.isPlaying || pendingGraphUpdates.Count > 0) {
- return;
- }
- lastCheckTime = Time.realtimeSinceStartup;
- if (colliderEnabled) {
-
- Bounds newBounds = bounds;
- var newRotation = tr.rotation;
- Vector3 minDiff = prevBounds.min - newBounds.min;
- Vector3 maxDiff = prevBounds.max - newBounds.max;
- var extents = newBounds.extents.magnitude;
-
-
- var errorFromRotation = extents*Quaternion.Angle(prevRotation, newRotation)*Mathf.Deg2Rad;
-
- if (minDiff.sqrMagnitude > updateError*updateError || maxDiff.sqrMagnitude > updateError*updateError ||
- errorFromRotation > updateError || !prevEnabled) {
-
- DoUpdateGraphs();
- }
- } else {
-
- if (prevEnabled) {
- DoUpdateGraphs();
- }
- }
- }
-
-
-
-
- protected override void OnDisable () {
- base.OnDisable();
- if (AstarPath.active != null && Application.isPlaying) {
- var guo = new GraphUpdateObject(prevBounds);
- pendingGraphUpdates.Enqueue(guo);
- AstarPath.active.UpdateGraphs(guo);
- prevEnabled = false;
- }
-
-
-
- pendingGraphUpdates.Clear();
- }
-
-
-
-
-
-
- public void DoUpdateGraphs () {
- if (coll == null && coll2D == null) return;
-
- UnityEngine.Physics.SyncTransforms();
- UnityEngine.Physics2D.SyncTransforms();
- if (!colliderEnabled) {
-
-
- var guo = new GraphUpdateObject(prevBounds);
- pendingGraphUpdates.Enqueue(guo);
- AstarPath.active.UpdateGraphs(guo);
- } else {
- Bounds newBounds = bounds;
- Bounds merged = newBounds;
- merged.Encapsulate(prevBounds);
-
-
- if (BoundsVolume(merged) < BoundsVolume(newBounds) + BoundsVolume(prevBounds)) {
-
- var guo = new GraphUpdateObject(merged);
- pendingGraphUpdates.Enqueue(guo);
- AstarPath.active.UpdateGraphs(guo);
- } else {
-
- var guo1 = new GraphUpdateObject(prevBounds);
- var guo2 = new GraphUpdateObject(newBounds);
- pendingGraphUpdates.Enqueue(guo1);
- pendingGraphUpdates.Enqueue(guo2);
- AstarPath.active.UpdateGraphs(guo1);
- AstarPath.active.UpdateGraphs(guo2);
- }
- #if ASTARDEBUG
- Debug.DrawLine(prevBounds.min, prevBounds.max, Color.yellow);
- Debug.DrawLine(newBounds.min, newBounds.max, Color.red);
- #endif
- prevBounds = newBounds;
- }
- prevEnabled = colliderEnabled;
- prevRotation = tr.rotation;
-
- lastCheckTime = Time.realtimeSinceStartup;
- }
-
- static float BoundsVolume (Bounds b) {
- return System.Math.Abs(b.size.x * b.size.y * b.size.z);
- }
- }
- }
|