Modifiers.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using UnityEngine;
  2. namespace Pathfinding {
  3. /// <summary>
  4. /// Base for all path modifiers.
  5. /// See: MonoModifier
  6. /// Modifier
  7. /// </summary>
  8. public interface IPathModifier {
  9. int Order { get; }
  10. void Apply(Path path);
  11. void PreProcess(Path path);
  12. }
  13. /// <summary>
  14. /// Base class for path modifiers which are not attached to GameObjects.
  15. /// See: MonoModifier
  16. /// </summary>
  17. [System.Serializable]
  18. public abstract class PathModifier : IPathModifier {
  19. [System.NonSerialized]
  20. public Seeker seeker;
  21. /// <summary>
  22. /// Modifiers will be executed from lower order to higher order.
  23. /// This value is assumed to stay constant.
  24. /// </summary>
  25. public abstract int Order { get; }
  26. public void Awake (Seeker seeker) {
  27. this.seeker = seeker;
  28. if (seeker != null) {
  29. seeker.RegisterModifier(this);
  30. }
  31. }
  32. public void OnDestroy (Seeker seeker) {
  33. if (seeker != null) {
  34. seeker.DeregisterModifier(this);
  35. }
  36. }
  37. public virtual void PreProcess (Path path) {
  38. // Required by IPathModifier
  39. }
  40. /// <summary>Main Post-Processing function</summary>
  41. public abstract void Apply(Path path);
  42. }
  43. /// <summary>
  44. /// Base class for path modifiers which can be attached to GameObjects.
  45. /// See: Menubar -> Component -> Pathfinding -> Modifiers
  46. /// </summary>
  47. [System.Serializable]
  48. public abstract class MonoModifier : VersionedMonoBehaviour, IPathModifier {
  49. [System.NonSerialized]
  50. public Seeker seeker;
  51. /// <summary>Alerts the Seeker that this modifier exists</summary>
  52. protected virtual void OnEnable () {
  53. seeker = GetComponent<Seeker>();
  54. if (seeker != null) {
  55. seeker.RegisterModifier(this);
  56. }
  57. }
  58. protected virtual void OnDisable () {
  59. if (seeker != null) {
  60. seeker.DeregisterModifier(this);
  61. }
  62. }
  63. /// <summary>
  64. /// Modifiers will be executed from lower order to higher order.
  65. /// This value is assumed to stay constant.
  66. /// </summary>
  67. public abstract int Order { get; }
  68. public virtual void PreProcess (Path path) {
  69. // Required by IPathModifier
  70. }
  71. /// <summary>Called for each path that the Seeker calculates after the calculation has finished</summary>
  72. public abstract void Apply(Path path);
  73. }
  74. }