This commit is contained in:
ntr committed 2023-09-26 20:37:15 +10:00
1 parent c6ed13597f
commit 294701f69a
24 files changed
+407 -90

No files matched your search

+183
View File
@@ -0,0 +1,183 @@
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<Sound, AudioClip> 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<ShipControls>();
srcAccel = gameObject.AddComponent<AudioSource>();
srcAccel.clip = sounds[Sound.Accelerate];
srcAccel.loop = true;
srcAccel.volume = 0f;
srcAccel.Play();
srcDecel = gameObject.AddComponent<AudioSource>();
srcDecel.clip = sounds[Sound.Decelerate];
srcDecel.loop = true;
srcDecel.volume = 0f;
srcDecel.Play();
srcStrafe = gameObject.AddComponent<AudioSource>();
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 / 8000;
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) {
srcAccel.volume = Math.Max(1f, srcAccel.volume + volumeDelta);
} else {
srcAccel.volume = Math.Min(0f, srcAccel.volume - volumeDelta);
}
if (decelerating) {
srcDecel.volume = Math.Max(1f, srcDecel.volume + volumeDelta);
} else {
srcDecel.volume = Math.Min(0f, srcDecel.volume - volumeDelta);
}
if (strafing) {
srcStrafe.volume = Math.Max(1f, srcStrafe.volume + volumeDelta);
} else {
srcStrafe.volume = Math.Min(0f, srcStrafe.volume - volumeDelta);
}
}
// 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();
// }
}