123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- //------------------------------------------------------------
- // 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
- {
- /// <summary>
- /// 声音代理。
- /// </summary>
- 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;
- /// <summary>
- /// 初始化声音代理的新实例。
- /// </summary>
- /// <param name="soundGroup">所在的声音组。</param>
- /// <param name="soundHelper">声音辅助器接口。</param>
- /// <param name="soundAgentHelper">声音代理辅助器接口。</param>
- 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();
- }
- /// <summary>
- /// 获取所在的声音组。
- /// </summary>
- public ISoundGroup SoundGroup
- {
- get
- {
- return m_SoundGroup;
- }
- }
- /// <summary>
- /// 获取或设置声音的序列编号。
- /// </summary>
- public int SerialId
- {
- get
- {
- return m_SerialId;
- }
- set
- {
- m_SerialId = value;
- }
- }
- /// <summary>
- /// 获取当前是否正在播放。
- /// </summary>
- public bool IsPlaying
- {
- get
- {
- return m_SoundAgentHelper.IsPlaying;
- }
- }
- /// <summary>
- /// 获取声音长度。
- /// </summary>
- public float Length
- {
- get
- {
- return m_SoundAgentHelper.Length;
- }
- }
- /// <summary>
- /// 获取或设置播放位置。
- /// </summary>
- public float Time
- {
- get
- {
- return m_SoundAgentHelper.Time;
- }
- set
- {
- m_SoundAgentHelper.Time = value;
- }
- }
- /// <summary>
- /// 获取是否静音。
- /// </summary>
- public bool Mute
- {
- get
- {
- return m_SoundAgentHelper.Mute;
- }
- }
- /// <summary>
- /// 获取或设置在声音组内是否静音。
- /// </summary>
- public bool MuteInSoundGroup
- {
- get
- {
- return m_MuteInSoundGroup;
- }
- set
- {
- m_MuteInSoundGroup = value;
- RefreshMute();
- }
- }
- /// <summary>
- /// 获取或设置是否循环播放。
- /// </summary>
- public bool Loop
- {
- get
- {
- return m_SoundAgentHelper.Loop;
- }
- set
- {
- m_SoundAgentHelper.Loop = value;
- }
- }
- /// <summary>
- /// 获取或设置声音优先级。
- /// </summary>
- public int Priority
- {
- get
- {
- return m_SoundAgentHelper.Priority;
- }
- set
- {
- m_SoundAgentHelper.Priority = value;
- }
- }
- /// <summary>
- /// 获取音量大小。
- /// </summary>
- public float Volume
- {
- get
- {
- return m_SoundAgentHelper.Volume;
- }
- }
- /// <summary>
- /// 获取或设置在声音组内音量大小。
- /// </summary>
- public float VolumeInSoundGroup
- {
- get
- {
- return m_VolumeInSoundGroup;
- }
- set
- {
- m_VolumeInSoundGroup = value;
- RefreshVolume();
- }
- }
- /// <summary>
- /// 获取或设置声音音调。
- /// </summary>
- public float Pitch
- {
- get
- {
- return m_SoundAgentHelper.Pitch;
- }
- set
- {
- m_SoundAgentHelper.Pitch = value;
- }
- }
- /// <summary>
- /// 获取或设置声音立体声声相。
- /// </summary>
- public float PanStereo
- {
- get
- {
- return m_SoundAgentHelper.PanStereo;
- }
- set
- {
- m_SoundAgentHelper.PanStereo = value;
- }
- }
- /// <summary>
- /// 获取或设置声音空间混合量。
- /// </summary>
- public float SpatialBlend
- {
- get
- {
- return m_SoundAgentHelper.SpatialBlend;
- }
- set
- {
- m_SoundAgentHelper.SpatialBlend = value;
- }
- }
- /// <summary>
- /// 获取或设置声音最大距离。
- /// </summary>
- public float MaxDistance
- {
- get
- {
- return m_SoundAgentHelper.MaxDistance;
- }
- set
- {
- m_SoundAgentHelper.MaxDistance = value;
- }
- }
- /// <summary>
- /// 获取或设置声音多普勒等级。
- /// </summary>
- public float DopplerLevel
- {
- get
- {
- return m_SoundAgentHelper.DopplerLevel;
- }
- set
- {
- m_SoundAgentHelper.DopplerLevel = value;
- }
- }
- /// <summary>
- /// 获取声音代理辅助器。
- /// </summary>
- public ISoundAgentHelper Helper
- {
- get
- {
- return m_SoundAgentHelper;
- }
- }
- /// <summary>
- /// 获取声音创建时间。
- /// </summary>
- internal DateTime SetSoundAssetTime
- {
- get
- {
- return m_SetSoundAssetTime;
- }
- }
- /// <summary>
- /// 播放声音。
- /// </summary>
- public void Play()
- {
- m_SoundAgentHelper.Play(Constant.DefaultFadeInSeconds);
- }
- /// <summary>
- /// 播放声音。
- /// </summary>
- /// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
- public void Play(float fadeInSeconds)
- {
- m_SoundAgentHelper.Play(fadeInSeconds);
- }
- /// <summary>
- /// 停止播放声音。
- /// </summary>
- public void Stop()
- {
- m_SoundAgentHelper.Stop(Constant.DefaultFadeOutSeconds);
- }
- /// <summary>
- /// 停止播放声音。
- /// </summary>
- /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
- public void Stop(float fadeOutSeconds)
- {
- m_SoundAgentHelper.Stop(fadeOutSeconds);
- }
- /// <summary>
- /// 暂停播放声音。
- /// </summary>
- public void Pause()
- {
- m_SoundAgentHelper.Pause(Constant.DefaultFadeOutSeconds);
- }
- /// <summary>
- /// 暂停播放声音。
- /// </summary>
- /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
- public void Pause(float fadeOutSeconds)
- {
- m_SoundAgentHelper.Pause(fadeOutSeconds);
- }
- /// <summary>
- /// 恢复播放声音。
- /// </summary>
- public void Resume()
- {
- m_SoundAgentHelper.Resume(Constant.DefaultFadeInSeconds);
- }
- /// <summary>
- /// 恢复播放声音。
- /// </summary>
- /// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
- public void Resume(float fadeInSeconds)
- {
- m_SoundAgentHelper.Resume(fadeInSeconds);
- }
- /// <summary>
- /// 重置声音代理。
- /// </summary>
- 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();
- }
- }
- }
- }
|