Parallel.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using Urho;
  4. namespace Urho.Actions
  5. {
  6. public class Parallel : FiniteTimeAction
  7. {
  8. public FiniteTimeAction[] Actions { get; private set; }
  9. #region Constructors
  10. public Parallel (params FiniteTimeAction[] actions) : base ()
  11. {
  12. // Can't call base(duration) because max action duration needs to be determined here
  13. float maxDuration = 0.0f;
  14. foreach (FiniteTimeAction action in actions)
  15. {
  16. if (action.Duration > maxDuration)
  17. {
  18. maxDuration = action.Duration;
  19. }
  20. }
  21. Duration = maxDuration;
  22. Actions = actions;
  23. for (int i = 0; i < Actions.Length; i++)
  24. {
  25. var actionDuration = Actions [i].Duration;
  26. if (actionDuration < Duration)
  27. {
  28. Actions [i] = new Sequence (Actions [i], new DelayTime (Duration - actionDuration));
  29. }
  30. }
  31. }
  32. #endregion Constructors
  33. protected internal override ActionState StartAction(Node target)
  34. {
  35. return new ParallelState (this, target);
  36. }
  37. public override FiniteTimeAction Reverse ()
  38. {
  39. FiniteTimeAction[] rev = new FiniteTimeAction[Actions.Length];
  40. for (int i = 0; i < Actions.Length; i++)
  41. {
  42. rev [i] = Actions [i].Reverse ();
  43. }
  44. return new Parallel (rev);
  45. }
  46. }
  47. public class ParallelState : FiniteTimeActionState
  48. {
  49. protected FiniteTimeAction[] Actions { get; set; }
  50. protected FiniteTimeActionState[] ActionStates { get; set; }
  51. public ParallelState (Parallel action, Node target)
  52. : base (action, target)
  53. {
  54. Actions = action.Actions;
  55. ActionStates = new FiniteTimeActionState[Actions.Length];
  56. for (int i = 0; i < Actions.Length; i++)
  57. {
  58. ActionStates [i] = (FiniteTimeActionState)Actions [i].StartAction (target);
  59. }
  60. }
  61. protected internal override void Stop ()
  62. {
  63. for (int i = 0; i < Actions.Length; i++)
  64. {
  65. ActionStates [i].Stop ();
  66. }
  67. base.Stop ();
  68. }
  69. public override void Update (float time)
  70. {
  71. for (int i = 0; i < Actions.Length; i++)
  72. {
  73. ActionStates [i].Update (time);
  74. }
  75. }
  76. }
  77. }