123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using UnityEngine;
- using System.Collections.Generic;
- using James.Util;
- public class DestructableTarget : AnimationPlayer
- {
- #region fields
-
- // After having exploded, the fruit disappears after this many seconds.
- // Set this parameter to 0 if you don't want the fruit to disappear.
- public float destroyAfterSeconds = 4;
-
- // Determines the force of the explosion if you call Explode without parameters.
- public float defaultExplosionForce = 300;
- public GameObject splashEffect;
-
- float stateTime;
- new Transform transform;
- GameObject partsOuterRoot;
- GameObject partsInnerRoot;
- Transform partsCommonRootTransform;
-
- AudioSource[] splashSounds;
- bool hasExploded;
-
- Dictionary<Transform, Vector3> origPositions = new Dictionary<Transform, Vector3>();
- Dictionary<Transform, Quaternion> origRotations = new Dictionary<Transform, Quaternion>();
-
- #endregion
-
- public Collider extra_simple_c;
- public Collider[] ExtraCollider;
- public Rigidbody ExtraBody;
- public Transform goldCollison;
- public AudioSource AwakeSound;
- void Awake()
- {
- Messenger.MarkAsPermanent(MsgType.PLAY_TARGET_AWAKE_SOUND);
- Messenger.AddListener(MsgType.PLAY_TARGET_AWAKE_SOUND, PlayAwakeSound);//打开关卡UI
- Messenger.MarkAsPermanent(MsgType.STOP_TARGET_AWAKE_SOUND);
- Messenger.AddListener(MsgType.STOP_TARGET_AWAKE_SOUND, StopSound);//停止靶子音效
- transform = base.transform;
- var partsOuterTrans = transform.Find("Parts outer");
- if(partsOuterTrans != null) partsOuterRoot = partsOuterTrans.gameObject;
- var partsInnerTrans = transform.Find("Parts inner");
- if (partsInnerTrans != null) partsInnerRoot = partsInnerTrans.gameObject;
- partsCommonRootTransform = transform.Find("Parts common");
-
- splashSounds = GetComponentsInChildren<AudioSource>(true);
- if (partsInnerRoot != null)
- {
- foreach (Transform partTransform in partsInnerRoot.transform)
- {
- origPositions[partTransform] = partTransform.localPosition;
- origRotations[partTransform] = partTransform.localRotation;
- }
- }
- if ( partsCommonRootTransform != null )
- foreach ( Transform partTransform in partsCommonRootTransform )
- {
- origPositions[ partTransform ] = partTransform.localPosition;
- origRotations[ partTransform ] = partTransform.localRotation;
- }
- //钱袋子,钱箱,金猪,打碎后金币掉落的地板碰撞,放置到地板位置好让金币反弹
- if(goldCollison != null)
- goldCollison.position = Vector3.zero;
- }
- private void OnDestroy()
- {
- Messenger.RemoveListener(MsgType.PLAY_TARGET_AWAKE_SOUND, PlayAwakeSound);
- Messenger.RemoveListener(MsgType.STOP_TARGET_AWAKE_SOUND, StopSound);
- }
- private void PlayAwakeSound()
- {
- if (SoundController.instance.IsSoundOn() && AwakeSound != null)
- {
- AwakeSound.Play();
- }
- }
- private void StopSound()
- {
- if (SoundController.instance.IsSoundOn() && AwakeSound != null)
- {
- AwakeSound.Stop();
- }
- }
- void Deactivate()
- {
- // Destroy ( gameObject );
- }
-
- override protected float hit(ShootResult sr, ShootData data, int life = 0)
- {
- Explode(new Vector3(0,0, defaultExplosionForce));
- return 10;
- }
-
- override protected float death(ShootResult sr, ShootData data)
- {
- Explode(new Vector3(0,0, defaultExplosionForce));
- return 10;
- }
- public void Explode ( Vector3? forceVector = null )
- {
- if ( hasExploded )
- return;
- if (splashSounds.Length > 0 && SoundController.instance.IsSoundOn())
- {
- if (AwakeSound != null)
- {
- AwakeSound.Stop();
- }
- splashSounds[UnityEngine.Random.Range(0, splashSounds.Length)].Play();
- }
-
- float force = forceVector.HasValue ? forceVector.Value.magnitude : defaultExplosionForce;
- Vector3 hitForce = forceVector ?? Vector3.zero;
-
- //*** Split into parts
- {
- var curRenderer = GetComponent<Renderer>();
- if (curRenderer != null) curRenderer.enabled = false;
- if(partsOuterRoot != null) partsOuterRoot.SetActive(false);
- Collider self = GetComponent<Collider>();
- if (self) self.enabled = false;
- if(extra_simple_c) extra_simple_c.enabled = false;
- partsInnerRoot.SetActive( true );
- Transform flyDirTrans = targetObj.m_shape.transform;
- if (targetObj.FatherObj != null)
- {
- flyDirTrans = targetObj.FatherObj.m_shape.transform;
- }
- foreach ( Transform partTransform in origPositions.Keys )
- {
- MeshCollider meshCollider = partTransform.GetComponent<Collider>() as MeshCollider;
- if ( meshCollider != null )
- meshCollider.convex = true;
- Rigidbody currRigidbody = partTransform.GetComponent<Rigidbody>();
- currRigidbody.isKinematic = false;
- currRigidbody.velocity = Vector3.zero;
- currRigidbody.angularVelocity = Vector3.zero;
- // 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;
- 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;
- currRigidbody.AddForce(explore_vector * force);
- currRigidbody.AddRelativeTorque(targetObj.m_shape.transform.forward * -20 * Random.Range(-5f, 5f));
- }
- }
- if (splashEffect) {
- splashEffect.transform.rotation = Quaternion.identity;
- splashEffect.transform.position = transform.position;
- #if !UNITY_2018_1_OR_NEWER
- ParticleEmitter[] pe = splashEffect.GetComponentsInChildren<ParticleEmitter>();
- if(pe != null) {
- int len = pe.Length;
- for(int i = 0; i < len; ++ i) {
- ParticleEmitter emitter = pe[i];
- emitter.ClearParticles();
- emitter.worldVelocity = forceVector.HasValue ? forceVector.Value/force : Vector3.zero;
- emitter.Emit();
- }
- }
- #else
- ParticleSystem ps = splashEffect.GetComponent<ParticleSystem>();
- if (ps != null)
- {
- ps.Play();
- }
- #endif
- }
- if ( destroyAfterSeconds > 0 )
- Invoke("Deactivate", destroyAfterSeconds);
-
- hasExploded = true;
- }
- public void FallDown()
- {
- if (ExtraBody != null && ExtraCollider.Length > 0)
- {
- for (int i = 0; i < ExtraCollider.Length; ++i)
- {
- ExtraCollider[i].enabled = true;
- }
- ExtraBody.isKinematic = false;
- ExtraBody.useGravity = true;
- }
- }
-
-
-
- public void Reset()
- {
- foreach ( Transform partTransform in origPositions.Keys )
- {
- partTransform.localPosition = origPositions[ partTransform ];
- partTransform.localRotation = origRotations[ partTransform ];
- }
-
- if ( partsCommonRootTransform != null )
- foreach ( Transform partTransform in partsCommonRootTransform )
- partTransform.GetComponent<Rigidbody>().isKinematic = true;
-
- partsInnerRoot.SetActive( false );
- GetComponent<Renderer>().enabled = true;
- Collider c = GetComponent<Collider>();
- if(c) c.enabled = true;
- hasExploded = false;
- }
-
- // #if UNITY_EDITOR
-
- // void OnGUI()
- // {
- // if(GUI.Button(new Rect(0, 0, 100, 100), "Explore")) {
- // Explode(new Vector3(0,0, defaultExplosionForce));
- // }
- // if(GUI.Button(new Rect(0, 100, 100, 100), "Reset")) {
- // Reset();
- // }
- // }
- // #endif
- }
|