/Unity/Assets/AstarPathfindingProject/Modifiers/Modifiers.cs

https://bitbucket.org/z_ul-abdeen/thirdyearprojectunity · C# · 274 lines · 156 code · 49 blank · 69 comment · 21 complexity · 1b7158043c472bf1b12abc1f6d849623 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Pathfinding {
  5. //To be able to serialize modifiers, we store it in a holder which can contain any modifier type
  6. /*[System.Serializable]
  7. public class ModifierHolder {
  8. public int activeModifier;
  9. public Modifier defaultMod;
  10. public FunnelModifier funnelMod;
  11. public SimpleSmoothModifier simpleSmoothMod;
  12. public Modifier GetModifier () {
  13. switch (activeModifier) {
  14. case 0:
  15. return defaultMod;
  16. case 1:
  17. return funnelMod;
  18. case 2:
  19. return simpleSmoothMod;
  20. }
  21. return defaultMod;
  22. }
  23. public Vector3[] Apply (Node[] path, Vector3 start, Vector3 end, int startIndex, int endIndex, NavGraph graph) {
  24. return GetModifier ().Apply (path,start,end, startIndex, endIndex, graph);
  25. }
  26. public Vector3[] Apply (Vector3[] path, Vector3 start, Vector3 end) {
  27. return GetModifier ().Apply (path, start, end);
  28. }
  29. }*/
  30. /** Defines inputs and outputs for a modifier */
  31. [System.Flags]
  32. public enum ModifierData {
  33. All = -1, /**< All bits set to 1 */
  34. StrictNodePath = 1 << 0, /**< Node array with original length */
  35. NodePath = 1 << 1, /**< Node array */
  36. StrictVectorPath = 1 << 2, /**< Vector path with original length (same as node path array length). Think of it as: the node positions have changed */
  37. VectorPath = 1 << 3, /**< Vector path */
  38. Original = 1 << 4, /**< Used when the modifier requires to be the first in the list (or after a modifier outputting ModifierData.All) */
  39. None = 0, /**< Zero (no bits true) */
  40. Nodes = NodePath | StrictNodePath, /**< Combine of NodePath and StrictNodePath */
  41. Vector = VectorPath | StrictVectorPath /**< Combine of VectorPath and StrictVectorPath */
  42. }
  43. /** Base for all path modifiers.
  44. * \see MonoModifier
  45. * Modifier */
  46. public interface IPathModifier {
  47. int Priority {
  48. get;
  49. set;
  50. }
  51. ModifierData input { get; }
  52. ModifierData output { get; }
  53. void ApplyOriginal (Path p);
  54. void Apply (Path p, ModifierData source);
  55. void PreProcess (Path p);
  56. }
  57. /** Base class for path modifiers which are not attached to GameObjects.
  58. * \see MonoModifier */
  59. [System.Serializable]
  60. public abstract class PathModifier : IPathModifier {
  61. /** Higher priority modifiers are executed first */
  62. public int priority = 0;
  63. [System.NonSerialized]
  64. public Seeker seeker;
  65. public abstract ModifierData input { get; }
  66. public abstract ModifierData output { get; }
  67. public int Priority {
  68. get {
  69. return priority;
  70. }
  71. set {
  72. priority = value;
  73. }
  74. }
  75. public void Awake (Seeker s) {
  76. seeker = s;
  77. if (s != null) {
  78. s.RegisterModifier (this);
  79. }
  80. }
  81. public void OnDestroy (Seeker s) {
  82. if (s != null) {
  83. s.DeregisterModifier (this);
  84. }
  85. }
  86. /** \deprecated */
  87. [System.Obsolete]
  88. public virtual void ApplyOriginal (Path p) {
  89. }
  90. /** Main Post-Processing function */
  91. public abstract void Apply (Path p, ModifierData source);
  92. /** \deprecated */
  93. [System.Obsolete]
  94. public virtual void PreProcess (Path p) {
  95. }
  96. }
  97. /** Base class for path modifiers which can be attached to GameObjects.
  98. * \see[AddComponentMenu("CONTEXT/Seeker/Something")] Modifier */
  99. [System.Serializable]
  100. public abstract class MonoModifier : MonoBehaviour, IPathModifier {
  101. public void OnEnable () {}
  102. public void OnDisable () {}
  103. [System.NonSerialized]
  104. public Seeker seeker;
  105. /** Higher priority modifiers are executed first */
  106. public int priority = 0;
  107. public int Priority {
  108. get {
  109. return priority;
  110. }
  111. set {
  112. priority = value;
  113. }
  114. }
  115. public abstract ModifierData input { get; }
  116. public abstract ModifierData output { get; }
  117. /** Alerts the Seeker that this modifier exists */
  118. public void Awake () {
  119. seeker = GetComponent<Seeker>();
  120. if (seeker != null) {
  121. seeker.RegisterModifier (this);
  122. //seeker.postProcessOriginalPath += new OnPathDelegate (ApplyOriginal);
  123. //seeker.postProcessPath += new OnPathDelegate (Apply);
  124. //seeker.getNextTarget += new GetNextTargetDelegate (GetNextTarget);
  125. }
  126. }
  127. public void OnDestroy () {
  128. if (seeker != null) {
  129. seeker.DeregisterModifier (this);
  130. //seeker.postProcessOriginalPath -= new OnPathDelegate (ApplyOriginal);
  131. //seeker.postProcessPath -= new OnPathDelegate (Apply);
  132. //seeker.getNextTarget -= new GetNextTargetDelegate (GetNextTarget);
  133. }
  134. }
  135. /** \deprecated */
  136. [System.Obsolete]
  137. public virtual void ApplyOriginal (Path p) {
  138. //Debug.Log ("Base call");
  139. }
  140. /** Main Post-Processing function */
  141. public abstract void Apply (Path p, ModifierData source);
  142. [System.Obsolete]
  143. public virtual void PreProcess (Path p) {
  144. }
  145. //This is for the first pass of original data modifiers
  146. /** \deprecated */
  147. [System.Obsolete]
  148. public virtual Vector3[] Apply (GraphNode[] path, Vector3 start, Vector3 end, int startIndex, int endIndex, NavGraph graph) {
  149. Vector3[] p = new Vector3[endIndex-startIndex];
  150. for (int i=startIndex;i< endIndex;i++) {
  151. p[i-startIndex] = (Vector3)path[i].position;
  152. }
  153. return p;
  154. }
  155. /** \deprecated
  156. * This is for all other position only modifiers (mostly smoothers) */
  157. [System.Obsolete]
  158. public virtual Vector3[] Apply (Vector3[] path, Vector3 start, Vector3 end) {
  159. return path;
  160. }
  161. }
  162. public class ModifierConverter {
  163. /** Returns If \a a has all bits that \a b has set to true, also set to true */
  164. public static bool AllBits (ModifierData a, ModifierData b) {
  165. return (a & b) == b;
  166. }
  167. /** Returns If \a a and \a b has any bits in common */
  168. public static bool AnyBits (ModifierData a, ModifierData b) {
  169. return (a & b) != 0;
  170. }
  171. /** Converts a path from \a input to \a output */
  172. public static ModifierData Convert (Path p, ModifierData input, ModifierData output) {
  173. //"Input" can not be converted to "output", log error
  174. if (!CanConvert (input,output)) {
  175. Debug.LogError ("Can't convert "+input+" to "+output);
  176. return ModifierData.None;
  177. }
  178. //"Output" can take "input" with no change, return
  179. if (AnyBits (input,output)) {
  180. return input;
  181. }
  182. //If input is a node path, and output wants a vector array, convert the node array to a vector array
  183. if (AnyBits (input,ModifierData.Nodes) && AnyBits (output, ModifierData.Vector)) {
  184. p.vectorPath.Clear();
  185. for (int i=0;i<p.vectorPath.Count;i++) {
  186. p.vectorPath.Add ((Vector3)p.path[i].position);
  187. }
  188. //Return VectorPath and also StrictVectorPath if input has StrictNodePath set
  189. return ModifierData.VectorPath | (AnyBits (input, ModifierData.StrictNodePath) ? ModifierData.StrictVectorPath : ModifierData.None);
  190. }
  191. Debug.LogError ("This part should not be reached - Error in ModifierConverted\nInput: "+input+" ("+(int)input+")\nOutput: "+output+" ("+(int)output+")");
  192. return ModifierData.None;
  193. }
  194. /** Returns If \a input can be converted to \a output */
  195. public static bool CanConvert (ModifierData input, ModifierData output) {
  196. ModifierData convert = CanConvertTo (input);
  197. return AnyBits (output,convert);
  198. }
  199. /** Returns All data types \a a can be converted to */
  200. public static ModifierData CanConvertTo (ModifierData a) {
  201. if (a == ModifierData.All) {
  202. return ModifierData.All;
  203. }
  204. ModifierData result = a;
  205. if (AnyBits (a,ModifierData.Nodes)) {
  206. result |= ModifierData.VectorPath;
  207. }
  208. if (AnyBits (a,ModifierData.StrictNodePath)) {
  209. result |= ModifierData.StrictVectorPath;
  210. }
  211. if (AnyBits (a,ModifierData.StrictVectorPath)) {
  212. result |= ModifierData.VectorPath;
  213. }
  214. return result;
  215. }
  216. }
  217. }