//------------------------------------------------------------
// 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
{
///
/// 声音组。
///
private sealed class SoundGroup : ISoundGroup
{
private readonly string m_Name;
private readonly ISoundGroupHelper m_SoundGroupHelper;
private readonly List m_SoundAgents;
private bool m_AvoidBeingReplacedBySamePriority;
private bool m_Mute;
private float m_Volume;
///
/// 初始化声音组的新实例。
///
/// 声音组名称。
/// 声音组辅助器。
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();
}
///
/// 获取声音组名称。
///
public string Name
{
get
{
return m_Name;
}
}
///
/// 获取声音代理数。
///
public int SoundAgentCount
{
get
{
return m_SoundAgents.Count;
}
}
///
/// 获取或设置声音组中的声音是否避免被同优先级声音替换。
///
public bool AvoidBeingReplacedBySamePriority
{
get
{
return m_AvoidBeingReplacedBySamePriority;
}
set
{
m_AvoidBeingReplacedBySamePriority = value;
}
}
///
/// 获取或设置声音组静音。
///
public bool Mute
{
get
{
return m_Mute;
}
set
{
m_Mute = value;
foreach (SoundAgent soundAgent in m_SoundAgents)
{
soundAgent.RefreshMute();
}
}
}
///
/// 获取或设置声音组音量。
///
public float Volume
{
get
{
return m_Volume;
}
set
{
m_Volume = value;
foreach (SoundAgent soundAgent in m_SoundAgents)
{
soundAgent.RefreshVolume();
}
}
}
///
/// 获取声音组辅助器。
///
public ISoundGroupHelper Helper
{
get
{
return m_SoundGroupHelper;
}
}
///
/// 增加声音代理辅助器。
///
/// 声音辅助器接口。
/// 要增加的声音代理辅助器。
public void AddSoundAgentHelper(ISoundHelper soundHelper, ISoundAgentHelper soundAgentHelper)
{
m_SoundAgents.Add(new SoundAgent(this, soundHelper, soundAgentHelper));
}
///
/// 播放声音。
///
/// 声音的序列编号。
/// 声音资源。
/// 播放声音参数。
/// 错误码。
/// 用于播放的声音代理。
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;
}
///
/// 停止播放声音。
///
/// 要停止播放声音的序列编号。
/// 声音淡出时间,以秒为单位。
/// 是否停止播放声音成功。
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;
}
///
/// 暂停播放声音。
///
/// 要暂停播放声音的序列编号。
/// 声音淡出时间,以秒为单位。
/// 是否暂停播放声音成功。
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;
}
///
/// 恢复播放声音。
///
/// 要恢复播放声音的序列编号。
/// 声音淡入时间,以秒为单位。
/// 是否恢复播放声音成功。
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;
}
///
/// 停止所有已加载的声音。
///
public void StopAllLoadedSounds()
{
foreach (SoundAgent soundAgent in m_SoundAgents)
{
if (soundAgent.IsPlaying)
{
soundAgent.Stop();
}
}
}
///
/// 停止所有已加载的声音。
///
/// 声音淡出时间,以秒为单位。
public void StopAllLoadedSounds(float fadeOutSeconds)
{
foreach (SoundAgent soundAgent in m_SoundAgents)
{
if (soundAgent.IsPlaying)
{
soundAgent.Stop(fadeOutSeconds);
}
}
}
}
}
}