using UnityEngine; using URandom = UnityEngine.Random; using System.Collections; using UnityEngine.UI; using System; using James.Base; using JamesCore; using James.Sound; using James.Util; public class DestructAnim : AnimationPlayer { public bool HiddenScarInTheMomentOfHit = false; public GameObject[] Chunks; public GameObject[] HidingObjs; //list of the objects that will be hidden after the crush. public float ExplosionForce = 200; //force added to every chunk of the broken object. public float ChunksRotation = 20; //rotation force added to every chunk when it explodes. public bool DestroyAftertime = true; //if true, then chunks will be destroyed after time. public float time = 15; //time before chunks will be destroyed from the scene. public GameObject FX; public bool AutoDestroy = true; //if true, then object will be automatically break after after "AutoDestTime" since game start. public float AutoDestTime = 2; //Auto destruction time (counts from game start). private AudioSource mAudioSource; void Start () { if(AutoDestroy){ Invoke("Crushing", AutoDestTime); } mAudioSource = GetComponent(); if(mAudioSource) mAudioSource.pitch = URandom.Range (0.7f, 1.1f); if(HidingObjs.Length !=0){ foreach(GameObject hidingObj in HidingObjs){ hidingObj.SetActive(true); } } } private MuteType muteType { get { short mute = playerProxy.MuteType; MuteType muteType = MuteType.None; bool defined = Enum.IsDefined(typeof(MuteType), (int)mute); if(defined) { muteType = (MuteType) mute; } return muteType; } } private PlayerProxy _pproxy; private PlayerProxy playerProxy { get { return _pproxy ?? (_pproxy = Core.DCore.getNetworkProxy()); } } void Crushing(){ if(HidingObjs.Length !=0){ foreach(GameObject hidingObj in HidingObjs){ hidingObj.SetActive(false); } } if(FX) FX.SetActive(true); if(mAudioSource && !muteType.check(MuteType.SoundEff)) { mAudioSource.Play(); } Renderer render = GetComponent(); if(render) render.enabled = false; Collider c = GetComponent(); if(c) c.enabled = false; Rigidbody rigid = GetComponent(); if(rigid) rigid.isKinematic = true; Transform flyDirTrans = targetObj.m_shape.transform; if (targetObj.FatherObj != null) { flyDirTrans = targetObj.FatherObj.m_shape.transform; if (targetObj.FatherObj.FatherObj != null) { flyDirTrans = targetObj.FatherObj.FatherObj.m_shape.transform; } } if(Chunks != null) { int len = Chunks.Length; for(int i = 0; i < len; ++ i) { GameObject chunk = Chunks[i]; chunk.SetActive(true); Rigidbody c_rigid = chunk.GetComponent(); if(c_rigid) { c_rigid.useGravity = true; c_rigid.isKinematic = false; } MeshCollider mc = chunk.GetComponent(); if(mc)mc.enabled = true; Vector3 explore_vector = flyDirTrans.forward + URandom.Range(0.2f, 0.5f) * flyDirTrans.up + URandom.Range(-0.5f, 0.5f) * flyDirTrans.right; c_rigid.AddForce(explore_vector * ExplosionForce); c_rigid.AddRelativeTorque(flyDirTrans.forward * -ChunksRotation*URandom.Range(-5f, 5f)); c_rigid.AddRelativeTorque(flyDirTrans.right * -ChunksRotation*URandom.Range(-5f, 5f)); } } if(DestroyAftertime){ Invoke("DestructObject", time); } } void DestructObject(){ Destroy(gameObject); } protected override float hit(ShootResult sr, ShootData data, int life = 0) { Crushing(); //Shatter(sr.hitRealPos); return time; } protected override float death(ShootResult sr, ShootData data) { Crushing(); //Shatter(sr.hitRealPos); return time; } private void Shatter(Vector3 pos) { //ShatterTool st = gameObject.GetComponent(); //if (st != null) //{ // st.Shatter(pos, transform.position); //} } /* #if UNITY_EDITOR void OnGUI() { if(GUI.Button(new Rect(0, 0, 100, 100), "Hit")) { hit(null, null); } } #endif */ }