123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- 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<AudioSource>();
- 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<PlayerProxy>());
- }
- }
- 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<Renderer>();
- if(render) render.enabled = false;
- Collider c = GetComponent<Collider>();
- if(c) c.enabled = false;
- Rigidbody rigid = GetComponent<Rigidbody>();
- 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<Rigidbody>();
- if(c_rigid) {
- c_rigid.useGravity = true;
- c_rigid.isKinematic = false;
- }
- MeshCollider mc = chunk.GetComponent<MeshCollider>();
- 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<ShatterTool>();
- //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
- */
- }
|