ok ok
This commit is contained in:
1 parent
c6ed13597f
commit
294701f69a
24 files changed
+407
-90
No files matched your search
@@ -1,56 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
public enum Sound {
|
||||
MenuLow,
|
||||
MenuHigh,
|
||||
|
||||
Accelerate,
|
||||
Decelerate,
|
||||
Strafe,
|
||||
|
||||
Damage,
|
||||
Respawn,
|
||||
|
||||
BoostAcceleration,
|
||||
BoostDeceleration,
|
||||
BoostStrafe,
|
||||
BoostShield,
|
||||
|
||||
BoostGenerate,
|
||||
BoostAvailable,
|
||||
BoostFull,
|
||||
BoostDrain,
|
||||
}
|
||||
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class Audio : MonoBehaviour {
|
||||
[SerializeField]
|
||||
Sound[] soundNames;
|
||||
|
||||
[SerializeField]
|
||||
AudioClip[] clips;
|
||||
|
||||
Dictionary<Sound, AudioClip> sounds = new();
|
||||
|
||||
AudioSource source;
|
||||
|
||||
void Start() {
|
||||
source = GetComponent<AudioSource>();
|
||||
|
||||
for (int i = 0; i < clips.Length; i++) {
|
||||
sounds.Add(soundNames[i], clips[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void PlaySound(Sound s) {
|
||||
AudioClip clip;
|
||||
bool found = sounds.TryGetValue(s, out clip);
|
||||
if (clip) {
|
||||
source.clip = clip;
|
||||
source.Play(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+183
@@ -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();
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9bcaa1cfcea9cae4bb92bdc96cfbb4d2
|
||||
guid: 566ffe93221add0489d9a9a01670b93a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -47,6 +47,10 @@ public class ShipControls : MonoBehaviour {
|
||||
|
||||
public float Speed => rigidBody.velocity.magnitude;
|
||||
|
||||
// used for audio
|
||||
public bool Accelerating => input.ship.accelerate.ReadValue<float>() > 0;
|
||||
public bool Decelerating => input.ship.decelerate.ReadValue<float>() > 0;
|
||||
|
||||
void Start() {
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
rigidBody = GetComponent<Rigidbody>();
|
||||
|
||||
@@ -29,7 +29,7 @@ public class MenuControls : MonoBehaviour {
|
||||
ControlsUI controlsUI;
|
||||
|
||||
[SerializeField]
|
||||
Audio audio;
|
||||
ShipAudio audio;
|
||||
|
||||
SpaceRaceInput input;
|
||||
|
||||
|
||||
Reference in new issue
Block a user