/Code/src/com/game/AI/TaskController.java

https://bitbucket.org/DeveloperUX/behaviortree · Java · 145 lines · 66 code · 16 blank · 63 comment · 0 complexity · 2536771d6322aa0579827b9db6ec68cf MD5 · raw file

  1. package com.game.AI;
  2. /**
  3. * Class added by composition to any task, to keep track of the Task state
  4. * and logic flow.
  5. *
  6. * This state-control class is separated from the Task class so the Decorators
  7. * have a chance at compile-time security.
  8. * @author Ying
  9. *
  10. */
  11. public class TaskController
  12. {
  13. /**
  14. * Indicates whether the task is finished or not
  15. */
  16. private boolean done;
  17. /**
  18. * If finished, it indicates if it has finished with success or not
  19. */
  20. private boolean sucess;
  21. /**
  22. * Indicates if the task has started or not
  23. */
  24. private boolean started;
  25. /**
  26. * Reference to the task we monitor
  27. */
  28. private Task task;
  29. /**
  30. * Creates a new instance of the TaskController class
  31. * @param task Task to controll.
  32. */
  33. public TaskController(Task task)
  34. {
  35. SetTask(task);
  36. Initialize();
  37. }
  38. /**
  39. * Initializes the class data
  40. */
  41. private void Initialize()
  42. {
  43. this.done = false;
  44. this.sucess = true;
  45. this.started = false;
  46. }
  47. /**
  48. * Sets the task reference
  49. * @param task Task to monitor
  50. */
  51. public void SetTask(Task task)
  52. {
  53. this.task = task;
  54. }
  55. /**
  56. * Starts the monitored class
  57. */
  58. public void SafeStart()
  59. {
  60. this.started = true;
  61. task.Start();
  62. }
  63. /**
  64. * Ends the monitored task
  65. */
  66. public void SafeEnd()
  67. {
  68. this.done = false;
  69. this.started = false;
  70. task.End();
  71. }
  72. /**
  73. * Ends the monitored class, with success
  74. */
  75. protected void FinishWithSuccess()
  76. {
  77. this.sucess = true;
  78. this.done = true;
  79. task.LogTask("Finished with success");
  80. }
  81. /**
  82. * Ends the monitored class, with failure
  83. */
  84. protected void FinishWithFailure()
  85. {
  86. this.sucess = false;
  87. this.done = true;
  88. task.LogTask("Finished with failure");
  89. }
  90. /**
  91. * Indicates whether the task finished successfully
  92. * @return True if it did, false if it didn't
  93. */
  94. public boolean Succeeded()
  95. {
  96. return this.sucess;
  97. }
  98. /**
  99. * Indicates whether the task finished with failure
  100. * @return True if it did, false if it didn't
  101. */
  102. public boolean Failed()
  103. {
  104. return !this.sucess;
  105. }
  106. /**
  107. * Indicates whether the task finished
  108. * @return True if it did, false if it didn't
  109. */
  110. public boolean Finished()
  111. {
  112. return this.done;
  113. }
  114. /**
  115. * Indicates whether the class has started or not
  116. * @return True if it has, false if it hasn't
  117. */
  118. public boolean Started()
  119. {
  120. return this.started;
  121. }
  122. /**
  123. * Marks the class as just started.
  124. */
  125. public void Reset()
  126. {
  127. this.done = false;
  128. }
  129. }