ConditionalNode.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using GraphProcessor;
  7. using UnityEngine;
  8. namespace NodeGraphProcessor.Examples
  9. {
  10. [System.Serializable]
  11. /// <summary>
  12. /// This is the base class for every node that is executed by the conditional processor, it takes an executed bool as input to
  13. /// </summary>
  14. public abstract class ConditionalNode : BaseNode, IConditionalNode
  15. {
  16. // These booleans will controls wether or not the execution of the folowing nodes will be done or discarded.
  17. [Input(name = "Executed", allowMultiple = true)]
  18. public ConditionalLink executed;
  19. public abstract IEnumerable< ConditionalNode > GetExecutedNodes();
  20. // Assure that the executed field is always at the top of the node port section
  21. public override FieldInfo[] GetNodeFields()
  22. {
  23. var fields = base.GetNodeFields();
  24. Array.Sort(fields, (f1, f2) => f1.Name == nameof(executed) ? -1 : 1);
  25. return fields;
  26. }
  27. }
  28. [System.Serializable]
  29. /// <summary>
  30. /// This class represent a simple node which takes one event in parameter and pass it to the next node
  31. /// </summary>
  32. public abstract class LinearConditionalNode : ConditionalNode, IConditionalNode
  33. {
  34. [Output(name = "Executes")]
  35. public ConditionalLink executes;
  36. public override IEnumerable< ConditionalNode > GetExecutedNodes()
  37. {
  38. // Return all the nodes connected to the executes port
  39. return outputPorts.FirstOrDefault(n => n.fieldName == nameof(executes))
  40. .GetEdges().Select(e => e.inputNode as ConditionalNode);
  41. }
  42. }
  43. [System.Serializable]
  44. /// <summary>
  45. /// This class represent a waitable node which invokes another node after a time/frame
  46. /// </summary>
  47. public abstract class WaitableNode : LinearConditionalNode
  48. {
  49. [Output(name = "Execute After")]
  50. public ConditionalLink executeAfter;
  51. protected void ProcessFinished()
  52. {
  53. onProcessFinished.Invoke(this);
  54. }
  55. [HideInInspector]
  56. public Action<WaitableNode> onProcessFinished;
  57. public IEnumerable< ConditionalNode > GetExecuteAfterNodes()
  58. {
  59. return outputPorts.FirstOrDefault(n => n.fieldName == nameof(executeAfter))
  60. .GetEdges().Select(e => e.inputNode as ConditionalNode);
  61. }
  62. }
  63. }