DestructAnim.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using UnityEngine;
  2. using URandom = UnityEngine.Random;
  3. using System.Collections;
  4. using UnityEngine.UI;
  5. using System;
  6. using James.Base;
  7. using JamesCore;
  8. using James.Sound;
  9. using James.Util;
  10. public class DestructAnim : AnimationPlayer {
  11. public bool HiddenScarInTheMomentOfHit = false;
  12. public GameObject[] Chunks;
  13. public GameObject[] HidingObjs; //list of the objects that will be hidden after the crush.
  14. public float ExplosionForce = 200; //force added to every chunk of the broken object.
  15. public float ChunksRotation = 20; //rotation force added to every chunk when it explodes.
  16. public bool DestroyAftertime = true; //if true, then chunks will be destroyed after time.
  17. public float time = 15; //time before chunks will be destroyed from the scene.
  18. public GameObject FX;
  19. public bool AutoDestroy = true; //if true, then object will be automatically break after after "AutoDestTime" since game start.
  20. public float AutoDestTime = 2; //Auto destruction time (counts from game start).
  21. private AudioSource mAudioSource;
  22. void Start () {
  23. if(AutoDestroy){
  24. Invoke("Crushing", AutoDestTime);
  25. }
  26. mAudioSource = GetComponent<AudioSource>();
  27. if(mAudioSource) mAudioSource.pitch = URandom.Range (0.7f, 1.1f);
  28. if(HidingObjs.Length !=0){
  29. foreach(GameObject hidingObj in HidingObjs){
  30. hidingObj.SetActive(true);
  31. }
  32. }
  33. }
  34. private MuteType muteType {
  35. get {
  36. short mute = playerProxy.MuteType;
  37. MuteType muteType = MuteType.None;
  38. bool defined = Enum.IsDefined(typeof(MuteType), (int)mute);
  39. if(defined) {
  40. muteType = (MuteType) mute;
  41. }
  42. return muteType;
  43. }
  44. }
  45. private PlayerProxy _pproxy;
  46. private PlayerProxy playerProxy {
  47. get {
  48. return _pproxy ?? (_pproxy = Core.DCore.getNetworkProxy<PlayerProxy>());
  49. }
  50. }
  51. void Crushing(){
  52. if(HidingObjs.Length !=0){
  53. foreach(GameObject hidingObj in HidingObjs){
  54. hidingObj.SetActive(false);
  55. }
  56. }
  57. if(FX)
  58. FX.SetActive(true);
  59. if(mAudioSource && !muteType.check(MuteType.SoundEff)) {
  60. mAudioSource.Play();
  61. }
  62. Renderer render = GetComponent<Renderer>();
  63. if(render) render.enabled = false;
  64. Collider c = GetComponent<Collider>();
  65. if(c) c.enabled = false;
  66. Rigidbody rigid = GetComponent<Rigidbody>();
  67. if(rigid) rigid.isKinematic = true;
  68. Transform flyDirTrans = targetObj.m_shape.transform;
  69. if (targetObj.FatherObj != null)
  70. {
  71. flyDirTrans = targetObj.FatherObj.m_shape.transform;
  72. if (targetObj.FatherObj.FatherObj != null)
  73. {
  74. flyDirTrans = targetObj.FatherObj.FatherObj.m_shape.transform;
  75. }
  76. }
  77. if(Chunks != null) {
  78. int len = Chunks.Length;
  79. for(int i = 0; i < len; ++ i) {
  80. GameObject chunk = Chunks[i];
  81. chunk.SetActive(true);
  82. Rigidbody c_rigid = chunk.GetComponent<Rigidbody>();
  83. if(c_rigid) {
  84. c_rigid.useGravity = true;
  85. c_rigid.isKinematic = false;
  86. }
  87. MeshCollider mc = chunk.GetComponent<MeshCollider>();
  88. if(mc)mc.enabled = true;
  89. Vector3 explore_vector = flyDirTrans.forward + URandom.Range(0.2f, 0.5f) * flyDirTrans.up + URandom.Range(-0.5f, 0.5f) * flyDirTrans.right;
  90. c_rigid.AddForce(explore_vector * ExplosionForce);
  91. c_rigid.AddRelativeTorque(flyDirTrans.forward * -ChunksRotation*URandom.Range(-5f, 5f));
  92. c_rigid.AddRelativeTorque(flyDirTrans.right * -ChunksRotation*URandom.Range(-5f, 5f));
  93. }
  94. }
  95. if(DestroyAftertime){
  96. Invoke("DestructObject", time);
  97. }
  98. }
  99. void DestructObject(){
  100. Destroy(gameObject);
  101. }
  102. protected override float hit(ShootResult sr, ShootData data, int life = 0)
  103. {
  104. Crushing();
  105. //Shatter(sr.hitRealPos);
  106. return time;
  107. }
  108. protected override float death(ShootResult sr, ShootData data)
  109. {
  110. Crushing();
  111. //Shatter(sr.hitRealPos);
  112. return time;
  113. }
  114. private void Shatter(Vector3 pos)
  115. {
  116. //ShatterTool st = gameObject.GetComponent<ShatterTool>();
  117. //if (st != null)
  118. //{
  119. // st.Shatter(pos, transform.position);
  120. //}
  121. }
  122. /*
  123. #if UNITY_EDITOR
  124. void OnGUI()
  125. {
  126. if(GUI.Button(new Rect(0, 0, 100, 100), "Hit"))
  127. {
  128. hit(null, null);
  129. }
  130. }
  131. #endif
  132. */
  133. }