DestructableTarget.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using James.Util;
  4. public class DestructableTarget : AnimationPlayer
  5. {
  6. #region fields
  7. // After having exploded, the fruit disappears after this many seconds.
  8. // Set this parameter to 0 if you don't want the fruit to disappear.
  9. public float destroyAfterSeconds = 4;
  10. // Determines the force of the explosion if you call Explode without parameters.
  11. public float defaultExplosionForce = 300;
  12. public GameObject splashEffect;
  13. float stateTime;
  14. new Transform transform;
  15. GameObject partsOuterRoot;
  16. GameObject partsInnerRoot;
  17. Transform partsCommonRootTransform;
  18. AudioSource[] splashSounds;
  19. bool hasExploded;
  20. Dictionary<Transform, Vector3> origPositions = new Dictionary<Transform, Vector3>();
  21. Dictionary<Transform, Quaternion> origRotations = new Dictionary<Transform, Quaternion>();
  22. #endregion
  23. public Collider extra_simple_c;
  24. public Collider[] ExtraCollider;
  25. public Rigidbody ExtraBody;
  26. public Transform goldCollison;
  27. public AudioSource AwakeSound;
  28. void Awake()
  29. {
  30. Messenger.MarkAsPermanent(MsgType.PLAY_TARGET_AWAKE_SOUND);
  31. Messenger.AddListener(MsgType.PLAY_TARGET_AWAKE_SOUND, PlayAwakeSound);//打开关卡UI
  32. Messenger.MarkAsPermanent(MsgType.STOP_TARGET_AWAKE_SOUND);
  33. Messenger.AddListener(MsgType.STOP_TARGET_AWAKE_SOUND, StopSound);//停止靶子音效
  34. transform = base.transform;
  35. var partsOuterTrans = transform.Find("Parts outer");
  36. if(partsOuterTrans != null) partsOuterRoot = partsOuterTrans.gameObject;
  37. var partsInnerTrans = transform.Find("Parts inner");
  38. if (partsInnerTrans != null) partsInnerRoot = partsInnerTrans.gameObject;
  39. partsCommonRootTransform = transform.Find("Parts common");
  40. splashSounds = GetComponentsInChildren<AudioSource>(true);
  41. if (partsInnerRoot != null)
  42. {
  43. foreach (Transform partTransform in partsInnerRoot.transform)
  44. {
  45. origPositions[partTransform] = partTransform.localPosition;
  46. origRotations[partTransform] = partTransform.localRotation;
  47. }
  48. }
  49. if ( partsCommonRootTransform != null )
  50. foreach ( Transform partTransform in partsCommonRootTransform )
  51. {
  52. origPositions[ partTransform ] = partTransform.localPosition;
  53. origRotations[ partTransform ] = partTransform.localRotation;
  54. }
  55. //钱袋子,钱箱,金猪,打碎后金币掉落的地板碰撞,放置到地板位置好让金币反弹
  56. if(goldCollison != null)
  57. goldCollison.position = Vector3.zero;
  58. }
  59. private void OnDestroy()
  60. {
  61. Messenger.RemoveListener(MsgType.PLAY_TARGET_AWAKE_SOUND, PlayAwakeSound);
  62. Messenger.RemoveListener(MsgType.STOP_TARGET_AWAKE_SOUND, StopSound);
  63. }
  64. private void PlayAwakeSound()
  65. {
  66. if (SoundController.instance.IsSoundOn() && AwakeSound != null)
  67. {
  68. AwakeSound.Play();
  69. }
  70. }
  71. private void StopSound()
  72. {
  73. if (SoundController.instance.IsSoundOn() && AwakeSound != null)
  74. {
  75. AwakeSound.Stop();
  76. }
  77. }
  78. void Deactivate()
  79. {
  80. // Destroy ( gameObject );
  81. }
  82. override protected float hit(ShootResult sr, ShootData data, int life = 0)
  83. {
  84. Explode(new Vector3(0,0, defaultExplosionForce));
  85. return 10;
  86. }
  87. override protected float death(ShootResult sr, ShootData data)
  88. {
  89. Explode(new Vector3(0,0, defaultExplosionForce));
  90. return 10;
  91. }
  92. public void Explode ( Vector3? forceVector = null )
  93. {
  94. if ( hasExploded )
  95. return;
  96. if (splashSounds.Length > 0 && SoundController.instance.IsSoundOn())
  97. {
  98. if (AwakeSound != null)
  99. {
  100. AwakeSound.Stop();
  101. }
  102. splashSounds[UnityEngine.Random.Range(0, splashSounds.Length)].Play();
  103. }
  104. float force = forceVector.HasValue ? forceVector.Value.magnitude : defaultExplosionForce;
  105. Vector3 hitForce = forceVector ?? Vector3.zero;
  106. //*** Split into parts
  107. {
  108. var curRenderer = GetComponent<Renderer>();
  109. if (curRenderer != null) curRenderer.enabled = false;
  110. if(partsOuterRoot != null) partsOuterRoot.SetActive(false);
  111. Collider self = GetComponent<Collider>();
  112. if (self) self.enabled = false;
  113. if(extra_simple_c) extra_simple_c.enabled = false;
  114. partsInnerRoot.SetActive( true );
  115. Transform flyDirTrans = targetObj.m_shape.transform;
  116. if (targetObj.FatherObj != null)
  117. {
  118. flyDirTrans = targetObj.FatherObj.m_shape.transform;
  119. }
  120. foreach ( Transform partTransform in origPositions.Keys )
  121. {
  122. MeshCollider meshCollider = partTransform.GetComponent<Collider>() as MeshCollider;
  123. if ( meshCollider != null )
  124. meshCollider.convex = true;
  125. Rigidbody currRigidbody = partTransform.GetComponent<Rigidbody>();
  126. currRigidbody.isKinematic = false;
  127. currRigidbody.velocity = Vector3.zero;
  128. currRigidbody.angularVelocity = Vector3.zero;
  129. // Vector3 explore_vector = targetObj.m_shape.transform.forward + Random.Range(0.2f, 0.5f) * targetObj.m_shape.transform.up + Random.Range(-0.5f, 0.5f) * targetObj.m_shape.transform.right;
  130. Vector3 explore_vector = flyDirTrans.forward + Random.Range(0.2f, 0.5f) * targetObj.m_shape.transform.up + Random.Range(-0.5f, 0.5f) * targetObj.m_shape.transform.right;
  131. currRigidbody.AddForce(explore_vector * force);
  132. currRigidbody.AddRelativeTorque(targetObj.m_shape.transform.forward * -20 * Random.Range(-5f, 5f));
  133. }
  134. }
  135. if (splashEffect) {
  136. splashEffect.transform.rotation = Quaternion.identity;
  137. splashEffect.transform.position = transform.position;
  138. #if !UNITY_2018_1_OR_NEWER
  139. ParticleEmitter[] pe = splashEffect.GetComponentsInChildren<ParticleEmitter>();
  140. if(pe != null) {
  141. int len = pe.Length;
  142. for(int i = 0; i < len; ++ i) {
  143. ParticleEmitter emitter = pe[i];
  144. emitter.ClearParticles();
  145. emitter.worldVelocity = forceVector.HasValue ? forceVector.Value/force : Vector3.zero;
  146. emitter.Emit();
  147. }
  148. }
  149. #else
  150. ParticleSystem ps = splashEffect.GetComponent<ParticleSystem>();
  151. if (ps != null)
  152. {
  153. ps.Play();
  154. }
  155. #endif
  156. }
  157. if ( destroyAfterSeconds > 0 )
  158. Invoke("Deactivate", destroyAfterSeconds);
  159. hasExploded = true;
  160. }
  161. public void FallDown()
  162. {
  163. if (ExtraBody != null && ExtraCollider.Length > 0)
  164. {
  165. for (int i = 0; i < ExtraCollider.Length; ++i)
  166. {
  167. ExtraCollider[i].enabled = true;
  168. }
  169. ExtraBody.isKinematic = false;
  170. ExtraBody.useGravity = true;
  171. }
  172. }
  173. public void Reset()
  174. {
  175. foreach ( Transform partTransform in origPositions.Keys )
  176. {
  177. partTransform.localPosition = origPositions[ partTransform ];
  178. partTransform.localRotation = origRotations[ partTransform ];
  179. }
  180. if ( partsCommonRootTransform != null )
  181. foreach ( Transform partTransform in partsCommonRootTransform )
  182. partTransform.GetComponent<Rigidbody>().isKinematic = true;
  183. partsInnerRoot.SetActive( false );
  184. GetComponent<Renderer>().enabled = true;
  185. Collider c = GetComponent<Collider>();
  186. if(c) c.enabled = true;
  187. hasExploded = false;
  188. }
  189. // #if UNITY_EDITOR
  190. // void OnGUI()
  191. // {
  192. // if(GUI.Button(new Rect(0, 0, 100, 100), "Explore")) {
  193. // Explode(new Vector3(0,0, defaultExplosionForce));
  194. // }
  195. // if(GUI.Button(new Rect(0, 100, 100, 100), "Reset")) {
  196. // Reset();
  197. // }
  198. // }
  199. // #endif
  200. }