RepeaterRandom.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. namespace BehaviorDesigner.Runtime.Tasks
  2. {
  3. [TaskDescription(@"The repeater task will repeat execution of its child task until the child task has been run a specified number of times. " +
  4. "It has the option of continuing to execute the child task even if the child task returns a failure.")]
  5. [TaskIcon("{SkinColor}RepeaterIcon.png")]
  6. public class RepeaterRandom : Decorator
  7. {
  8. [Tooltip("The number of times to repeat the execution of its child task")]
  9. public SharedInt count = 1;
  10. public SharedInt minCount = 1;
  11. public SharedInt Maxcount = 3;
  12. [Tooltip("Allows the repeater to repeat forever")]
  13. public SharedBool repeatForever;
  14. [Tooltip("Should the task return if the child task returns a failure")]
  15. public SharedBool endOnFailure;
  16. // The number of times the child task has been run.
  17. private int executionCount = 0;
  18. // The status of the child after it has finished running.
  19. private TaskStatus executionStatus = TaskStatus.Inactive;
  20. public override void OnStart()
  21. {
  22. count = UnityEngine.Random.Range(minCount.Value, Maxcount.Value + 1);
  23. }
  24. public override bool CanExecute()
  25. {
  26. // Continue executing until we've reached the count or the child task returned failure and we should stop on a failure.
  27. return (repeatForever.Value || executionCount < count.Value) && (!endOnFailure.Value || (endOnFailure.Value && executionStatus != TaskStatus.Failure));
  28. }
  29. public override void OnChildExecuted(TaskStatus childStatus)
  30. {
  31. // The child task has finished execution. Increase the execution count and update the execution status.
  32. executionCount++;
  33. executionStatus = childStatus;
  34. }
  35. public override void OnEnd()
  36. {
  37. // Reset the variables back to their starting values.
  38. executionCount = 0;
  39. executionStatus = TaskStatus.Inactive;
  40. }
  41. public override void OnReset()
  42. {
  43. // Reset the public properties back to their original values.
  44. count = 0;
  45. endOnFailure = true;
  46. }
  47. }
  48. }