Files
PacManUdon/Assets/Scripts/SoundManager.cs
2025-12-11 22:43:40 +01:00

139 lines
3.0 KiB
C#

using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
public class SoundManager : UdonSharpBehaviour
{
[SerializeField] private AudioSource audioSourcePlayer;
[SerializeField] private AudioSource audioSourceGhosts;
[SerializeField] private AudioClip pacStart;
[SerializeField] private AudioClip pacDot1;
[SerializeField] private AudioClip pacDot2;
[SerializeField] private AudioClip pacDie1;
[SerializeField] private AudioClip pacFruit;
[SerializeField] private AudioClip pacCoin;
[SerializeField] private AudioClip pacGhostEat;
[SerializeField] private AudioClip pacGhost1;
[SerializeField] private AudioClip pacGhostBlue;
[SerializeField] private AudioClip pacGhostRetreat;
private AudioClip _nextDotSound;
private bool _isRetreating;
private bool _isBlue;
private bool _suppress;
public void Initialize()
{
Reset();
}
public void Reset()
{
_nextDotSound = pacDot2;
_isRetreating = false;
_isBlue = false;
_suppress = false;
}
public void SuppressSound(bool suppress)
{
_suppress = suppress;
if (suppress)
{
StopAllSound();
}
}
public void PlayGameStartSound()
{
PlaySound(audioSourcePlayer, pacStart);
}
public void PlayPelletSound()
{
PlaySound(audioSourcePlayer, _nextDotSound);
_nextDotSound = _nextDotSound == pacDot1 ? pacDot2 : pacDot1;
}
public void PlayDeathSound()
{
PlaySound(audioSourcePlayer, pacDie1);
}
public void PlayFruitSound()
{
PlaySound(audioSourcePlayer, pacFruit);
}
public void PlayCoinSound()
{
PlaySound(audioSourcePlayer, pacCoin);
}
public void PlayGhostEatSound()
{
PlaySound(audioSourcePlayer, pacGhostEat);
}
public void StartGhostSound(bool isBlue)
{
_isBlue = isBlue;
if (_isRetreating)
{
// Retreating overrides other ghost sound
return;
}
var sound = _isBlue ? pacGhostBlue : pacGhost1;
PlaySound(audioSourceGhosts, sound, true);
}
public void StartGhostRetreat()
{
_isRetreating = true;
PlaySound(audioSourceGhosts, pacGhostRetreat, true);
}
public void EndGhostRetreat()
{
if (!_isRetreating)
{
return;
}
_isRetreating = false;
StartGhostSound(_isBlue);
}
public void StopAllSound()
{
audioSourcePlayer.Stop();
audioSourceGhosts.Stop();
}
private void PlaySound(AudioSource audioSource, AudioClip audioClip, bool loop = false)
{
if (_suppress)
{
return;
}
if (loop && audioSource.clip == audioClip)
{
// Don't restart a looping sound
return;
}
audioSource.clip = audioClip;
audioSource.Play();
audioSource.loop = loop;
}
}