From 8c8555985d602fe07fbe690d36180ceb4713a539 Mon Sep 17 00:00:00 2001 From: GwWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Fri, 9 Jan 2026 19:31:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(assets):=20=E5=B0=86=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E9=87=8D=E6=9E=84=E4=B8=BA=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E4=BB=A5=E4=BC=98=E5=8C=96=E6=9E=B6=E6=9E=84?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重命名 AbstractAssetCatalogSystem 为 AbstractAssetCatalogUtility 并继承 AbstractContextUtility - 重命名 IAssetCatalogSystem 为 IAssetCatalogUtility 并继承 IContextUtility 接口 - 重命名 IResourceFactorySystem 为 IResourceFactoryUtility 并继承 IContextUtility 接口 - 重命名 AbstractResourceFactorySystem 为 AbstractResourceFactoryUtility 并调整依赖注入方式 - 重命名 IResourceLoadSystem 为 IResourceLoadUtility 并继承 IContextUtility 接口 - 重命名 ResourceLoadSystem 为 ResourceLoadUtility 并更新内部日志输出信息 - 移除 AbstractAudioManagerSystem 和 IAudioManagerSystem 相关代码 - 更新所有相关依赖注入和类型引用以匹配新的工具组件架构 --- ...talogSystem.cs => IAssetCatalogUtility.cs} | 7 +- ...rySystem.cs => IResourceFactoryUtility.cs} | 7 +- ...stem.cs => AbstractAssetCatalogUtility.cs} | 4 +- ...m.cs => AbstractResourceFactoryUtility.cs} | 43 +- ...eLoadSystem.cs => IResourceLoadUtility.cs} | 4 +- ...ceLoadSystem.cs => ResourceLoadUtility.cs} | 6 +- .../system/AbstractAudioManagerSystem.cs | 548 ------------------ .../system/IAudioManagerSystem.cs | 179 ------ 8 files changed, 29 insertions(+), 769 deletions(-) rename GFramework.Game.Abstractions/assets/{IAssetCatalogSystem.cs => IAssetCatalogUtility.cs} (92%) rename GFramework.Game.Abstractions/assets/{IResourceFactorySystem.cs => IResourceFactoryUtility.cs} (74%) rename GFramework.Game/assets/{AbstractAssetCatalogSystem.cs => AbstractAssetCatalogUtility.cs} (98%) rename GFramework.Godot/assets/{AbstractResourceFactorySystem.cs => AbstractResourceFactoryUtility.cs} (73%) rename GFramework.Godot/assets/{IResourceLoadSystem.cs => IResourceLoadUtility.cs} (96%) rename GFramework.Godot/assets/{ResourceLoadSystem.cs => ResourceLoadUtility.cs} (97%) delete mode 100644 GFramework.Godot/system/AbstractAudioManagerSystem.cs delete mode 100644 GFramework.Godot/system/IAudioManagerSystem.cs diff --git a/GFramework.Game.Abstractions/assets/IAssetCatalogSystem.cs b/GFramework.Game.Abstractions/assets/IAssetCatalogUtility.cs similarity index 92% rename from GFramework.Game.Abstractions/assets/IAssetCatalogSystem.cs rename to GFramework.Game.Abstractions/assets/IAssetCatalogUtility.cs index ff38ba1..a022360 100644 --- a/GFramework.Game.Abstractions/assets/IAssetCatalogSystem.cs +++ b/GFramework.Game.Abstractions/assets/IAssetCatalogUtility.cs @@ -1,11 +1,12 @@ -using GFramework.Core.Abstractions.system; +using GFramework.Core.Abstractions.utility; namespace GFramework.Game.Abstractions.assets; /// -/// 资源目录系统接口,用于管理场景和资源的获取与查询 +/// 资产目录工具接口,提供对场景单元、场景页面和普通资产的管理功能 +/// 继承自IUtility接口,用于处理资产目录相关的操作 /// -public interface IAssetCatalogSystem : ISystem +public interface IAssetCatalogUtility : IContextUtility { /// /// 根据指定的键获取场景单元标识符 diff --git a/GFramework.Game.Abstractions/assets/IResourceFactorySystem.cs b/GFramework.Game.Abstractions/assets/IResourceFactoryUtility.cs similarity index 74% rename from GFramework.Game.Abstractions/assets/IResourceFactorySystem.cs rename to GFramework.Game.Abstractions/assets/IResourceFactoryUtility.cs index fff1989..0898c94 100644 --- a/GFramework.Game.Abstractions/assets/IResourceFactorySystem.cs +++ b/GFramework.Game.Abstractions/assets/IResourceFactoryUtility.cs @@ -1,12 +1,13 @@ using System; -using GFramework.Core.Abstractions.system; +using GFramework.Core.Abstractions.utility; namespace GFramework.Game.Abstractions.assets; /// -/// 资源工厂系统接口,用于获取指定类型的资源创建函数 +/// 资源工厂工具接口,提供根据键名或资产目录映射获取资源创建函数的功能 +/// 继承自IContextUtility接口,用于在游戏框架中管理资源创建工厂 /// -public interface IResourceFactorySystem : ISystem +public interface IResourceFactoryUtility : IContextUtility { /// /// 根据指定键名获取指定类型T的资源创建函数 diff --git a/GFramework.Game/assets/AbstractAssetCatalogSystem.cs b/GFramework.Game/assets/AbstractAssetCatalogUtility.cs similarity index 98% rename from GFramework.Game/assets/AbstractAssetCatalogSystem.cs rename to GFramework.Game/assets/AbstractAssetCatalogUtility.cs index 894464d..bf2d5dc 100644 --- a/GFramework.Game/assets/AbstractAssetCatalogSystem.cs +++ b/GFramework.Game/assets/AbstractAssetCatalogUtility.cs @@ -1,4 +1,4 @@ -using GFramework.Core.system; +using GFramework.Core.utility; using GFramework.Game.Abstractions.assets; namespace GFramework.Game.assets; @@ -8,7 +8,7 @@ namespace GFramework.Game.assets; /// 提供了统一的接口来注册和查询不同类型的资产(如游戏单元、模板、普通资源)。 /// 子类需要实现 方法以完成具体资产的注册逻辑。 /// -public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalogSystem +public abstract class AbstractAssetCatalogUtility : AbstractContextUtility, IAssetCatalogUtility { private readonly Dictionary _assets = new(); private readonly Dictionary _scenePages = new(); diff --git a/GFramework.Godot/assets/AbstractResourceFactorySystem.cs b/GFramework.Godot/assets/AbstractResourceFactoryUtility.cs similarity index 73% rename from GFramework.Godot/assets/AbstractResourceFactorySystem.cs rename to GFramework.Godot/assets/AbstractResourceFactoryUtility.cs index eabdcb0..f88f35c 100644 --- a/GFramework.Godot/assets/AbstractResourceFactorySystem.cs +++ b/GFramework.Godot/assets/AbstractResourceFactoryUtility.cs @@ -1,6 +1,5 @@ -using GFramework.Core.Abstractions.enums; using GFramework.Core.extensions; -using GFramework.Core.system; +using GFramework.Core.utility; using GFramework.Game.Abstractions.assets; using Godot; @@ -10,27 +9,11 @@ namespace GFramework.Godot.assets; /// 资源工厂系统抽象基类,用于统一管理各类资源的创建与预加载逻辑。 /// 提供注册场景和资源的方法,并通过依赖的资源加载系统和资产目录系统完成实际资源的获取与构造。 /// -public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceFactorySystem +public abstract class AbstractResourceFactoryUtility : AbstractContextUtility, IResourceFactoryUtility { - private IAssetCatalogSystem? _assetCatalogSystem; + private IAssetCatalogUtility? _assetCatalogUtility; private ResourceFactory.Registry? _registry; - private IResourceLoadSystem? _resourceLoadSystem; - - - /// - /// 在架构阶段发生变化时执行相应的处理逻辑。 - /// - /// 当前的架构阶段 - public override void OnArchitecturePhase(ArchitecturePhase phase) - { - if (phase == ArchitecturePhase.Ready) - { - // 在架构准备就绪阶段注册资源并预加载所有资源 - RegisterResources(); - _registry!.PreloadAll(); - } - } - + private IResourceLoadUtility? _resourceLoadUtility; /// /// 根据指定的键获取资源工厂函数。 @@ -63,8 +46,10 @@ public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceF protected override void OnInit() { _registry = new ResourceFactory.Registry(); - _resourceLoadSystem = this.GetSystem(); - _assetCatalogSystem = this.GetSystem(); + _resourceLoadUtility = this.GetUtility(); + _assetCatalogUtility = this.GetUtility(); + RegisterResources(); + _registry!.PreloadAll(); } /// @@ -86,11 +71,11 @@ public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceF bool preload = false) where T : Node { - var id = _assetCatalogSystem!.GetSceneUnit(sceneUnitKey); + var id = _assetCatalogUtility!.GetSceneUnit(sceneUnitKey); _registry!.Register( sceneUnitKey, - _resourceLoadSystem!.GetOrRegisterGameUnitFactory(id), + _resourceLoadUtility!.GetOrRegisterGameUnitFactory(id), preload ); } @@ -107,11 +92,11 @@ public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceF bool preload = false) where T : Node { - var id = _assetCatalogSystem!.GetScenePage(scenePageKey); + var id = _assetCatalogUtility!.GetScenePage(scenePageKey); _registry!.Register( scenePageKey, - _resourceLoadSystem!.GetOrRegisterTemplateFactory(id), + _resourceLoadUtility!.GetOrRegisterTemplateFactory(id), preload ); } @@ -127,11 +112,11 @@ public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceF bool preload = false) where T : Resource { - var id = _assetCatalogSystem!.GetAsset(assetKey); + var id = _assetCatalogUtility!.GetAsset(assetKey); _registry!.Register( assetKey, - _resourceLoadSystem!.GetOrRegisterAssetFactory(id), + _resourceLoadUtility!.GetOrRegisterAssetFactory(id), preload ); } diff --git a/GFramework.Godot/assets/IResourceLoadSystem.cs b/GFramework.Godot/assets/IResourceLoadUtility.cs similarity index 96% rename from GFramework.Godot/assets/IResourceLoadSystem.cs rename to GFramework.Godot/assets/IResourceLoadUtility.cs index 2a124a4..862a871 100644 --- a/GFramework.Godot/assets/IResourceLoadSystem.cs +++ b/GFramework.Godot/assets/IResourceLoadUtility.cs @@ -1,4 +1,4 @@ -using GFramework.Core.Abstractions.system; +using GFramework.Core.Abstractions.utility; using GFramework.Game.Abstractions.assets; using Godot; @@ -7,7 +7,7 @@ namespace GFramework.Godot.assets; /// /// 资源加载系统接口,提供资源和场景的加载、实例化、预加载等功能 /// -public interface IResourceLoadSystem : ISystem +public interface IResourceLoadUtility : IContextUtility { /// /// 加载指定路径的资源 diff --git a/GFramework.Godot/assets/ResourceLoadSystem.cs b/GFramework.Godot/assets/ResourceLoadUtility.cs similarity index 97% rename from GFramework.Godot/assets/ResourceLoadSystem.cs rename to GFramework.Godot/assets/ResourceLoadUtility.cs index d70318b..0aaccf0 100644 --- a/GFramework.Godot/assets/ResourceLoadSystem.cs +++ b/GFramework.Godot/assets/ResourceLoadUtility.cs @@ -1,4 +1,4 @@ -using GFramework.Core.system; +using GFramework.Core.utility; using GFramework.Game.Abstractions.assets; using Godot; @@ -8,7 +8,7 @@ namespace GFramework.Godot.assets; /// 资源加载系统,用于统一管理和缓存Godot资源(如场景、纹理等)的加载与实例化。 /// 提供基础加载、场景实例化、资源工厂注册以及缓存管理功能。 /// -public class ResourceLoadSystem : AbstractSystem, IResourceLoadSystem +public class ResourceLoadUtility : AbstractContextUtility, IResourceLoadUtility { /// /// 已加载的资源缓存字典,键为资源路径,值为已加载的Resource对象。 @@ -129,7 +129,7 @@ public class ResourceLoadSystem : AbstractSystem, IResourceLoadSystem var res = GD.Load(path); if (res == null) { - GD.PrintErr($"[ResourceLoadSystem] Load failed: {path}"); + GD.PrintErr($"[ResourceLoadUtility] Load failed: {path}"); return null; } diff --git a/GFramework.Godot/system/AbstractAudioManagerSystem.cs b/GFramework.Godot/system/AbstractAudioManagerSystem.cs deleted file mode 100644 index dadf886..0000000 --- a/GFramework.Godot/system/AbstractAudioManagerSystem.cs +++ /dev/null @@ -1,548 +0,0 @@ -using GFramework.Core.extensions; -using GFramework.Core.system; -using GFramework.Game.Abstractions.assets; -using GFramework.Godot.assets; -using Godot; - -namespace GFramework.Godot.system; - -/// -/// 音频管理器抽象基类,提供音频播放的基础实现 -/// -public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManagerSystem -{ - /// - /// 最大同时播放的音效数量 - /// - protected const int MaxSoundPlayers = 10; - - /// - /// 最大同时播放的3D音效数量 - /// - protected const int MaxSound3DPlayers = 5; - - /// - /// 可用3D音效播放器队列 - /// - protected readonly Queue AvailableSound3DPlayers = new(); - - /// - /// 可用音效播放器队列 - /// - protected readonly Queue AvailableSoundPlayers = new(); - - /// - /// 3D音效播放器列表 - /// - protected readonly List Sound3DPlayers = []; - - /// - /// 音效播放器列表 - /// - protected readonly List SoundPlayers = []; - - /// - /// 环境音量 - /// - protected float AmbientVolume = 1.0f; - - /// - /// 资源目录系统依赖 - /// - protected IAssetCatalogSystem? AssetCatalogSystem; - - /// - /// 主音量 - /// - protected float MasterVolume = 1.0f; - - /// - /// 音乐淡入淡出动画 - /// - protected Tween? MusicFadeTween; - - /// - /// 背景音乐播放器 - /// - protected AudioStreamPlayer? MusicPlayer; - - /// - /// 背景音乐音量 - /// - protected float MusicVolume = 1.0f; - - /// - /// 资源工厂系统依赖 - /// - protected IResourceFactorySystem? ResourceFactorySystem; - - /// - /// 音频资源加载系统依赖 - /// - protected IResourceLoadSystem? ResourceLoadSystem; - - /// - /// 特效音量 - /// - protected float SfxVolume = 1.0f; - - /// - /// 音效音量 - /// - protected float SoundVolume = 1.0f; - - /// - /// 语音音量 - /// - protected float VoiceVolume = 1.0f; - - /// - /// 所有者节点的抽象属性 - /// - protected abstract Node Owner { get; } - - /// - /// 播放背景音乐 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 是否循环播放 - public virtual void PlayMusic(string audioPath, float volume = 1.0f, bool loop = true) - { - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null || MusicPlayer == null) return; - - // 停止当前正在进行的淡入淡出效果 - MusicFadeTween?.Kill(); - - MusicPlayer.Stream = audioStream; - MusicPlayer.VolumeDb = LinearToDb(volume * MusicVolume * MasterVolume); - MusicPlayer.Play(); - } - - /// - /// 播放音效 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - public virtual void PlaySound(string audioPath, float volume = 1.0f, float pitch = 1.0f) - { - if (AvailableSoundPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSoundPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * SoundVolume * MasterVolume); - player.Play(); - } - - /// - /// 播放特效音效 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - public virtual void PlaySfx(string audioPath, float volume = 1.0f, float pitch = 1.0f) - { - if (AvailableSoundPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSoundPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * SfxVolume * MasterVolume); - player.Play(); - } - - /// - /// 播放语音 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - public virtual void PlayVoice(string audioPath, float volume = 1.0f, float pitch = 1.0f) - { - if (AvailableSoundPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSoundPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * VoiceVolume * MasterVolume); - player.Play(); - } - - /// - /// 播放环境音效 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - public virtual void PlayAmbient(string audioPath, float volume = 1.0f, float pitch = 1.0f) - { - if (AvailableSoundPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSoundPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * AmbientVolume * MasterVolume); - player.Play(); - } - - /// - /// 停止背景音乐 - /// - public virtual void StopMusic() - { - MusicFadeTween?.Kill(); - MusicPlayer?.Stop(); - } - - /// - /// 暂停背景音乐 - /// - public virtual void PauseMusic() - { - MusicFadeTween?.Kill(); - // todo 需要记录音乐播放位置,以便恢复播放时从正确位置开始 - } - - /// - /// 恢复背景音乐播放 - /// - public virtual void ResumeMusic() - { - MusicPlayer?.Play(); - } - - /// - /// 设置背景音乐音量 - /// - /// 音量大小,范围0-1 - public virtual void SetMusicVolume(float volume) - { - MusicVolume = volume; - if (MusicPlayer != null) MusicPlayer.VolumeDb = LinearToDb(MusicVolume * MasterVolume); - } - - /// - /// 获取背景音乐音量 - /// - /// 音量大小,范围0-1 - public virtual float GetMusicVolume() - { - return MusicVolume; - } - - /// - /// 设置音效音量 - /// - /// 音量大小,范围0-1 - public virtual void SetSoundVolume(float volume) - { - SoundVolume = volume; - } - - /// - /// 获取音效音量 - /// - /// 音量大小,范围0-1 - public virtual float GetSoundVolume() - { - return SoundVolume; - } - - /// - /// 设置主音量 - /// - /// 音量大小,范围0-1 - public virtual void SetMasterVolume(float volume) - { - MasterVolume = volume; - - // 更新音乐音量 - if (MusicPlayer != null) MusicPlayer.VolumeDb = LinearToDb(MusicVolume * MasterVolume); - } - - /// - /// 获取主音量 - /// - /// 音量大小,范围0-1 - public virtual float GetMasterVolume() - { - return MasterVolume; - } - - /// - /// 设置SFX音量 - /// - /// 音量大小,范围0-1 - public virtual void SetSfxVolume(float volume) - { - SfxVolume = volume; - } - - /// - /// 获取SFX音量 - /// - /// 音量大小,范围0-1 - public virtual float GetSfxVolume() - { - return SfxVolume; - } - - /// - /// 设置语音音量 - /// - /// 音量大小,范围0-1 - public virtual void SetVoiceVolume(float volume) - { - VoiceVolume = volume; - } - - /// - /// 获取语音音量 - /// - /// 音量大小,范围0-1 - public virtual float GetVoiceVolume() - { - return VoiceVolume; - } - - /// - /// 设置环境音量 - /// - /// 音量大小,范围0-1 - public virtual void SetAmbientVolume(float volume) - { - AmbientVolume = volume; - } - - /// - /// 获取环境音量 - /// - /// 音量大小,范围0-1 - public virtual float GetAmbientVolume() - { - return AmbientVolume; - } - - /// - /// 检查背景音乐是否正在播放 - /// - /// 正在播放返回true,否则返回false - public virtual bool IsMusicPlaying() - { - return MusicPlayer?.Playing ?? false; - } - - /// - /// 淡入背景音乐 - /// - /// 音频文件路径 - /// 淡入持续时间(秒) - /// 目标音量 - public virtual void FadeInMusic(string audioPath, float duration, float volume = 1.0f) - { - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null || MusicPlayer == null) return; - - // 停止当前正在进行的淡入淡出效果 - MusicFadeTween?.Kill(); - - MusicPlayer.Stream = audioStream; - MusicPlayer.VolumeDb = LinearToDb(0.0f); // 初始音量为0 - MusicPlayer.Play(); - - // 创建淡入动画 - MusicFadeTween = Owner.CreateTween(); - MusicFadeTween.TweenProperty(MusicPlayer, "volume_db", LinearToDb(volume * MusicVolume * MasterVolume), - duration); - } - - /// - /// 淡出背景音乐 - /// - /// 淡出持续时间(秒) - public virtual void FadeOutMusic(float duration) - { - if (MusicPlayer == null) return; - - // 停止当前正在进行的淡入淡出效果 - MusicFadeTween?.Kill(); - - // 创建淡出动画 - MusicFadeTween = Owner.CreateTween(); - MusicFadeTween.TweenProperty(MusicPlayer, "volume_db", LinearToDb(0.0f), duration); - MusicFadeTween.TweenCallback(Callable.From(() => MusicPlayer.Stop())); - } - - /// - /// 设置低通滤波器强度 - /// - /// 滤波器强度,范围0-1 - public virtual void SetLowPassFilter(float amount) - { - // TODO: 实现低通滤波器效果 - // 可以通过AudioEffectLowPassFilter实现 - } - - /// - /// 设置音频混响效果 - /// - /// 房间大小 - /// 阻尼 - /// 湿声级别 - public virtual void SetReverb(float roomSize, float damping, float wetLevel) - { - // TODO: 实现音频混响效果 - // 可以通过AudioEffectReverb实现 - } - - /// - /// 播放3D音效 - /// - /// 音频文件路径 - /// 3D空间中的位置 - /// 音量大小,范围0-1 - public virtual void PlaySound3D(string audioPath, Vector3 position, float volume = 1.0f) - { - if (AvailableSound3DPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSound3DPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * SoundVolume * MasterVolume); - player.Position = position; - player.Play(); - } - - /// - /// 系统初始化方法 - /// - protected override void OnInit() - { - // 获取依赖的系统 - ResourceLoadSystem = this.GetSystem(); - AssetCatalogSystem = this.GetSystem(); - ResourceFactorySystem = this.GetSystem(); - - // 初始化背景音乐播放器 - MusicPlayer = new AudioStreamPlayer(); - Owner.AddChild(MusicPlayer); - - // 预创建音效播放器池 - for (var i = 0; i < MaxSoundPlayers; i++) - { - var soundPlayer = new AudioStreamPlayer(); - Owner.AddChild(soundPlayer); - soundPlayer.Finished += () => OnSoundFinished(soundPlayer); - SoundPlayers.Add(soundPlayer); - AvailableSoundPlayers.Enqueue(soundPlayer); - } - - // 预创建3D音效播放器池 - for (var i = 0; i < MaxSound3DPlayers; i++) - { - var sound3DPlayer = new AudioStreamPlayer3D(); - Owner.AddChild(sound3DPlayer); - sound3DPlayer.Finished += () => OnSound3DFinished(sound3DPlayer); - Sound3DPlayers.Add(sound3DPlayer); - AvailableSound3DPlayers.Enqueue(sound3DPlayer); - } - } - - /// - /// 当音效播放完成时的回调 - /// - /// 完成播放的音频播放器 - private void OnSoundFinished(AudioStreamPlayer player) - { - // 将播放器放回可用队列 - AvailableSoundPlayers.Enqueue(player); - } - - /// - /// 当3D音效播放完成时的回调 - /// - /// 完成播放的3D音频播放器 - private void OnSound3DFinished(AudioStreamPlayer3D player) - { - // 将播放器放回可用队列 - AvailableSound3DPlayers.Enqueue(player); - } - - /// - /// 通过资源ID播放背景音乐 - /// - /// 音乐资源ID - /// 音量大小,范围0-1 - /// 是否循环播放 - public virtual void PlayMusic(AssetCatalog.AssetId musicId, float volume = 1.0f, bool loop = true) - { - PlayMusic(musicId.Path, volume, loop); - } - - /// - /// 通过资源ID播放音效 - /// - /// 音效资源ID - /// 音量大小,范围0-1 - /// 音调调整 - public virtual void PlaySound(AssetCatalog.AssetId soundId, float volume = 1.0f, float pitch = 1.0f) - { - PlaySound(soundId.Path, volume, pitch); - } - - /// - /// 将线性音量值转换为分贝值 - /// - /// 线性音量值(0-1) - /// 分贝值 - protected static float LinearToDb(float linear) - { - return linear > 0 ? 20 * Mathf.Log(linear) : -100; - } - - /// - /// 将分贝值转换为线性音量值 - /// - /// 分贝值 - /// 线性音量值(0-1) - protected static float DbToLinear(float db) - { - return db > -100 ? Mathf.Exp(db / 20) : 0; - } - - /// - /// 系统销毁时清理资源 - /// - protected override void OnDestroy() - { - // 停止并清理淡入淡出动画 - MusicFadeTween?.Kill(); - - // 清理音乐播放器 - MusicPlayer?.QueueFree(); - - // 清理音效播放器池 - foreach (var player in SoundPlayers) player.QueueFree(); - - // 清理3D音效播放器池 - foreach (var player in Sound3DPlayers) player.QueueFree(); - - SoundPlayers.Clear(); - AvailableSoundPlayers.Clear(); - Sound3DPlayers.Clear(); - AvailableSound3DPlayers.Clear(); - } -} \ No newline at end of file diff --git a/GFramework.Godot/system/IAudioManagerSystem.cs b/GFramework.Godot/system/IAudioManagerSystem.cs deleted file mode 100644 index 209297c..0000000 --- a/GFramework.Godot/system/IAudioManagerSystem.cs +++ /dev/null @@ -1,179 +0,0 @@ -using GFramework.Core.Abstractions.system; -using Godot; - -namespace GFramework.Godot.system; - -/// -/// 音频管理器系统接口,用于统一管理背景音乐和音效的播放 -/// -public interface IAudioManagerSystem : ISystem -{ - /// - /// 播放背景音乐 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 是否循环播放 - void PlayMusic(string audioPath, float volume = 1.0f, bool loop = true); - - /// - /// 播放音效 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - void PlaySound(string audioPath, float volume = 1.0f, float pitch = 1.0f); - - /// - /// 播放特效音效 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - void PlaySfx(string audioPath, float volume = 1.0f, float pitch = 1.0f); - - /// - /// 播放语音 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - void PlayVoice(string audioPath, float volume = 1.0f, float pitch = 1.0f); - - /// - /// 播放环境音效 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - void PlayAmbient(string audioPath, float volume = 1.0f, float pitch = 1.0f); - - /// - /// 停止背景音乐 - /// - void StopMusic(); - - /// - /// 暂停背景音乐 - /// - void PauseMusic(); - - /// - /// 恢复背景音乐播放 - /// - void ResumeMusic(); - - /// - /// 设置背景音乐音量 - /// - /// 音量大小,范围0-1 - void SetMusicVolume(float volume); - - /// - /// 获取背景音乐音量 - /// - /// 音量大小,范围0-1 - float GetMusicVolume(); - - /// - /// 设置音效音量 - /// - /// 音量大小,范围0-1 - void SetSoundVolume(float volume); - - /// - /// 获取音效音量 - /// - /// 音量大小,范围0-1 - float GetSoundVolume(); - - /// - /// 设置特效音量 - /// - /// 音量大小,范围0-1 - void SetSfxVolume(float volume); - - /// - /// 获取特效音量 - /// - /// 音量大小,范围0-1 - float GetSfxVolume(); - - /// - /// 设置语音音量 - /// - /// 音量大小,范围0-1 - void SetVoiceVolume(float volume); - - /// - /// 获取语音音量 - /// - /// 音量大小,范围0-1 - float GetVoiceVolume(); - - /// - /// 设置环境音量 - /// - /// 音量大小,范围0-1 - void SetAmbientVolume(float volume); - - /// - /// 获取环境音量 - /// - /// 音量大小,范围0-1 - float GetAmbientVolume(); - - /// - /// 设置主音量 - /// - /// 音量大小,范围0-1 - void SetMasterVolume(float volume); - - /// - /// 获取主音量 - /// - /// 音量大小,范围0-1 - float GetMasterVolume(); - - /// - /// 检查背景音乐是否正在播放 - /// - /// 正在播放返回true,否则返回false - bool IsMusicPlaying(); - - /// - /// 淡入背景音乐 - /// - /// 音频文件路径 - /// 淡入持续时间(秒) - /// 目标音量 - void FadeInMusic(string audioPath, float duration, float volume = 1.0f); - - /// - /// 淡出背景音乐 - /// - /// 淡出持续时间(秒) - void FadeOutMusic(float duration); - - /// - /// 播放3D音效 - /// - /// 音频文件路径 - /// 3D空间中的位置 - /// 音量大小,范围0-1 - void PlaySound3D(string audioPath, Vector3 position, float volume = 1.0f); - - /// - /// 设置低通滤波器强度 - /// - /// 滤波器强度,范围0-1 - void SetLowPassFilter(float amount); - - /// - /// 设置音频混响效果 - /// - /// 房间大小 - /// 阻尼 - /// 湿声级别 - void SetReverb(float roomSize, float damping, float wetLevel); -} \ No newline at end of file