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.

111 lines
3.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace coa4u
  5. {
  6. /// <summary>
  7. /// Runs several actions in a sequence.
  8. /// </summary>
  9. class ActionSequence : ActionInterval
  10. {
  11. protected ActionInstant[] actions;
  12. protected int index;
  13. public ActionSequence(ActionInstant action1, ActionInstant action2)
  14. : base(0)
  15. {
  16. actions = new ActionInstant[] { action1, action2 };
  17. }
  18. public ActionSequence(ActionInstant[] actionsList)
  19. : base(0)
  20. {
  21. actions = actionsList;
  22. }
  23. /// <summary>
  24. /// Returns a copy of the action.
  25. /// </summary>
  26. public override ActionInstant clone()
  27. {
  28. ActionInstant[] aList = new ActionInstant[actions.Length];
  29. for (int i = 0; i < actions.Length; i++)
  30. {
  31. aList[i] = actions[i].clone();
  32. }
  33. return new ActionSequence(aList);
  34. }
  35. /// <summary>
  36. /// Returns the reversed version of the action, if it is possible.
  37. /// </summary>
  38. public override ActionInstant reverse()
  39. {
  40. ActionInstant[] aList = new ActionInstant[actions.Length];
  41. for (int i = 0; i < actions.Length; i++)
  42. {
  43. aList[actions.Length - 1 - i] = actions[i].reverse();
  44. }
  45. return new ActionSequence(aList);
  46. }
  47. /// <summary>
  48. /// This method is called at the action start.
  49. /// </summary>
  50. public override void start()
  51. {
  52. base.start();
  53. index = 0;
  54. actions[0].setActor(target);
  55. actions[0].start();
  56. while (!actions[index].running && index < actions.Length)
  57. {
  58. index += 1;
  59. actions[index].setActor(target);
  60. actions[index].start();
  61. }
  62. }
  63. /// <summary>
  64. /// This method is called every frame update.
  65. /// </summary>
  66. public override void step(float dt)
  67. {
  68. dt *= timeScale;
  69. float dtrdata = 0;
  70. if (actions[index].running)
  71. {
  72. actions[index].step(dt);
  73. if (!actions[index].running)
  74. dtrdata = actions[index].dtr;
  75. }
  76. while (!actions[index].running && index < actions.Length - 1)
  77. {
  78. index += 1;
  79. actions[index].setActor(target);
  80. actions[index].start();
  81. if (actions[index].running && dtrdata > 0)
  82. actions[index].step(dtrdata);
  83. }
  84. if (!actions[index].running)
  85. {
  86. stop();
  87. dtr = dtrdata;
  88. }
  89. }
  90. /// <summary>
  91. /// This method is called after the interval action is stopped.
  92. /// </summary>
  93. public override void stop()
  94. {
  95. base.stop();
  96. for (int i = 0; i < actions.Length; i++)
  97. {
  98. actions[i].stop();
  99. }
  100. }
  101. }
  102. }