Guest User

Untitled

a guest
Dec 12th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6.  
  7. namespace Client.Game.Abilities.Utils
  8. {
  9. public class ActionSequence : List<ActionElement>
  10. {
  11.  
  12. public float accum = 0f;
  13. public int index = -1;
  14.  
  15. public float TotalTime {
  16. get {
  17. var ret = 0f;
  18. foreach (var element in this)
  19. ret += element.Duration;
  20.  
  21. return ret;
  22. }
  23. }
  24.  
  25. public void Insert(ActionElement element)
  26. {
  27. element.Timestamp = TotalTime;
  28. this.Add(element);
  29. }
  30.  
  31. public void Insert(Action action, float duration = 0f)
  32. {
  33. Insert(new ActionElement(action, duration));
  34. }
  35.  
  36. internal void InsertContinuous(Action action, float duration)
  37. {
  38. Insert(new ActionElement(action, duration, true));
  39. }
  40.  
  41. private int IndexAtTime(float time)
  42. {
  43. int i = 0;
  44. var iat = i;
  45. for(; i<this.Count; i++)
  46. {
  47. if (this[i].Timestamp <= time)
  48. iat = i;
  49. else
  50. return iat;
  51. }
  52. return iat;
  53. }
  54.  
  55. internal void InsertWait(float time)
  56. {
  57. Insert(() => { }, time);
  58. }
  59.  
  60. public void Update(float dt)
  61. {
  62. accum += dt;
  63. var newIndex = IndexAtTime(accum);
  64. for( var i = index; i<= newIndex; i++)
  65. {
  66. if(i > index)
  67. {
  68. index = i;
  69. //Debug.Log("Invoking action index " + i + " at time: " + accum);
  70. this[i].Action();
  71. return;
  72. }
  73. }
  74. var current = this[newIndex];
  75. if(current.Continuous)
  76. {
  77. current.Action();
  78. }
  79.  
  80. }
  81.  
  82. public void Log()
  83. {
  84. var str = new StringBuilder();
  85. foreach( var action in this)
  86. {
  87. str.AppendFormat("{0}:{1}\n", action.Timestamp, action.Action);
  88. }
  89.  
  90. Debug.Log(str.ToString());
  91. }
  92. }
  93.  
  94. public class ActionElement
  95. {
  96. public float Duration;
  97. public Action Action;
  98. public bool Continuous;
  99. public float Timestamp;
  100. public ActionElement(Action action, float duration, bool callContinuously = false)
  101. {
  102. this.Duration = duration;
  103. this.Action = action;
  104. this.Continuous = callContinuously;
  105. }
  106. }
  107. }
Add Comment
Please, Sign In to add comment