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.

ActionRepeat.cs 2.3KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 tgtAction, int tgtCount)
  16. : base(0)
  17. {
  18. action = tgtAction;
  19. count = tgtCount;
  20. counter = 0;
  21. forever = false;
  22. }
  23. public ActionRepeat(ActionInterval tgtAction)
  24. : base(0)
  25. {
  26. action = tgtAction;
  27. count = 0;
  28. counter = 0;
  29. forever = true;
  30. }
  31. /// <summary>
  32. /// Returns a copy of the action.
  33. /// </summary>
  34. public override ActionInstant clone()
  35. {
  36. return new ActionRepeat((ActionInterval)action.clone(), count);
  37. }
  38. /// <summary>
  39. /// Returns the reversed version of the action, if it is possible.
  40. /// </summary>
  41. public override ActionInstant reverse()
  42. {
  43. return new ActionRepeat((ActionInterval)action.reverse(), count);
  44. }
  45. /// <summary>
  46. /// This method is called at the action start.
  47. /// </summary>
  48. public override void start()
  49. {
  50. base.start();
  51. action.setActor(target);
  52. action.start();
  53. counter = 0;
  54. }
  55. /// <summary>
  56. /// This method is called every frame update.
  57. /// </summary>
  58. public override void step(float dt)
  59. {
  60. dt *= timeScale;
  61. if (action.running)
  62. {
  63. action.step(dt);
  64. }
  65. if (!action.running && (forever || counter < count - 1))
  66. {
  67. float dtrdata = action.dtr;
  68. action.start();
  69. if (dtrdata > 0)
  70. action.step(dtrdata);
  71. counter += 1;
  72. }
  73. else if (!action.running && counter >= count - 1)
  74. {
  75. dtr = action.dtr;
  76. stop();
  77. }
  78. }
  79. }
  80. }