SoundManager sync

This commit is contained in:
2026-06-22 16:03:46 +02:00
parent 8a6f1b482b
commit d6b870de79
2 changed files with 46 additions and 29 deletions

View File

@@ -408,6 +408,8 @@ namespace Marro.PacManUdon
ghostManager.CollectSyncedData(data, ref index, eventType); ghostManager.CollectSyncedData(data, ref index, eventType);
pacMan.CollectSyncedData(data, ref index, eventType); pacMan.CollectSyncedData(data, ref index, eventType);
soundManager.CollectSyncedData(data, ref index, eventType);
} }
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType) public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
@@ -447,6 +449,8 @@ namespace Marro.PacManUdon
pacMan.WriteSyncedData(data, ref index, eventType); pacMan.WriteSyncedData(data, ref index, eventType);
soundManager.WriteSyncedData(data, ref index, eventType);
return true; return true;
} }

View File

@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using Marro.PacManUdon; using Marro.PacManUdon;
using UnityEngine; using UnityEngine;
@@ -27,13 +28,13 @@ public class SoundManager : SyncedObject
[SerializeField] private AudioClip siren3; [SerializeField] private AudioClip siren3;
[SerializeField] private AudioClip siren4; [SerializeField] private AudioClip siren4;
private AudioClip _nextDotSound; private bool alternatePelletSound;
private bool _ghostRetreating; private bool ghostRetreating;
private bool _ghostBlue; private bool ghostBlue;
private int _ghostSoundLevel; private int ghostSoundLevel;
private bool _currentlyPlayingSiren; private bool currentlyPlayingSiren;
private bool _suppress; private bool suppress;
public void Initialize() public void Initialize()
{ {
@@ -44,19 +45,19 @@ public class SoundManager : SyncedObject
{ {
StopAllSound(); StopAllSound();
_nextDotSound = pacDot2; alternatePelletSound = false;
_ghostRetreating = false; ghostRetreating = false;
_ghostBlue = false; ghostBlue = false;
_ghostSoundLevel = 0; ghostSoundLevel = 0;
_currentlyPlayingSiren = false; currentlyPlayingSiren = false;
_suppress = true; suppress = true;
} }
public void SuppressSound(bool suppress) public void SuppressSound(bool suppress)
{ {
_suppress = suppress; this.suppress = suppress;
if (suppress) if (suppress)
{ {
@@ -76,8 +77,8 @@ public class SoundManager : SyncedObject
public void PlayPelletSound() public void PlayPelletSound()
{ {
PlaySound(audioSourcePacMan, _nextDotSound); PlaySound(audioSourcePacMan, alternatePelletSound ? pacDot2 : pacDot1);
_nextDotSound = _nextDotSound == pacDot1 ? pacDot2 : pacDot1; alternatePelletSound = !alternatePelletSound;
} }
public void PlayDeathSound() public void PlayDeathSound()
@@ -112,13 +113,13 @@ public class SoundManager : SyncedObject
public void SetGhostBlue(bool isBlue) public void SetGhostBlue(bool isBlue)
{ {
_ghostBlue = isBlue; ghostBlue = isBlue;
UpdateGhostSound(); UpdateGhostSound();
} }
public void SetGhostRetreat(bool isRetreating) public void SetGhostRetreat(bool isRetreating)
{ {
_ghostRetreating = isRetreating; ghostRetreating = isRetreating;
UpdateGhostSound(); UpdateGhostSound();
} }
@@ -151,19 +152,19 @@ public class SoundManager : SyncedObject
// Debug.Log($"UpdatePelletCount: {pelletCount}, level: {level}"); // Debug.Log($"UpdatePelletCount: {pelletCount}, level: {level}");
if (_ghostSoundLevel >= level) if (ghostSoundLevel >= level)
{ {
return; return;
} }
_ghostSoundLevel = level; ghostSoundLevel = level;
UpdateGhostSound(); UpdateGhostSound();
} }
private void PlaySound(AudioSource audioSource, AudioClip audioClip, bool loop = false) private void PlaySound(AudioSource audioSource, AudioClip audioClip, bool loop = false)
{ {
// Debug.Log($"PlaySound, audioSource: {audioSource}, audioClip: {audioClip}, loop: {loop}, suppress: {_suppress}"); // Debug.Log($"PlaySound, audioSource: {audioSource}, audioClip: {audioClip}, loop: {loop}, suppress: {_suppress}");
if (_suppress) if (suppress)
{ {
return; return;
} }
@@ -181,7 +182,7 @@ public class SoundManager : SyncedObject
private void SwitchSound(AudioSource audioSource, AudioClip audioClip, bool loop) private void SwitchSound(AudioSource audioSource, AudioClip audioClip, bool loop)
{ {
if (_suppress) if (suppress)
{ {
return; return;
} }
@@ -207,22 +208,22 @@ public class SoundManager : SyncedObject
private void UpdateGhostSound() private void UpdateGhostSound()
{ {
if (_ghostRetreating) if (ghostRetreating)
{ {
PlaySound(audioSourceGhosts, pacGhostRetreat, true); PlaySound(audioSourceGhosts, pacGhostRetreat, true);
_currentlyPlayingSiren = false; currentlyPlayingSiren = false;
return; return;
} }
if (_ghostBlue) if (ghostBlue)
{ {
PlaySound(audioSourceGhosts, pacGhostBlue, true); PlaySound(audioSourceGhosts, pacGhostBlue, true);
_currentlyPlayingSiren = false; currentlyPlayingSiren = false;
return; return;
} }
var currentGhostLevelSound = GetSoundForGhostLevel(_ghostSoundLevel); var currentGhostLevelSound = GetSoundForGhostLevel(ghostSoundLevel);
if (_currentlyPlayingSiren) if (currentlyPlayingSiren)
{ {
SwitchSound(audioSourceGhosts, currentGhostLevelSound, true); SwitchSound(audioSourceGhosts, currentGhostLevelSound, true);
} }
@@ -231,7 +232,7 @@ public class SoundManager : SyncedObject
PlaySound(audioSourceGhosts, currentGhostLevelSound, true); PlaySound(audioSourceGhosts, currentGhostLevelSound, true);
} }
_currentlyPlayingSiren = true; currentlyPlayingSiren = true;
} }
private AudioClip GetSoundForGhostLevel(int ghostLevel) private AudioClip GetSoundForGhostLevel(int ghostLevel)
@@ -253,11 +254,23 @@ public class SoundManager : SyncedObject
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType) public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{ {
data.Append(alternatePelletSound, ref index);
data.Append(ghostRetreating, ref index);
data.Append(ghostBlue, ref index);
data.AppendAsByte(ghostSoundLevel, ref index);
data.Append(currentlyPlayingSiren, ref index);
} }
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType) public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{ {
alternatePelletSound = data.ReadBool(ref index);
ghostRetreating = data.ReadBool(ref index);
ghostBlue = data.ReadBool(ref index);
ghostSoundLevel = data.ReadByte(ref index);
currentlyPlayingSiren = data.ReadBool(ref index);
UpdateGhostSound();
return true; return true;
} }
} }