XftCameraShakeComp.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. [ExecuteInEditMode]
  6. public class XftCameraShakeComp : MonoBehaviour
  7. {
  8. public Spring PositionSpring
  9. {
  10. get
  11. {
  12. return this.mPositionSpring;
  13. }
  14. set
  15. {
  16. this.mPositionSpring = value;
  17. }
  18. }
  19. public Spring RotationSpring
  20. {
  21. get
  22. {
  23. return this.mRotationSpring;
  24. }
  25. set
  26. {
  27. this.mRotationSpring = value;
  28. }
  29. }
  30. public XftEventComponent Client
  31. {
  32. get
  33. {
  34. return this.m_client;
  35. }
  36. }
  37. private void Awake()
  38. {
  39. base.enabled = false;
  40. }
  41. public void Reset(XftEventComponent client)
  42. {
  43. this.m_client = client;
  44. if (this.m_client.CameraShakeType == XCameraShakeType.Spring)
  45. {
  46. if (this.PositionSpring != null && !this.CheckDone())
  47. {
  48. base.transform.localPosition = this.mOriPosition;
  49. base.transform.localRotation = Quaternion.Euler(this.mOriRotation);
  50. }
  51. this.PositionSpring = new Spring(client.transform, Spring.TransformType.Position);
  52. this.PositionSpring.MinVelocity = 1E-05f;
  53. this.RotationSpring = new Spring(client.transform, Spring.TransformType.Rotation);
  54. this.RotationSpring.MinVelocity = 1E-05f;
  55. this.PositionSpring.Stiffness = new Vector3(this.m_client.PositionStifness, this.m_client.PositionStifness, this.m_client.PositionStifness);
  56. this.PositionSpring.Damping = Vector3.one - new Vector3(this.m_client.PositionDamping, this.m_client.PositionDamping, this.m_client.PositionDamping);
  57. this.RotationSpring.Stiffness = new Vector3(this.m_client.RotationStiffness, this.m_client.RotationStiffness, this.m_client.RotationStiffness);
  58. this.RotationSpring.Damping = Vector3.one - new Vector3(this.m_client.RotationDamping, this.m_client.RotationDamping, this.m_client.RotationDamping);
  59. this.m_client.transform.localPosition = base.transform.localPosition;
  60. this.m_client.transform.localRotation = base.transform.localRotation;
  61. this.PositionSpring.RefreshTransformType();
  62. this.RotationSpring.RefreshTransformType();
  63. this.m_earthQuakeTimeTemp = this.m_client.EarthQuakeTime;
  64. this.mLastPosition = base.transform.localPosition;
  65. this.mLastRotation = base.transform.localRotation.eulerAngles;
  66. this.mOriPosition = base.transform.localPosition;
  67. this.mOriRotation = base.transform.localRotation.eulerAngles;
  68. }
  69. this.Update();
  70. }
  71. private void UpdateEarthQuake()
  72. {
  73. if (this.m_client == null || !this.m_client.UseEarthQuake || this.m_earthQuakeTimeTemp <= 0f || !this.EarthQuakeToggled || this.m_client.ElapsedTime > this.m_client.EarthQuakeTime)
  74. {
  75. return;
  76. }
  77. this.m_earthQuakeTimeTemp -= 0.0166f * (60f * Time.deltaTime);
  78. float num;
  79. if (this.m_client.EarthQuakeMagTye == MAGTYPE.Fixed)
  80. {
  81. num = this.m_client.EarthQuakeMagnitude;
  82. }
  83. else if (this.m_client.EarthQuakeMagTye == MAGTYPE.Curve_OBSOLETE)
  84. {
  85. num = this.m_client.EarthQuakeMagCurve.Evaluate(this.m_client.ElapsedTime);
  86. }
  87. else
  88. {
  89. num = this.m_client.EarthQuakeMagCurveX.Evaluate(this.m_client.ElapsedTime);
  90. }
  91. Vector3 force = Vector3.Scale(XftSmoothRandom.GetVector3Centered(1f), new Vector3(num, 0f, num)) * Mathf.Min(this.m_earthQuakeTimeTemp, 1f);
  92. float num2 = 0f;
  93. if (UnityEngine.Random.value < 0.3f)
  94. {
  95. num2 = UnityEngine.Random.Range(0f, num * 0.35f) * Mathf.Min(this.m_earthQuakeTimeTemp, 1f);
  96. if (this.PositionSpring.State.y >= this.PositionSpring.RestState.y)
  97. {
  98. num2 = -num2;
  99. }
  100. }
  101. this.PositionSpring.AddForce(force);
  102. this.RotationSpring.AddForce(new Vector3(0f, 0f, -force.x * 2f) * this.m_client.EarthQuakeCameraRollFactor);
  103. this.PositionSpring.AddForce(new Vector3(0f, num2, 0f));
  104. }
  105. public bool CheckDone()
  106. {
  107. if (this.m_client.CameraShakeType == XCameraShakeType.Spring)
  108. {
  109. if (this.PositionSpring == null || this.RotationSpring == null)
  110. {
  111. return true;
  112. }
  113. if (this.PositionSpring.Done && this.RotationSpring.Done)
  114. {
  115. return true;
  116. }
  117. }
  118. else if (this.m_client.ElapsedTime > this.m_client.ShakeCurveTime)
  119. {
  120. return true;
  121. }
  122. return false;
  123. }
  124. private void UpdateCurve()
  125. {
  126. float time = this.m_client.ElapsedTime / this.m_client.ShakeCurveTime;
  127. Vector3 a = this.mOriPosition + this.m_client.PositionForce * (this.m_client.PositionCurve.Evaluate(time) * 2f - 1f);
  128. Vector3 a2 = this.mOriRotation + this.m_client.RotationForce * (this.m_client.RotationCurve.Evaluate(time) * 2f - 1f);
  129. Vector3 b = a - this.mLastPosition;
  130. Vector3 b2 = a2 - this.mLastRotation;
  131. base.transform.localPosition += b;
  132. Vector3 eulerAngles = base.transform.localRotation.eulerAngles;
  133. base.transform.localRotation = Quaternion.Euler(eulerAngles + b2);
  134. this.mLastPosition = a;
  135. this.mLastRotation = a2;
  136. }
  137. private void UpdateSpring()
  138. {
  139. if (this.PositionSpring == null || this.RotationSpring == null)
  140. {
  141. return;
  142. }
  143. this.UpdateEarthQuake();
  144. this.PositionSpring.FixedUpdate();
  145. this.RotationSpring.FixedUpdate();
  146. Vector3 b = this.m_client.transform.localPosition - this.mLastPosition;
  147. Vector3 b2 = this.m_client.transform.localEulerAngles - this.mLastRotation;
  148. base.transform.localPosition += b;
  149. Vector3 eulerAngles = base.transform.localRotation.eulerAngles;
  150. base.transform.localRotation = Quaternion.Euler(eulerAngles + b2);
  151. this.mLastPosition = this.m_client.transform.localPosition;
  152. this.mLastRotation = this.m_client.transform.localEulerAngles;
  153. }
  154. private void Update()
  155. {
  156. if (this.m_client == null)
  157. {
  158. return;
  159. }
  160. if (this.m_client.CameraShakeType == XCameraShakeType.Curve)
  161. {
  162. this.UpdateCurve();
  163. }
  164. else
  165. {
  166. this.UpdateSpring();
  167. }
  168. if (this.CheckDone())
  169. {
  170. base.enabled = false;
  171. return;
  172. }
  173. }
  174. protected Spring mPositionSpring;
  175. protected Spring mRotationSpring;
  176. protected XftEventComponent m_client;
  177. public bool EarthQuakeToggled;
  178. protected float m_earthQuakeTimeTemp;
  179. protected Vector3 mLastPosition;
  180. protected Vector3 mLastRotation;
  181. protected Vector3 mOriPosition;
  182. protected Vector3 mOriRotation;
  183. }
  184. }