quantum-space-buddies/QSB/Audio/QSBPlayerAudioController.cs

102 lines
2.9 KiB
C#
Raw Normal View History

2022-10-05 04:00:03 +00:00
using QSB.PlayerBodySetup.Remote;
using QSB.Utility;
2022-08-27 18:44:52 +00:00
using UnityEngine;
2021-12-14 22:53:53 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.Audio;
2022-08-27 18:44:52 +00:00
[UsedInUnityProject]
2022-03-03 03:46:33 +00:00
public class QSBPlayerAudioController : MonoBehaviour
2021-12-14 22:53:53 +00:00
{
2022-03-03 03:46:33 +00:00
public OWAudioSource _oneShotExternalSource;
public OWAudioSource _repairToolSource;
2022-10-05 04:00:03 +00:00
public OWAudioSource _damageAudioSource;
private AudioManager _audioManager;
public void Start()
{
_audioManager = Locator.GetAudioManager();
2022-10-05 04:15:45 +00:00
// TODO: This should be done in the Unity project
2022-10-05 04:00:03 +00:00
var damageAudio = new GameObject("DamageAudioSource");
damageAudio.SetActive(false);
damageAudio.transform.SetParent(transform, false);
damageAudio.transform.localPosition = Vector3.zero;
_damageAudioSource = damageAudio.AddComponent<OWAudioSource>();
_damageAudioSource._audioSource = damageAudio.GetAddComponent<AudioSource>();
_damageAudioSource.SetTrack(_repairToolSource.GetTrack());
_damageAudioSource.spatialBlend = 1f;
_damageAudioSource.gameObject.GetAddComponent<QSBDopplerFixer>();
damageAudio.SetActive(true);
}
2021-12-14 22:53:53 +00:00
2022-03-03 03:46:33 +00:00
public void PlayEquipTool()
=> _oneShotExternalSource?.PlayOneShot(AudioType.ToolTranslatorEquip);
2021-12-14 22:53:53 +00:00
2022-03-03 03:46:33 +00:00
public void PlayUnequipTool()
=> _oneShotExternalSource?.PlayOneShot(AudioType.ToolTranslatorUnequip);
2021-12-14 22:53:53 +00:00
2022-03-03 03:46:33 +00:00
public void PlayTurnOnFlashlight()
=> _oneShotExternalSource?.PlayOneShot(AudioType.ToolFlashlightOn);
2021-12-14 22:56:54 +00:00
2022-03-03 03:46:33 +00:00
public void PlayTurnOffFlashlight()
=> _oneShotExternalSource?.PlayOneShot(AudioType.ToolFlashlightOff);
public void PlayWearSuit()
=> PlayOneShot(AudioType.PlayerSuitWearSuit);
public void PlayRemoveSuit()
=> PlayOneShot(AudioType.PlayerSuitRemoveSuit);
2022-08-27 22:07:50 +00:00
public void PlayOneShot(AudioType audioType, float pitch = 1f, float volume = 1f)
{
if (_oneShotExternalSource)
{
_oneShotExternalSource.pitch = pitch;
_oneShotExternalSource.PlayOneShot(audioType, volume);
}
}
public void PlayFootstep(AudioType audioType, float pitch) =>
PlayOneShot(audioType, pitch, 0.7f);
public void OnJump(float pitch) =>
PlayOneShot(AudioType.MovementJump, pitch, 0.7f);
2022-10-05 04:00:03 +00:00
private void StartHazardDamage(HazardVolume.HazardType latestHazardType)
{
var type = AudioType.EnterVolumeDamageHeat_LP;
if (latestHazardType == HazardVolume.HazardType.DARKMATTER)
{
type = AudioType.EnterVolumeDamageGhostfire_LP;
}
else if (latestHazardType == HazardVolume.HazardType.FIRE)
{
type = AudioType.EnterVolumeDamageFire_LP;
}
_damageAudioSource.clip = _audioManager.GetSingleAudioClip(type, true);
_damageAudioSource.Stop();
_damageAudioSource.FadeIn(0.2f, true, true, 1f);
}
private void EndHazardDamage()
{
if (_damageAudioSource.isPlaying)
{
_damageAudioSource.FadeOut(0.5f, OWAudioSource.FadeOutCompleteAction.STOP, 0f);
}
}
public void SetHazardDamage(HazardVolume.HazardType latestHazardType)
{
if (latestHazardType == HazardVolume.HazardType.NONE)
{
EndHazardDamage();
}
else
{
StartHazardDamage(latestHazardType);
}
}
}