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.

78 lines
2.0KB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace coa4u
  5. {
  6. /// <summary>
  7. /// Runs one random action from the given list.
  8. /// </summary>
  9. class ActionRandom : ActionInterval
  10. {
  11. protected ActionInstant[] actions;
  12. protected int index;
  13. public ActionRandom(ActionInstant action1, ActionInstant action2)
  14. : base(0)
  15. {
  16. actions = new ActionInstant[] { action1, action2 };
  17. }
  18. public ActionRandom(ActionInstant[] actionsList)
  19. : base(0)
  20. {
  21. actions = actionsList;
  22. }
  23. public override ActionInstant Clone()
  24. {
  25. ActionInstant[] aList = new ActionInstant[actions.Length];
  26. for (int i = 0; i < actions.Length; i++)
  27. {
  28. aList[i] = actions[i].Clone();
  29. }
  30. return new ActionRandom(aList);
  31. }
  32. public override ActionInstant Reverse()
  33. {
  34. ActionInstant[] aList = new ActionInstant[actions.Length];
  35. for (int i = 0; i < actions.Length; i++)
  36. {
  37. aList[i] = actions[i].Reverse();
  38. }
  39. return new ActionRandom(aList);
  40. }
  41. public override void Start()
  42. {
  43. base.Start();
  44. index = UnityEngine.Random.Range(0, actions.Length);
  45. actions[index].SetActor(target);
  46. actions[index].Start();
  47. }
  48. public override void StepTimer(float dt)
  49. {
  50. dt *= timeScale;
  51. if (actions[index].running)
  52. actions[index].StepTimer(dt);
  53. if (!actions[index].running)
  54. {
  55. Stop();
  56. dtr = actions[index].dtr;
  57. }
  58. }
  59. /// <summary>
  60. /// This method is called after the interval action is stopped.
  61. /// </summary>
  62. public override void Stop()
  63. {
  64. base.Stop();
  65. actions[index].Stop();
  66. }
  67. }
  68. }