using System.Collections.Generic; using UnityEngine; namespace Gamekit2D { [RequireComponent(typeof(Rigidbody2D))] [RequireComponent(typeof(Collider2D))] public class Pushable : MonoBehaviour { private FMOD.Studio.EventInstance PushSound; private FMOD.Studio.EventInstance FallSound; FMOD.Studio.PLAYBACK_STATE PbState; private FMOD.Studio.EventInstance BobbleSound; FMOD.Studio.PLAYBACK_STATE PbState2; static ContactPoint2D[] s_ContactPointBuffer = new ContactPoint2D[16]; static Dictionary s_PushableCache = new Dictionary (); public Transform playerPushingRightPosition; public Transform playerPushingLeftPosition; public Transform pushablePosition; public AudioSource pushableAudioSource; public AudioClip startingPushClip; public AudioClip loopPushClip; public AudioClip endPushClip; public bool Grounded { get { return m_Grounded; } } protected SpriteRenderer m_SpriteRenderer; protected Rigidbody2D m_Rigidbody2D; protected bool m_Grounded; Collider2D[] m_WaterColliders; //StackTrace stackTrace = new StackTrace(); private float y; void Awake () { PushSound = FMODUnity.RuntimeManager.CreateInstance("event:/SFX/Interactables/Pushable"); FallSound = FMODUnity.RuntimeManager.CreateInstance("event:/SFX/Interactables/Box_Falling"); BobbleSound = FMODUnity.RuntimeManager.CreateInstance("event:/SFX/Interactables/Bobbling Create"); m_SpriteRenderer = GetComponent(); m_Rigidbody2D = GetComponent (); if (s_PushableCache.Count == 0) { Pushable[] pushables = FindObjectsOfType (); for (int i = 0; i < pushables.Length; i++) { Collider2D[] pushableColliders = pushables[i].GetComponents (); for (int j = 0; j < pushableColliders.Length; j++) { s_PushableCache.Add (pushableColliders[j], pushables[i]); } } } WaterArea[] waterAreas = FindObjectsOfType (); m_WaterColliders = new Collider2D[waterAreas.Length]; for (int i = 0; i < waterAreas.Length; i++) { m_WaterColliders[i] = waterAreas[i].GetComponent (); } } void FixedUpdate () { FallSound.getPlaybackState(out PbState); BobbleSound.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(transform, GetComponent())); Vector2 velocity = m_Rigidbody2D.velocity; velocity.x = 0f; m_Rigidbody2D.velocity = velocity; CheckGrounded(); for (int i = 0; i < m_WaterColliders.Length; i++) { if (m_Rigidbody2D.IsTouching (m_WaterColliders[i])) { m_Rigidbody2D.constraints |= RigidbodyConstraints2D.FreezePositionX; if (PbState != FMOD.Studio.PLAYBACK_STATE.STOPPED) FallSound.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT); BobbleSound.getPlaybackState(out PbState2); if (PbState2 != FMOD.Studio.PLAYBACK_STATE.PLAYING) BobbleSound.start(); y = velocity.y; if (y < 0) y *= -1; //BobbleSound.setParameterValue("Bobble", y); < ----Use this if you're using A version of FMOD before V2.00.00 BobbleSound.setParameterByName("Bobble", y, false); } else { if (!m_Grounded && velocity.y < -3) { //FallSound.setParameterValue("Velocity", velocity.y); < ----Use this if you're using A version of FMOD before V2.00.00 FallSound.setParameterByName("Velocity", velocity.y, false); if (PbState != FMOD.Studio.PLAYBACK_STATE.PLAYING) { FMODUnity.RuntimeManager.AttachInstanceToGameObject(FallSound, transform, GetComponent()); FallSound.start(); } } } } } private void OnDestroy() { PushSound.release(); FallSound.release(); BobbleSound.stop(FMOD.Studio.STOP_MODE.IMMEDIATE); BobbleSound.release(); } public void StartPushing() { pushableAudioSource.loop = false; pushableAudioSource.clip = startingPushClip; pushableAudioSource.Play(); Debug.Log("START"); FMODUnity.RuntimeManager.AttachInstanceToGameObject(PushSound, transform, GetComponent()); PushSound.start(); } public void EndPushing() { pushableAudioSource.loop = false; pushableAudioSource.clip = endPushClip; pushableAudioSource.Play(); Debug.Log("END"); PushSound.triggerCue(); } public void Move (Vector2 movement) { m_Rigidbody2D.position = m_Rigidbody2D.position + movement; //stackTrace = new StackTrace(); //print("stackTrace !! " + stackTrace.GetFrame(1).GetMethod().Name); if (!pushableAudioSource.isPlaying) { pushableAudioSource.clip = loopPushClip; pushableAudioSource.loop = true; pushableAudioSource.Play(); } } protected void CheckGrounded() { m_Grounded = false; int count = m_Rigidbody2D.GetContacts(s_ContactPointBuffer); for(int i = 0; i < count; ++i) { if(s_ContactPointBuffer[i].normal.y > 0.9f) { m_Grounded = true; Pushable pushable; if(s_PushableCache.TryGetValue (s_ContactPointBuffer[i].collider, out pushable)) { //if it is grounded on another pushable, we ensure that it is drawn after the one under, so it appear on top. m_SpriteRenderer.sortingOrder = pushable.m_SpriteRenderer.sortingOrder + 1; } } } } } }