ACurve.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace CombatLibrary.CombatLibrary.CombatCore.Utility
  4. {
  5. public struct ACurve
  6. {
  7. public List<CurveInfo> CurveInfos;
  8. public CurveType CurveType;
  9. public ACurve(CurveInfo[] curveInfos)
  10. {
  11. CurveInfos = new List<CurveInfo>();
  12. CurveInfos.AddRange(curveInfos);
  13. CurveType = CurveType.HYT;
  14. }
  15. public float Evaluate(float t)
  16. {
  17. if (CurveInfos == null)
  18. {
  19. return t;
  20. }
  21. if (CurveInfos.Count < 2)
  22. {
  23. return t;
  24. }
  25. switch (CurveType)
  26. {
  27. case CurveType.HYT:
  28. return HYT(t);
  29. break;
  30. case CurveType.BSL:
  31. return BSL(t);
  32. break;
  33. }
  34. return 0;
  35. }
  36. public Vector2 EvaluateForVector2(float t,float currTime)
  37. {
  38. if (CurveInfos == null)
  39. {
  40. return new Vector2();
  41. }
  42. if (CurveInfos.Count < 2)
  43. {
  44. return new Vector2();
  45. }
  46. switch (CurveType)
  47. {
  48. // case CurveType.HYT:
  49. // // return HYT(t);
  50. // break;
  51. case CurveType.BSL:
  52. return BSLForVector2(t, currTime);
  53. break;
  54. }
  55. return new Vector2();
  56. }
  57. private Vector2 BSLForVector2(float t,float currTime)
  58. {
  59. CurveInfo c1 = default;
  60. CurveInfo c2 = default;
  61. for (int i = 0; i < CurveInfos.Count; i++)
  62. {
  63. if (CurveInfos[i].BSLt > currTime)
  64. {
  65. int lindex = i - 1;
  66. if (lindex < 0)
  67. {
  68. CurveInfo endInfo = CurveInfos[i];
  69. Vector2 endPos = new Vector2((float)endInfo.t, (float)endInfo.v);
  70. return endPos;
  71. }
  72. c1 = CurveInfos[i - 1];
  73. c2 = CurveInfos[i];
  74. break;
  75. }
  76. }
  77. if (c2.t==0)
  78. {
  79. CurveInfo endInfo = CurveInfos[^1];
  80. Vector2 endPos = new Vector2(endInfo.t, endInfo.v);
  81. return endPos;
  82. }
  83. Vector2 pos1 = new Vector2((float)c1.t, (float)c1.v);
  84. Vector2 pos2 = new Vector2((float)c1.it, (float)c1.ot);
  85. Vector2 pos4 = new Vector2((float)c2.t, (float)c2.v);
  86. Vector2 pos3 = new Vector2((float)c2.it, (float)c2.ot);
  87. // float v11 = (float) c1.t;
  88. // float v12 = (float) c1.v;
  89. // float t11 = (float) c1.it;
  90. // float t12 = (float) c1.ot;
  91. //
  92. // float v21 = (float) c2.t;
  93. // float v22 = (float) c2.v;
  94. // float t21 = (float) c2.it;
  95. // float t22 = (float) c2.ot;
  96. Vector2 a = Vector2.Lerp(pos1, pos2, t);
  97. Vector2 b = Vector2.Lerp(pos2, pos3, t);
  98. Vector2 c = Vector2.Lerp(pos3, pos4, t);
  99. Vector2 d = Vector2.Lerp(a, b, t);
  100. Vector2 e = Vector2.Lerp(b, c, t);
  101. Vector2 f = Vector2.Lerp(d, e, t);
  102. return f;
  103. }
  104. private float BSL(float t)
  105. {
  106. CurveInfo c1 = default;
  107. CurveInfo c2 = default;
  108. for (int i = 0; i < CurveInfos.Count; i++)
  109. {
  110. if (CurveInfos[i].t > t)
  111. {
  112. int lindex = i - 1;
  113. if (lindex < 0)
  114. {
  115. return (float)CurveInfos[i].v;
  116. }
  117. c1 = CurveInfos[i - 1];
  118. c2 = CurveInfos[i];
  119. break;
  120. }
  121. }
  122. if (c2.t == 0)
  123. {
  124. return (float)CurveInfos[^1].v;
  125. }
  126. Vector2 pos1 = new Vector2((float)c1.t, (float)c1.v);
  127. Vector2 pos2 = new Vector2((float)c1.it, (float)c1.ot);
  128. Vector2 pos4 = new Vector2((float)c2.t, (float)c2.v);
  129. Vector2 pos3 = new Vector2((float)c2.it, (float)c2.ot);
  130. // float v11 = (float) c1.t;
  131. // float v12 = (float) c1.v;
  132. // float t11 = (float) c1.it;
  133. // float t12 = (float) c1.ot;
  134. //
  135. // float v21 = (float) c2.t;
  136. // float v22 = (float) c2.v;
  137. // float t21 = (float) c2.it;
  138. // float t22 = (float) c2.ot;
  139. Vector2 a = Vector2.Lerp(pos1, pos2, t);
  140. Vector2 b = Vector2.Lerp(pos2, pos3, t);
  141. Vector2 c = Vector2.Lerp(pos3, pos4, t);
  142. Vector2 d = Vector2.Lerp(a, b, t);
  143. Vector2 e = Vector2.Lerp(b, c, t);
  144. Vector2 f = Vector2.Lerp(d, e, t);
  145. return f.y;
  146. }
  147. private float HYT(float t)
  148. {
  149. CurveInfo c1 = default;
  150. CurveInfo c2 = default;
  151. for (int i = 0; i < CurveInfos.Count; i++)
  152. {
  153. if (CurveInfos[i].t > t)
  154. {
  155. int lindex = i - 1;
  156. if (lindex < 0)
  157. {
  158. return (float)CurveInfos[i].v;
  159. }
  160. c1 = CurveInfos[i - 1];
  161. c2 = CurveInfos[i];
  162. break;
  163. }
  164. }
  165. if (c2.t == 0)
  166. {
  167. return (float)CurveInfos[^1].v;
  168. }
  169. float ct = (float)c2.t - c1.t;
  170. t = (t - c1.t) / ct;
  171. float ot = c1.ot * ct;
  172. float it = c2.it * ct;
  173. float t2 = t * t;
  174. float t3 = t2 * t;
  175. float a = (2 * t3 - 3 * t2 + 1);
  176. float b = t3 - 2 * t2 + t;
  177. float c = t3 - t2;
  178. float d = -2 * t3 + 3 * t2;
  179. return a * c1.v + b * ot + c * it + d * c2.v;
  180. }
  181. }
  182. }