Spring.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class Spring
  6. {
  7. public Spring(Transform transform, Spring.TransformType modifier)
  8. {
  9. this.m_transform = transform;
  10. this.Modifier = modifier;
  11. this.RefreshTransformType();
  12. }
  13. public Transform Transform
  14. {
  15. set
  16. {
  17. this.m_transform = value;
  18. this.RefreshTransformType();
  19. }
  20. }
  21. public bool Done
  22. {
  23. get
  24. {
  25. return this.m_done;
  26. }
  27. set
  28. {
  29. this.m_done = value;
  30. }
  31. }
  32. public void FixedUpdate()
  33. {
  34. if (this.m_velocityFadeInEndTime > Time.time)
  35. {
  36. this.m_velocityFadeInCap = Mathf.Clamp01(1f - (this.m_velocityFadeInEndTime - Time.time) / this.m_velocityFadeInLength);
  37. }
  38. else
  39. {
  40. this.m_velocityFadeInCap = 1f;
  41. }
  42. if (this.Modifier != this.m_currentTransformType)
  43. {
  44. this.RefreshTransformType();
  45. }
  46. this.m_transformFunction();
  47. }
  48. private void Position()
  49. {
  50. this.Calculate();
  51. this.m_transform.localPosition = this.State;
  52. }
  53. private void PositionAdditive()
  54. {
  55. this.Calculate();
  56. this.m_transform.localPosition += this.State;
  57. }
  58. private void Rotation()
  59. {
  60. this.Calculate();
  61. this.m_transform.localEulerAngles = this.State;
  62. }
  63. private void RotationAdditive()
  64. {
  65. this.Calculate();
  66. this.m_transform.localEulerAngles += this.State;
  67. }
  68. private void Scale()
  69. {
  70. this.Calculate();
  71. this.m_transform.localScale = this.State;
  72. }
  73. private void ScaleAdditive()
  74. {
  75. this.Calculate();
  76. this.m_transform.localScale += this.State;
  77. }
  78. public void RefreshTransformType()
  79. {
  80. switch (this.Modifier)
  81. {
  82. case Spring.TransformType.Position:
  83. this.State = this.m_transform.localPosition;
  84. this.m_transformFunction = new Spring.TransformDelegate(this.Position);
  85. break;
  86. case Spring.TransformType.PositionAdditive:
  87. this.State = this.m_transform.localPosition;
  88. this.m_transformFunction = new Spring.TransformDelegate(this.PositionAdditive);
  89. break;
  90. case Spring.TransformType.Rotation:
  91. this.State = this.m_transform.localEulerAngles;
  92. this.m_transformFunction = new Spring.TransformDelegate(this.Rotation);
  93. break;
  94. case Spring.TransformType.RotationAdditive:
  95. this.State = this.m_transform.localEulerAngles;
  96. this.m_transformFunction = new Spring.TransformDelegate(this.RotationAdditive);
  97. break;
  98. case Spring.TransformType.Scale:
  99. this.State = this.m_transform.localScale;
  100. this.m_transformFunction = new Spring.TransformDelegate(this.Scale);
  101. break;
  102. case Spring.TransformType.ScaleAdditive:
  103. this.State = this.m_transform.localScale;
  104. this.m_transformFunction = new Spring.TransformDelegate(this.ScaleAdditive);
  105. break;
  106. }
  107. this.m_currentTransformType = this.Modifier;
  108. this.RestState = this.State;
  109. }
  110. protected void Calculate()
  111. {
  112. if (this.State == this.RestState)
  113. {
  114. this.m_done = true;
  115. return;
  116. }
  117. Vector3 a = this.RestState - this.State;
  118. this.m_velocity += Vector3.Scale(a, this.Stiffness);
  119. this.m_velocity = Vector3.Scale(this.m_velocity, this.Damping);
  120. this.m_velocity = Vector3.ClampMagnitude(this.m_velocity, this.MaxVelocity);
  121. if (Mathf.Abs(this.m_velocity.sqrMagnitude) > this.MinVelocity * this.MinVelocity)
  122. {
  123. this.Move();
  124. }
  125. else
  126. {
  127. this.Reset();
  128. }
  129. }
  130. public void AddForce(Vector3 force)
  131. {
  132. force *= this.m_velocityFadeInCap;
  133. this.m_velocity += force;
  134. this.m_velocity = Vector3.ClampMagnitude(this.m_velocity, this.MaxVelocity);
  135. this.Move();
  136. this.m_done = false;
  137. }
  138. public void AddForce(float x, float y, float z)
  139. {
  140. this.AddForce(new Vector3(x, y, z));
  141. }
  142. protected void Move()
  143. {
  144. this.State += this.m_velocity;
  145. this.State = new Vector3(Mathf.Clamp(this.State.x, this.MinState.x, this.MaxState.x), Mathf.Clamp(this.State.y, this.MinState.y, this.MaxState.y), Mathf.Clamp(this.State.z, this.MinState.z, this.MaxState.z));
  146. }
  147. public void Reset()
  148. {
  149. this.m_velocity = Vector3.zero;
  150. this.State = this.RestState;
  151. this.m_done = true;
  152. }
  153. public void Stop()
  154. {
  155. this.m_velocity = Vector3.zero;
  156. }
  157. public void ForceVelocityFadeIn(float seconds)
  158. {
  159. this.m_velocityFadeInLength = seconds;
  160. this.m_velocityFadeInEndTime = Time.time + seconds;
  161. this.m_velocityFadeInCap = 0f;
  162. }
  163. public Spring.TransformType Modifier;
  164. protected Spring.TransformDelegate m_transformFunction;
  165. public Vector3 State = Vector3.zero;
  166. protected Spring.TransformType m_currentTransformType;
  167. protected Vector3 m_velocity = Vector3.zero;
  168. public Vector3 RestState = Vector3.zero;
  169. public Vector3 Stiffness = new Vector3(0.5f, 0.5f, 0.5f);
  170. public Vector3 Damping = new Vector3(0.75f, 0.75f, 0.75f);
  171. protected float m_velocityFadeInCap = 1f;
  172. protected float m_velocityFadeInEndTime;
  173. protected float m_velocityFadeInLength;
  174. public float MaxVelocity = 10000f;
  175. public float MinVelocity = 1E-07f;
  176. public Vector3 MaxState = new Vector3(10000f, 10000f, 10000f);
  177. public Vector3 MinState = new Vector3(-10000f, -10000f, -10000f);
  178. protected Transform m_transform;
  179. protected bool m_done;
  180. public enum TransformType
  181. {
  182. Position,
  183. PositionAdditive,
  184. Rotation,
  185. RotationAdditive,
  186. Scale,
  187. ScaleAdditive
  188. }
  189. protected delegate void TransformDelegate();
  190. }
  191. }