123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 loyalsoft. All rights reserved.
- // Homepage: http://www.game7000.com/
- // Feedback: http://www.game7000.com/
- //------------------------------------------------------------
- using System.Collections.Generic;
- namespace GameFramework.Sound
- {
- internal sealed partial class SoundManager : GameFrameworkModule, ISoundManager
- {
- /// <summary>
- /// 声音组。
- /// </summary>
- private sealed class SoundGroup : ISoundGroup
- {
- private readonly string m_Name;
- private readonly ISoundGroupHelper m_SoundGroupHelper;
- private readonly List<SoundAgent> m_SoundAgents;
- private bool m_AvoidBeingReplacedBySamePriority;
- private bool m_Mute;
- private float m_Volume;
- /// <summary>
- /// 初始化声音组的新实例。
- /// </summary>
- /// <param name="name">声音组名称。</param>
- /// <param name="soundGroupHelper">声音组辅助器。</param>
- public SoundGroup(string name, ISoundGroupHelper soundGroupHelper)
- {
- if (string.IsNullOrEmpty(name))
- {
- throw new GameFrameworkException("Sound group name is invalid.");
- }
- if (soundGroupHelper == null)
- {
- throw new GameFrameworkException("Sound group helper is invalid.");
- }
- m_Name = name;
- m_SoundGroupHelper = soundGroupHelper;
- m_SoundAgents = new List<SoundAgent>();
- }
- /// <summary>
- /// 获取声音组名称。
- /// </summary>
- public string Name
- {
- get
- {
- return m_Name;
- }
- }
- /// <summary>
- /// 获取声音代理数。
- /// </summary>
- public int SoundAgentCount
- {
- get
- {
- return m_SoundAgents.Count;
- }
- }
- /// <summary>
- /// 获取或设置声音组中的声音是否避免被同优先级声音替换。
- /// </summary>
- public bool AvoidBeingReplacedBySamePriority
- {
- get
- {
- return m_AvoidBeingReplacedBySamePriority;
- }
- set
- {
- m_AvoidBeingReplacedBySamePriority = value;
- }
- }
- /// <summary>
- /// 获取或设置声音组静音。
- /// </summary>
- public bool Mute
- {
- get
- {
- return m_Mute;
- }
- set
- {
- m_Mute = value;
- foreach (SoundAgent soundAgent in m_SoundAgents)
- {
- soundAgent.RefreshMute();
- }
- }
- }
- /// <summary>
- /// 获取或设置声音组音量。
- /// </summary>
- public float Volume
- {
- get
- {
- return m_Volume;
- }
- set
- {
- m_Volume = value;
- foreach (SoundAgent soundAgent in m_SoundAgents)
- {
- soundAgent.RefreshVolume();
- }
- }
- }
- /// <summary>
- /// 获取声音组辅助器。
- /// </summary>
- public ISoundGroupHelper Helper
- {
- get
- {
- return m_SoundGroupHelper;
- }
- }
- /// <summary>
- /// 增加声音代理辅助器。
- /// </summary>
- /// <param name="soundHelper">声音辅助器接口。</param>
- /// <param name="soundAgentHelper">要增加的声音代理辅助器。</param>
- public void AddSoundAgentHelper(ISoundHelper soundHelper, ISoundAgentHelper soundAgentHelper)
- {
- m_SoundAgents.Add(new SoundAgent(this, soundHelper, soundAgentHelper));
- }
- /// <summary>
- /// 播放声音。
- /// </summary>
- /// <param name="serialId">声音的序列编号。</param>
- /// <param name="soundAsset">声音资源。</param>
- /// <param name="playSoundParams">播放声音参数。</param>
- /// <param name="errorCode">错误码。</param>
- /// <returns>用于播放的声音代理。</returns>
- public ISoundAgent PlaySound(int serialId, object soundAsset, PlaySoundParams playSoundParams, out PlaySoundErrorCode? errorCode)
- {
- errorCode = null;
- SoundAgent candidateAgent = null;
- foreach (SoundAgent soundAgent in m_SoundAgents)
- {
- if (!soundAgent.IsPlaying)
- {
- candidateAgent = soundAgent;
- break;
- }
- if (soundAgent.Priority < playSoundParams.Priority)
- {
- if (candidateAgent == null || soundAgent.Priority < candidateAgent.Priority)
- {
- candidateAgent = soundAgent;
- }
- }
- else if (!m_AvoidBeingReplacedBySamePriority && soundAgent.Priority == playSoundParams.Priority)
- {
- if (candidateAgent == null || soundAgent.SetSoundAssetTime < candidateAgent.SetSoundAssetTime)
- {
- candidateAgent = soundAgent;
- }
- }
- }
- if (candidateAgent == null)
- {
- errorCode = PlaySoundErrorCode.IgnoredDueToLowPriority;
- return null;
- }
- if (!candidateAgent.SetSoundAsset(soundAsset))
- {
- errorCode = PlaySoundErrorCode.SetSoundAssetFailure;
- return null;
- }
- candidateAgent.SerialId = serialId;
- candidateAgent.Time = playSoundParams.Time;
- candidateAgent.MuteInSoundGroup = playSoundParams.MuteInSoundGroup;
- candidateAgent.Loop = playSoundParams.Loop;
- candidateAgent.Priority = playSoundParams.Priority;
- candidateAgent.VolumeInSoundGroup = playSoundParams.VolumeInSoundGroup;
- candidateAgent.Pitch = playSoundParams.Pitch;
- candidateAgent.PanStereo = playSoundParams.PanStereo;
- candidateAgent.SpatialBlend = playSoundParams.SpatialBlend;
- candidateAgent.MaxDistance = playSoundParams.MaxDistance;
- candidateAgent.DopplerLevel = playSoundParams.DopplerLevel;
- candidateAgent.Play(playSoundParams.FadeInSeconds);
- return candidateAgent;
- }
- /// <summary>
- /// 停止播放声音。
- /// </summary>
- /// <param name="serialId">要停止播放声音的序列编号。</param>
- /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
- /// <returns>是否停止播放声音成功。</returns>
- public bool StopSound(int serialId, float fadeOutSeconds)
- {
- foreach (SoundAgent soundAgent in m_SoundAgents)
- {
- if (soundAgent.SerialId != serialId)
- {
- continue;
- }
- soundAgent.Stop(fadeOutSeconds);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 暂停播放声音。
- /// </summary>
- /// <param name="serialId">要暂停播放声音的序列编号。</param>
- /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
- /// <returns>是否暂停播放声音成功。</returns>
- public bool PauseSound(int serialId, float fadeOutSeconds)
- {
- foreach (SoundAgent soundAgent in m_SoundAgents)
- {
- if (soundAgent.SerialId != serialId)
- {
- continue;
- }
- soundAgent.Pause(fadeOutSeconds);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 恢复播放声音。
- /// </summary>
- /// <param name="serialId">要恢复播放声音的序列编号。</param>
- /// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
- /// <returns>是否恢复播放声音成功。</returns>
- public bool ResumeSound(int serialId, float fadeInSeconds)
- {
- foreach (SoundAgent soundAgent in m_SoundAgents)
- {
- if (soundAgent.SerialId != serialId)
- {
- continue;
- }
- soundAgent.Resume(fadeInSeconds);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 停止所有已加载的声音。
- /// </summary>
- public void StopAllLoadedSounds()
- {
- foreach (SoundAgent soundAgent in m_SoundAgents)
- {
- if (soundAgent.IsPlaying)
- {
- soundAgent.Stop();
- }
- }
- }
- /// <summary>
- /// 停止所有已加载的声音。
- /// </summary>
- /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
- public void StopAllLoadedSounds(float fadeOutSeconds)
- {
- foreach (SoundAgent soundAgent in m_SoundAgents)
- {
- if (soundAgent.IsPlaying)
- {
- soundAgent.Stop(fadeOutSeconds);
- }
- }
- }
- }
- }
- }
|