You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

82 lines
2.0KB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace coa4u
  5. {
  6. /// <summary>
  7. /// Runs the given action the several times. Also can repeat the action forever.
  8. /// </summary>
  9. class ActionRepeat : ActionInterval
  10. {
  11. protected ActionInterval action;
  12. protected int count;
  13. protected int counter;
  14. protected bool forever;
  15. public ActionRepeat(ActionInterval targetAction, int targetCount)
  16. : base(0)
  17. {
  18. action = targetAction;
  19. count = targetCount;
  20. counter = 0;
  21. forever = false;
  22. }
  23. public ActionRepeat(ActionInterval targetAction)
  24. : base(0)
  25. {
  26. action = targetAction;
  27. count = 0;
  28. counter = 0;
  29. forever = true;
  30. }
  31. public override ActionInstant Clone()
  32. {
  33. return new ActionRepeat((ActionInterval)action.Clone(), count);
  34. }
  35. public override ActionInstant Reverse()
  36. {
  37. return new ActionRepeat((ActionInterval)action.Reverse(), count);
  38. }
  39. public override void Start()
  40. {
  41. base.Start();
  42. action.SetActor(target);
  43. action.Start();
  44. counter = 0;
  45. }
  46. public override void StepTimer(float dt)
  47. {
  48. dt *= timeScale;
  49. if (action.running)
  50. {
  51. action.StepTimer(dt);
  52. }
  53. if (!action.running && (forever || counter < count - 1))
  54. {
  55. float dtrdata = action.dtr;
  56. action.Start();
  57. if (dtrdata > 0)
  58. action.StepTimer(dtrdata);
  59. counter += 1;
  60. }
  61. else if (!action.running && counter >= count - 1)
  62. {
  63. dtr = action.dtr;
  64. Stop();
  65. }
  66. }
  67. public override void Stop()
  68. {
  69. base.Stop();
  70. if (action.running)
  71. action.Stop();
  72. }
  73. }
  74. }