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.

106 lines
2.8KB

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