More sound stuff

This commit is contained in:
2025-12-13 20:30:54 +01:00
parent 42a32f5fe4
commit a2779ab31b
14 changed files with 286 additions and 49 deletions

View File

@@ -17,17 +17,24 @@ public class SoundManager : UdonSharpBehaviour
[SerializeField] private AudioClip pacFruit;
[SerializeField] private AudioClip pacCoin;
[SerializeField] private AudioClip pacGhostEat;
[SerializeField] private AudioClip pacGhost1;
[SerializeField] private AudioClip pacGhostBlue;
[SerializeField] private AudioClip pacGhostRetreat;
[SerializeField] private AudioClip pacExtraLife;
[SerializeField] private AudioClip siren0;
[SerializeField] private AudioClip siren1;
[SerializeField] private AudioClip siren2;
[SerializeField] private AudioClip siren3;
[SerializeField] private AudioClip siren4;
private AudioClip _nextDotSound;
private bool _isRetreating;
private bool _isBlue;
private bool _ghostRetreating;
private bool _ghostBlue;
private int _ghostSoundLevel;
private bool _currentlyPlayingSiren;
private bool _suppress;
public void Initialize()
{
Reset();
@@ -38,8 +45,12 @@ public class SoundManager : UdonSharpBehaviour
StopAllSound();
_nextDotSound = pacDot2;
_isRetreating = false;
_isBlue = false;
_ghostRetreating = false;
_ghostBlue = false;
_ghostSoundLevel = 0;
_currentlyPlayingSiren = false;
_suppress = true;
}
@@ -63,7 +74,7 @@ public class SoundManager : UdonSharpBehaviour
PlaySound(audioSourcePacMan, _nextDotSound);
_nextDotSound = _nextDotSound == pacDot1 ? pacDot2 : pacDot1;
}
public void PlayDeathSound()
{
PlaySound(audioSourcePacMan, pacDie1);
@@ -89,40 +100,21 @@ public class SoundManager : UdonSharpBehaviour
PlaySound(audioSourceExtraLife, pacExtraLife);
}
public void StartGhostSound(bool isBlue)
public void StartGhostSound()
{
Debug.Log($"Start ghost sound, isBlue: {isBlue}, _isRetreating: {_isRetreating}, _suppress: {_suppress}");
_isBlue = isBlue;
if (_isRetreating)
{
// Retreating overrides other ghost sound
return;
}
var sound = _isBlue ? pacGhostBlue : pacGhost1;
PlaySound(audioSourceGhosts, sound, true);
UpdateGhostSound();
}
public void StartGhostRetreat()
public void SetGhostBlue(bool isBlue)
{
_isRetreating = true;
PlaySound(audioSourceGhosts, pacGhostRetreat, true);
_ghostBlue = isBlue;
UpdateGhostSound();
}
public void EndGhostRetreat()
public void SetGhostRetreat(bool isRetreating)
{
Debug.Log("End ghost retreating");
if (!_isRetreating)
{
return;
}
_isRetreating = false;
StartGhostSound(_isBlue);
_ghostRetreating = isRetreating;
UpdateGhostSound();
}
public void StopAllSound()
@@ -132,6 +124,37 @@ public class SoundManager : UdonSharpBehaviour
// audioSourceExtraLife is not stopped on purpose, this sound is never interrupted
}
public void UpdatePelletCount(int pelletCount)
{
int level = 0;
if (pelletCount < 16)
{
level = 4;
}
else if (pelletCount < 32)
{
level = 3;
}
else if (pelletCount < 64)
{
level = 2;
}
else if (pelletCount < 220)
{
level = 1;
}
Debug.Log($"UpdatePelletCount: {pelletCount}, level: {level}");
if (_ghostSoundLevel >= level)
{
return;
}
_ghostSoundLevel = level;
UpdateGhostSound();
}
private void PlaySound(AudioSource audioSource, AudioClip audioClip, bool loop = false)
{
// Debug.Log($"PlaySound, audioSource: {audioSource}, audioClip: {audioClip}, loop: {loop}, suppress: {_suppress}");
@@ -139,7 +162,7 @@ public class SoundManager : UdonSharpBehaviour
{
return;
}
if (loop && audioSource.clip == audioClip && audioSource.isPlaying)
{
// Don't restart a looping sound
@@ -150,4 +173,76 @@ public class SoundManager : UdonSharpBehaviour
audioSource.Play();
audioSource.loop = loop;
}
private void SwitchSound(AudioSource audioSource, AudioClip audioClip, bool loop)
{
if (_suppress)
{
return;
}
if (audioSource.clip == audioClip)
{
// No need to switch
return;
}
if (!audioSource.isPlaying || audioSource.clip == null)
{
PlaySound(audioSource, audioClip, loop);
}
var newTimeSamples = (int)(audioSource.timeSamples / (double)audioSource.clip.samples * audioClip.samples);
audioSource.clip = audioClip;
audioSource.timeSamples = newTimeSamples;
audioSource.Play();
}
private void UpdateGhostSound()
{
if (_ghostRetreating)
{
PlaySound(audioSourceGhosts, pacGhostRetreat, true);
_currentlyPlayingSiren = false;
return;
}
if (_ghostBlue)
{
PlaySound(audioSourceGhosts, pacGhostBlue, true);
_currentlyPlayingSiren = false;
return;
}
var currentGhostLevelSound = GetSoundForGhostLevel(_ghostSoundLevel);
if (_currentlyPlayingSiren)
{
SwitchSound(audioSourceGhosts, currentGhostLevelSound, true);
}
else
{
PlaySound(audioSourceGhosts, currentGhostLevelSound, true);
}
_currentlyPlayingSiren = true;
}
private AudioClip GetSoundForGhostLevel(int ghostLevel)
{
switch (ghostLevel)
{
case 1:
return siren1;
case 2:
return siren2;
case 3:
return siren3;
case 4:
return siren4;
default:
return siren0;
};
}
}