//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using System; namespace GameFramework.Sound { internal sealed partial class SoundManager : GameFrameworkModule, ISoundManager { /// /// 声音代理。 /// private sealed class SoundAgent : ISoundAgent { private readonly SoundGroup m_SoundGroup; private readonly ISoundHelper m_SoundHelper; private readonly ISoundAgentHelper m_SoundAgentHelper; private int m_SerialId; private object m_SoundAsset; private DateTime m_SetSoundAssetTime; private bool m_MuteInSoundGroup; private float m_VolumeInSoundGroup; /// /// 初始化声音代理的新实例。 /// /// 所在的声音组。 /// 声音辅助器接口。 /// 声音代理辅助器接口。 public SoundAgent(SoundGroup soundGroup, ISoundHelper soundHelper, ISoundAgentHelper soundAgentHelper) { if (soundGroup == null) { throw new GameFrameworkException("Sound group is invalid."); } if (soundHelper == null) { throw new GameFrameworkException("Sound helper is invalid."); } if (soundAgentHelper == null) { throw new GameFrameworkException("Sound agent helper is invalid."); } m_SoundGroup = soundGroup; m_SoundHelper = soundHelper; m_SoundAgentHelper = soundAgentHelper; m_SoundAgentHelper.ResetSoundAgent += OnResetSoundAgent; m_SerialId = 0; m_SoundAsset = null; Reset(); } /// /// 获取所在的声音组。 /// public ISoundGroup SoundGroup { get { return m_SoundGroup; } } /// /// 获取或设置声音的序列编号。 /// public int SerialId { get { return m_SerialId; } set { m_SerialId = value; } } /// /// 获取当前是否正在播放。 /// public bool IsPlaying { get { return m_SoundAgentHelper.IsPlaying; } } /// /// 获取声音长度。 /// public float Length { get { return m_SoundAgentHelper.Length; } } /// /// 获取或设置播放位置。 /// public float Time { get { return m_SoundAgentHelper.Time; } set { m_SoundAgentHelper.Time = value; } } /// /// 获取是否静音。 /// public bool Mute { get { return m_SoundAgentHelper.Mute; } } /// /// 获取或设置在声音组内是否静音。 /// public bool MuteInSoundGroup { get { return m_MuteInSoundGroup; } set { m_MuteInSoundGroup = value; RefreshMute(); } } /// /// 获取或设置是否循环播放。 /// public bool Loop { get { return m_SoundAgentHelper.Loop; } set { m_SoundAgentHelper.Loop = value; } } /// /// 获取或设置声音优先级。 /// public int Priority { get { return m_SoundAgentHelper.Priority; } set { m_SoundAgentHelper.Priority = value; } } /// /// 获取音量大小。 /// public float Volume { get { return m_SoundAgentHelper.Volume; } } /// /// 获取或设置在声音组内音量大小。 /// public float VolumeInSoundGroup { get { return m_VolumeInSoundGroup; } set { m_VolumeInSoundGroup = value; RefreshVolume(); } } /// /// 获取或设置声音音调。 /// public float Pitch { get { return m_SoundAgentHelper.Pitch; } set { m_SoundAgentHelper.Pitch = value; } } /// /// 获取或设置声音立体声声相。 /// public float PanStereo { get { return m_SoundAgentHelper.PanStereo; } set { m_SoundAgentHelper.PanStereo = value; } } /// /// 获取或设置声音空间混合量。 /// public float SpatialBlend { get { return m_SoundAgentHelper.SpatialBlend; } set { m_SoundAgentHelper.SpatialBlend = value; } } /// /// 获取或设置声音最大距离。 /// public float MaxDistance { get { return m_SoundAgentHelper.MaxDistance; } set { m_SoundAgentHelper.MaxDistance = value; } } /// /// 获取或设置声音多普勒等级。 /// public float DopplerLevel { get { return m_SoundAgentHelper.DopplerLevel; } set { m_SoundAgentHelper.DopplerLevel = value; } } /// /// 获取声音代理辅助器。 /// public ISoundAgentHelper Helper { get { return m_SoundAgentHelper; } } /// /// 获取声音创建时间。 /// internal DateTime SetSoundAssetTime { get { return m_SetSoundAssetTime; } } /// /// 播放声音。 /// public void Play() { m_SoundAgentHelper.Play(Constant.DefaultFadeInSeconds); } /// /// 播放声音。 /// /// 声音淡入时间,以秒为单位。 public void Play(float fadeInSeconds) { m_SoundAgentHelper.Play(fadeInSeconds); } /// /// 停止播放声音。 /// public void Stop() { m_SoundAgentHelper.Stop(Constant.DefaultFadeOutSeconds); } /// /// 停止播放声音。 /// /// 声音淡出时间,以秒为单位。 public void Stop(float fadeOutSeconds) { m_SoundAgentHelper.Stop(fadeOutSeconds); } /// /// 暂停播放声音。 /// public void Pause() { m_SoundAgentHelper.Pause(Constant.DefaultFadeOutSeconds); } /// /// 暂停播放声音。 /// /// 声音淡出时间,以秒为单位。 public void Pause(float fadeOutSeconds) { m_SoundAgentHelper.Pause(fadeOutSeconds); } /// /// 恢复播放声音。 /// public void Resume() { m_SoundAgentHelper.Resume(Constant.DefaultFadeInSeconds); } /// /// 恢复播放声音。 /// /// 声音淡入时间,以秒为单位。 public void Resume(float fadeInSeconds) { m_SoundAgentHelper.Resume(fadeInSeconds); } /// /// 重置声音代理。 /// public void Reset() { if (m_SoundAsset != null) { m_SoundHelper.ReleaseSoundAsset(m_SoundAsset); m_SoundAsset = null; } m_SetSoundAssetTime = DateTime.MinValue; Time = Constant.DefaultTime; MuteInSoundGroup = Constant.DefaultMute; Loop = Constant.DefaultLoop; Priority = Constant.DefaultPriority; VolumeInSoundGroup = Constant.DefaultVolume; Pitch = Constant.DefaultPitch; PanStereo = Constant.DefaultPanStereo; SpatialBlend = Constant.DefaultSpatialBlend; MaxDistance = Constant.DefaultMaxDistance; DopplerLevel = Constant.DefaultDopplerLevel; m_SoundAgentHelper.Reset(); } internal bool SetSoundAsset(object soundAsset) { Reset(); m_SoundAsset = soundAsset; m_SetSoundAssetTime = DateTime.UtcNow; return m_SoundAgentHelper.SetSoundAsset(soundAsset); } internal void RefreshMute() { m_SoundAgentHelper.Mute = m_SoundGroup.Mute || m_MuteInSoundGroup; } internal void RefreshVolume() { m_SoundAgentHelper.Volume = m_SoundGroup.Volume * m_VolumeInSoundGroup; } private void OnResetSoundAgent(object sender, ResetSoundAgentEventArgs e) { Reset(); } } } }