using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public enum Sound { MenuLow, MenuHigh, Accelerate, Decelerate, Strafe, Damage, Respawn, BoostAcceleration, BoostDeceleration, BoostStrafe, BoostShield, BoostGenerate, BoostAvailable, BoostFull, BoostDrain, } public class ShipAudio : MonoBehaviour { // Match these two up [SerializeField] Sound[] soundNames; [SerializeField] AudioClip[] clips; Dictionary sounds = new(); AudioSource srcAccel; AudioSource srcDecel; AudioSource srcStrafe; SpaceRaceInput input; ShipControls ship; void Awake() { for (int i = 0; i < clips.Length; i++) { sounds.Add(soundNames[i], clips[i]); } ship = GetComponentInParent(); srcAccel = gameObject.AddComponent(); srcAccel.clip = sounds[Sound.Accelerate]; srcAccel.loop = true; srcAccel.volume = 0f; srcAccel.Play(); srcDecel = gameObject.AddComponent(); srcDecel.clip = sounds[Sound.Decelerate]; srcDecel.loop = true; srcDecel.volume = 0f; srcDecel.Play(); srcStrafe = gameObject.AddComponent(); srcStrafe.clip = sounds[Sound.Strafe]; srcStrafe.loop = true; srcStrafe.volume = 0f; srcStrafe.Play(); RegisterEvents(); } void Update() { srcAccel.pitch = Math.Min(1f, (1f / 500f) * ship.Speed); srcDecel.pitch = Math.Min(1f, (1f / 500f) * ship.Speed); srcStrafe.pitch = Math.Min(1f, (1f / 500f) * ship.Speed); // Super fucking annoying // stops all the clicks and pops of starting / stopping the clips // 0.125s to get max volume // == 8x per second... float volumeDelta = Time.deltaTime * 8; float decelDelta = (1 / ship.Speed); bool accelerating = input.ship.accelerate.ReadValue() > 0; bool decelerating = input.ship.decelerate.ReadValue() > 0; bool strafing = input.ship.strafe.ReadValue().magnitude > 0; if (accelerating) { srcAccel.volume = Math.Min(1f, srcAccel.volume + volumeDelta); } else { srcAccel.volume = Math.Max(0f, srcAccel.volume - decelDelta); } if (decelerating) { srcDecel.volume = Math.Min(1f, srcDecel.volume + volumeDelta); } else { srcDecel.volume = Math.Max(0f, srcDecel.volume - decelDelta); } if (strafing) { srcStrafe.volume = Math.Min(1f, srcStrafe.volume + volumeDelta); } else { srcStrafe.volume = Math.Max(0f, srcStrafe.volume - decelDelta); } } // public void PlaySound(Sound s) { // AudioSource src = sounds[s]; // src.Play(0); // } void RegisterEvents() { if (input == null) input = new SpaceRaceInput(); input.ship.Enable(); // input.ship.accelerate.started += OnAccelerateStart; // input.ship.accelerate.canceled += OnAccelerateCancel; // input.ship.decelerate.started += OnDecelerateStart; // input.ship.decelerate.canceled += OnDecelerateCancel; // input.ship.strafe.started += OnStrafeStart; // input.ship.strafe.canceled += OnStrafeCancel; // these we only care about the initial press // input.ship.respawn.started += OnRespawn; // input.ship.quit.started += OnQuit; // input.ship.powerUnit1.started += OnPowerUnit1; // input.ship.powerUnit2.started += OnPowerUnit2; // input.ship.powerUnit3.started += OnPowerUnit3; // input.ship.powerUnit4.started += OnPowerUnit4; } // void OnDestroy() { // input.ship.accelerate.started -= OnAccelerateStart; // input.ship.accelerate.canceled -= OnAccelerateCancel; // input.ship.decelerate.started -= OnDecelerateStart; // input.ship.decelerate.canceled -= OnDecelerateCancel; // input.ship.strafe.started -= OnStrafeStart; // input.ship.strafe.canceled -= OnStrafeCancel; // // these we only care about the initial press // input.ship.respawn.started -= OnRespawn; // input.ship.quit.started -= OnQuit; // input.ship.powerUnit1.started -= OnPowerUnit1; // input.ship.powerUnit2.started -= OnPowerUnit2; // input.ship.powerUnit3.started -= OnPowerUnit3; // input.ship.powerUnit4.started -= OnPowerUnit4; // } // void OnAcceleratePerformed(InputAction.CallbackContext _ctx) { // Debug.Log(_ctx.duration); // } // void OnAccelerateStart(InputAction.CallbackContext _ctx) { // srcAccel.Play(); // } // void OnAccelerateCancel(InputAction.CallbackContext _ctx) { // srcAccel.Stop(); // } // void OnDecelerateStart(InputAction.CallbackContext _ctx) { // srcDecel.Play(); // } // void OnDecelerateCancel(InputAction.CallbackContext _ctx) { // srcDecel.Stop(); // } // void OnStrafeStart(InputAction.CallbackContext _ctx) { // srcStrafe.Play(); // } // void OnStrafeCancel(InputAction.CallbackContext _ctx) { // srcStrafe.Stop(); // } }