diff --git a/DungeonShooting_Godot/project.godot b/DungeonShooting_Godot/project.godot index acd52e2..cc08f5e 100644 --- a/DungeonShooting_Godot/project.godot +++ b/DungeonShooting_Godot/project.godot @@ -26,6 +26,8 @@ window/size/width=1920 window/size/height=1080 +window/size/resizable=false +window/size/always_on_top=true window/dpi/allow_hidpi=true window/vsync/use_vsync=false window/stretch/mode="2d" diff --git a/DungeonShooting_Godot/src/game/GameApplication.cs b/DungeonShooting_Godot/src/game/GameApplication.cs index 517f5ef..51aa4d2 100644 --- a/DungeonShooting_Godot/src/game/GameApplication.cs +++ b/DungeonShooting_Godot/src/game/GameApplication.cs @@ -44,6 +44,9 @@ /// public RoomUI Ui { get; private set; } + /// + /// 全局根节点 + /// public Node2D GlobalNodeRoot { get; private set; } public GameApplication() diff --git a/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs b/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs index b1162a5..c2d6da6 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs @@ -23,8 +23,8 @@ ScatteringRangeBackSpeed = 40; //连发 ContinuousShoot = true; - AmmoCapacity = 120; - MaxAmmoCapacity = 120 * 70; + AmmoCapacity = 30; + MaxAmmoCapacity = 30 * 70; //扳机检测间隔 TriggerInterval = 0f; //连发数量 @@ -106,7 +106,7 @@ GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); } //播放射击音效 - SoundManager.PlaySoundEffect("ordinaryBullet.ogg", this, 6f); + SoundManager.PlaySoundEffectPosition(ResourcePath.resource_sound_sfx_ordinaryBullet_ogg, GameApplication.Instance.ViewToGlobalPosition(GlobalPosition), 6f); } protected override void OnShoot(float fireRotation) diff --git a/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs b/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs index 8d04498..f1424b1 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs @@ -72,7 +72,7 @@ GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); } //播放射击音效 - SoundManager.PlaySoundEffect("ordinaryBullet.ogg", this, 6f); + SoundManager.PlaySoundEffectPosition(ResourcePath.resource_sound_sfx_ordinaryBullet_ogg, GameApplication.Instance.ViewToGlobalPosition(GlobalPosition), 6f); } protected override void OnShoot(float fireRotation) diff --git a/DungeonShooting_Godot/src/game/item/weapon/shell/ShellCase.cs b/DungeonShooting_Godot/src/game/item/weapon/shell/ShellCase.cs index cf520ef..60e73d6 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/shell/ShellCase.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/shell/ShellCase.cs @@ -20,6 +20,6 @@ { //30秒后销毁 await ToSignal(GetTree().CreateTimer(30), "timeout"); - QueueFree(); + Destroy(); } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/manager/ResourceManager.cs b/DungeonShooting_Godot/src/game/manager/ResourceManager.cs index 4c69cd1..686c730 100644 --- a/DungeonShooting_Godot/src/game/manager/ResourceManager.cs +++ b/DungeonShooting_Godot/src/game/manager/ResourceManager.cs @@ -14,9 +14,11 @@ { _shadowMaterial = ResourceLoader.Load(ResourcePath.resource_materlal_Blend_tres); } + return _shadowMaterial; } } + private static ShaderMaterial _shadowMaterial; /// @@ -30,9 +32,11 @@ { _shadowShader = ResourceLoader.Load(ResourcePath.resource_materlal_Blend_gdshader); } + return _shadowShader; } } + private static Shader _shadowShader; private static readonly Dictionary CachePack = new Dictionary(); @@ -55,7 +59,12 @@ CachePack.Add(path, pack); return pack as T; } + else + { + GD.PrintErr("加载资源失败, 未找到资源: " + path); + } } - return default(T); + + return default; } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/manager/SoundManager.cs b/DungeonShooting_Godot/src/game/manager/SoundManager.cs index 2cf4f34..d7dfd26 100644 --- a/DungeonShooting_Godot/src/game/manager/SoundManager.cs +++ b/DungeonShooting_Godot/src/game/manager/SoundManager.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Godot; /// @@ -15,70 +16,158 @@ /// public class SoundManager { - public static SoundManager Instance { get => SingleTon.singleTon; } + private static Stack _streamPlayer2DStack = new Stack(); + private static Stack _streamPlayerStack = new Stack(); - private static class SingleTon + /// + /// 2D音频播放节点 + /// + private class AudioPlayer2D : AudioStreamPlayer2D { - internal static SoundManager singleTon = new SoundManager(); + public override void _Ready() + { + Connect("finished", this, nameof(OnPlayFinish)); + } + + public void OnPlayFinish() + { + RecycleAudioPlayer2D(this); + } } - private static AudioStreamPlayer audioStreamPlayer = new AudioStreamPlayer(); - - /// - /// 背景音乐路径 + /// 音频播放节点 /// - public static string BGMPath = "res://resource/sound/bgm/"; - /// - /// 音效路径 - /// - public static string SFXPath = "res://resource/sound/sfx/"; + private class AudioPlayer : AudioStreamPlayer + { + public override void _Ready() + { + Connect("finished", this, nameof(OnPlayFinish)); + } + + public void OnPlayFinish() + { + RecycleAudioPlayer(this); + } + } /// /// 播放声音 用于bgm /// - /// bgm名字 在resource/sound/bgm/目录下 - /// 需要播放声音得节点 将成为音频播放节点的父节点 + /// bgm路径 /// 音量 - public static void PlayeMusic(string soundName, Node node, float volume) + public static AudioStreamPlayer PlayMusic(string soundName, float volume = 0.5f) { - AudioStream sound = ResourceManager.Load(BGMPath + soundName); - if (sound != null) - { - AudioStreamPlayer soundNode = new AudioStreamPlayer(); - node.AddChild(soundNode); - soundNode.Stream = sound; - soundNode.Bus = Enum.GetName(typeof(BUS), 0); - soundNode.VolumeDb = volume; - soundNode.Play(); - } - else - { - GD.Print("没有这个资源!!!"); - } + var sound = ResourceManager.Load(soundName); + var soundNode = GetAudioPlayerInstance(); + GameApplication.Instance.GlobalNodeRoot.AddChild(soundNode); + soundNode.Stream = sound; + soundNode.Bus = Enum.GetName(typeof(BUS), 0); + soundNode.VolumeDb = volume; + soundNode.Play(); + return soundNode; } + /// /// 添加并播放音效 用于音效 /// - /// 音效文件名字 在resource/sound/sfx/目录下 - /// 需要播放声音得节点 将成为音频播放节点的父节点 + /// 音效文件路径 /// 音量 - public static void PlaySoundEffect(string soundName, Node node, float volume = 0f) + public static AudioStreamPlayer PlaySoundEffect(string soundName, float volume = 0.5f) { - AudioStream sound = ResourceManager.Load(SFXPath + soundName); - if (sound != null) + var sound = ResourceManager.Load(soundName); + var soundNode = GetAudioPlayerInstance(); + GameApplication.Instance.GlobalNodeRoot.AddChild(soundNode); + soundNode.Stream = sound; + soundNode.Bus = Enum.GetName(typeof(BUS), 1); + soundNode.VolumeDb = volume; + soundNode.Play(); + return soundNode; + } + + /// + /// 在指定的节点下播放音效 用于音效 + /// + /// 音效文件路径 + /// 发声节点所在全局坐标 + /// 音量 + /// 挂载节点, 为null则挂载到房间根节点下 + public static AudioStreamPlayer2D PlaySoundEffectPosition(string soundName, Vector2 pos, float volume = 0.5f, Node2D target = null) + { + var sound = ResourceManager.Load(soundName); + var soundNode = GetAudioPlayer2DInstance(); + if (target != null) { - AudioStreamPlayer soundNode = new AudioStreamPlayer(); - node.AddChild(soundNode); - soundNode.Stream = sound; - soundNode.Bus = Enum.GetName(typeof(BUS), 1); - soundNode.VolumeDb = volume; - soundNode.Play(); - GD.Print("bus:", soundNode.Bus); + target.AddChild(soundNode); } else { - GD.Print("没有这个资源!!!"); + GameApplication.Instance.GlobalNodeRoot.AddChild(soundNode); } + + soundNode.GlobalPosition = pos; + soundNode.Stream = sound; + soundNode.Bus = Enum.GetName(typeof(BUS), 1); + soundNode.VolumeDb = volume; + soundNode.Play(); + return soundNode; + } + + /// + /// 获取2D音频播放节点 + /// + private static AudioPlayer2D GetAudioPlayer2DInstance() + { + if (_streamPlayer2DStack.Count > 0) + { + return _streamPlayer2DStack.Pop(); + } + + var inst = new AudioPlayer2D(); + inst.AreaMask = 0; + return inst; + } + + /// + /// 获取音频播放节点 + /// + private static AudioPlayer GetAudioPlayerInstance() + { + if (_streamPlayerStack.Count > 0) + { + return _streamPlayerStack.Pop(); + } + + return new AudioPlayer(); + } + + /// + /// 回收2D音频播放节点 + /// + private static void RecycleAudioPlayer2D(AudioPlayer2D inst) + { + var parent = inst.GetParent(); + if (parent != null) + { + parent.RemoveChild(inst); + } + + inst.Stream = null; + _streamPlayer2DStack.Push(inst); + } + + /// + /// 回收音频播放节点 + /// + private static void RecycleAudioPlayer(AudioPlayer inst) + { + var parent = inst.GetParent(); + if (parent != null) + { + parent.RemoveChild(inst); + } + + inst.Stream = null; + _streamPlayerStack.Push(inst); } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/role/Enemy.cs b/DungeonShooting_Godot/src/game/role/Enemy.cs index 922e7f6..ed60cb4 100644 --- a/DungeonShooting_Godot/src/game/role/Enemy.cs +++ b/DungeonShooting_Godot/src/game/role/Enemy.cs @@ -10,10 +10,6 @@ public override void _Process(float delta) { base._Process(delta); - - if (Holster.ActiveWeapon != null) - { - Holster.ActiveWeapon.Trigger(); - } + Attack(); } } diff --git a/DungeonShooting_Godot/src/game/room/RoomManager.cs b/DungeonShooting_Godot/src/game/room/RoomManager.cs index e7769d2..2e28992 100644 --- a/DungeonShooting_Godot/src/game/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/game/room/RoomManager.cs @@ -38,7 +38,7 @@ public override void _Ready() { //播放bgm - SoundManager.PlayeMusic("intro.ogg", this, -17f); + SoundManager.PlayMusic(ResourcePath.resource_sound_bgm_Intro_ogg, -17f); _enemy.LookTarget = Player; _enemy.PickUpWeapon(WeaponManager.GetGun("1001"));