Node.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Model
  4. {
  5. public abstract class Node
  6. {
  7. private readonly NodeProto nodeProto;
  8. private long id;
  9. private string description;
  10. protected readonly List<Node> children = new List<Node>();
  11. protected Node(NodeProto nodeProto)
  12. {
  13. this.id = nodeProto.nodeId;
  14. this.description = nodeProto.describe;
  15. this.nodeProto = nodeProto;
  16. }
  17. public Node[] GetChildren
  18. {
  19. get
  20. {
  21. return children.ToArray();
  22. }
  23. }
  24. public NodeProto NodeProto
  25. {
  26. get { return nodeProto; }
  27. }
  28. public string Description
  29. {
  30. get
  31. {
  32. return this.description;
  33. }
  34. set
  35. {
  36. this.description = value;
  37. }
  38. }
  39. /// <summary>
  40. /// 策划配置的id
  41. /// </summary>
  42. public long Id
  43. {
  44. get
  45. {
  46. return this.id;
  47. }
  48. set
  49. {
  50. this.id = value;
  51. }
  52. }
  53. /// <summary>
  54. /// 节点的类型例如: NodeType.Not
  55. /// </summary>
  56. public string Type
  57. {
  58. get
  59. {
  60. return this.nodeProto.name;
  61. }
  62. }
  63. public void AddChild(Node child)
  64. {
  65. this.children.Add(child);
  66. }
  67. public virtual void EndInit(Scene scene)
  68. {
  69. }
  70. public bool DoRun(BehaviorTree behaviorTree, BTEnv env)
  71. {
  72. if (!env.ContainKey(BTEnvKey.NodePath))
  73. {
  74. env.Add(BTEnvKey.NodePath, new List<long>());
  75. }
  76. env.Get<List<long>>(BTEnvKey.NodePath).Add(this.nodeProto.nodeId);
  77. return this.Run(behaviorTree, env);
  78. }
  79. protected abstract bool Run(BehaviorTree behaviorTree, BTEnv env);
  80. }
  81. }