Files
2023-10-10 17:13:58 +11:00

144 lines
3.7 KiB
C#
Executable File

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public enum Sound {
MenuLow,
MenuHigh,
Accelerate,
Decelerate,
Strafe,
Damage, // noise pop
Respawn, // noise pop
BoostAccelerate,
BoostDecelerate,
BoostStrafe,
BoostShield,
BoostGenerate,
BoostAvailable,
BoostFull,
BoostDrain,
}
public class ShipAudio : MonoBehaviour {
// Match these two up
[SerializeField]
Sound[] powerUnitSounds;
[SerializeField]
AudioClip[] powerUnitClips;
[SerializeField]
AudioClip[] noiseClips;
Dictionary<Sound, AudioClip> sounds = new();
AudioSource srcShipAccel;
AudioSource srcShipDecel;
AudioSource srcShipStrafe;
AudioSource src;
SpaceRaceInput input;
ShipControls ship;
public void MuteShip() {
srcShipDecel.Stop();
srcShipAccel.Stop();
srcShipStrafe.Stop();
}
public void UnmuteShip() {
srcShipDecel.Play();
srcShipAccel.Play();
srcShipStrafe.Play();
}
void Awake() {
for (int i = 0; i < powerUnitClips.Length; i++) {
sounds.Add(powerUnitSounds[i], powerUnitClips[i]);
}
ship = GetComponentInParent<ShipControls>();
src = gameObject.AddComponent<AudioSource>();
srcShipAccel = gameObject.AddComponent<AudioSource>();
srcShipAccel.clip = sounds[Sound.Accelerate];
srcShipAccel.loop = true;
srcShipAccel.volume = 0f;
srcShipAccel.Play();
srcShipDecel = gameObject.AddComponent<AudioSource>();
srcShipDecel.clip = sounds[Sound.Decelerate];
srcShipDecel.loop = true;
srcShipDecel.volume = 0f;
srcShipDecel.Play();
srcShipStrafe = gameObject.AddComponent<AudioSource>();
srcShipStrafe.clip = sounds[Sound.Strafe];
srcShipStrafe.loop = true;
srcShipStrafe.volume = 0f;
srcShipStrafe.Play();
RegisterEvents();
}
void Update() {
srcShipAccel.pitch = Math.Min(0.5f, (1f / 500f) * ship.Speed);
srcShipDecel.pitch = Math.Min(0.5f, (1f / 500f) * ship.Speed);
srcShipStrafe.pitch = Math.Min(0.5f, (1f / 500f) * ship.Speed);
// Super fucking annoying
// stops all the clicks and pops of starting / stopping the powerUnitClips
// 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<float>() > 0;
bool decelerating = input.ship.decelerate.ReadValue<float>() > 0;
bool strafing = input.ship.strafe.ReadValue<Vector2>().magnitude > 0;
if (accelerating) {
srcShipAccel.volume = Math.Min(1f, srcShipAccel.volume + volumeDelta);
} else {
srcShipAccel.volume = Math.Max(0f, srcShipAccel.volume - decelDelta);
}
if (decelerating) {
srcShipDecel.volume = Math.Min(1f, srcShipDecel.volume + volumeDelta);
} else {
srcShipDecel.volume = Math.Max(0f, srcShipDecel.volume - decelDelta);
}
if (strafing) {
srcShipStrafe.volume = Math.Min(1f, srcShipStrafe.volume + volumeDelta);
} else {
srcShipStrafe.volume = Math.Max(0f, srcShipStrafe.volume - decelDelta);
}
}
public void PlayNoise() {
int noiseIndex = UnityEngine.Random.Range(0, noiseClips.Length);
src.pitch = UnityEngine.Random.Range(2f, 8f);
src.PlayOneShot(noiseClips[noiseIndex]);
}
void RegisterEvents() {
if (input == null)
input = new SpaceRaceInput();
input.ship.Enable();
}
}