123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using UnityEngine;
- using System.Collections.Generic;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
- [ExecuteInEditMode]
- public abstract class GraphModifier : VersionedMonoBehaviour {
-
- private static GraphModifier root;
- private GraphModifier prev;
- private GraphModifier next;
-
- [SerializeField]
- [HideInInspector]
- protected ulong uniqueID;
-
- protected static Dictionary<ulong, GraphModifier> usedIDs = new Dictionary<ulong, GraphModifier>();
- protected static List<T> GetModifiersOfType<T>() where T : GraphModifier {
- var current = root;
- var result = new List<T>();
- while (current != null) {
- var cast = current as T;
- if (cast != null) result.Add(cast);
- current = current.next;
- }
- return result;
- }
- public static void FindAllModifiers () {
- var allModifiers = FindObjectsOfType(typeof(GraphModifier)) as GraphModifier[];
- for (int i = 0; i < allModifiers.Length; i++) {
- if (allModifiers[i].enabled) allModifiers[i].OnEnable();
- }
- }
-
- public enum EventType {
- PostScan = 1 << 0,
- PreScan = 1 << 1,
- LatePostScan = 1 << 2,
- PreUpdate = 1 << 3,
- PostUpdate = 1 << 4,
- PostCacheLoad = 1 << 5
- }
-
- public static void TriggerEvent (GraphModifier.EventType type) {
- if (!Application.isPlaying) {
- FindAllModifiers();
- }
- GraphModifier c = root;
- switch (type) {
- case EventType.PreScan:
- while (c != null) { c.OnPreScan(); c = c.next; }
- break;
- case EventType.PostScan:
- while (c != null) { c.OnPostScan(); c = c.next; }
- break;
- case EventType.LatePostScan:
- while (c != null) { c.OnLatePostScan(); c = c.next; }
- break;
- case EventType.PreUpdate:
- while (c != null) { c.OnGraphsPreUpdate(); c = c.next; }
- break;
- case EventType.PostUpdate:
- while (c != null) { c.OnGraphsPostUpdate(); c = c.next; }
- break;
- case EventType.PostCacheLoad:
- while (c != null) { c.OnPostCacheLoad(); c = c.next; }
- break;
- }
- }
-
- protected virtual void OnEnable () {
- RemoveFromLinkedList();
- AddToLinkedList();
- ConfigureUniqueID();
- }
-
- protected virtual void OnDisable () {
- RemoveFromLinkedList();
- }
- protected override void Awake () {
- base.Awake();
- ConfigureUniqueID();
- }
- void ConfigureUniqueID () {
-
-
- GraphModifier usedBy;
- if (usedIDs.TryGetValue(uniqueID, out usedBy) && usedBy != this) {
- Reset();
- }
- usedIDs[uniqueID] = this;
- }
- void AddToLinkedList () {
- if (root == null) {
- root = this;
- } else {
- next = root;
- root.prev = this;
- root = this;
- }
- }
- void RemoveFromLinkedList () {
- if (root == this) {
- root = next;
- if (root != null) root.prev = null;
- } else {
- if (prev != null) prev.next = next;
- if (next != null) next.prev = prev;
- }
- prev = null;
- next = null;
- }
- protected virtual void OnDestroy () {
- usedIDs.Remove(uniqueID);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public virtual void OnPostScan () {}
-
-
-
-
-
-
-
-
-
-
-
-
- public virtual void OnPreScan () {}
-
-
-
-
- public virtual void OnLatePostScan () {}
-
-
-
-
-
- public virtual void OnPostCacheLoad () {}
-
- public virtual void OnGraphsPreUpdate () {}
-
-
-
-
- public virtual void OnGraphsPostUpdate () {}
- protected override void Reset () {
- base.Reset();
-
- var rnd1 = (ulong)Random.Range(0, int.MaxValue);
- var rnd2 = ((ulong)Random.Range(0, int.MaxValue) << 32);
- uniqueID = rnd1 | rnd2;
- usedIDs[uniqueID] = this;
- }
- }
- }
|