Refactor more stuff (Audio mostly)

This commit is contained in:
2025-03-10 20:52:39 -07:00
parent 23049b3231
commit a1c26ed7fb
45 changed files with 861 additions and 846 deletions

View File

@@ -0,0 +1,7 @@
using Zennysoft.Game.Implementation;
namespace Zennsoft.Game.Ma;
public partial class BGMPlayer : DimmableAudioStreamPlayer
{
}

View File

@@ -0,0 +1 @@
uid://d2usinntpmcry

View File

@@ -1,58 +0,0 @@
namespace Zennysoft.Game.Ma;
using Chickensoft.GodotNodeInterfaces;
using Godot;
public interface IDimmableAudioStreamPlayer : IAudioStreamPlayer
{
/// <summary>Fade this dimmable audio stream track in.</summary>
public void FadeIn();
/// <summary>Fade this dimmable audio stream track out.</summary>
public void FadeOut();
}
public partial class DimmableAudioStreamPlayer :
AudioStreamPlayer, IDimmableAudioStreamPlayer
{
#region Constants
// -60 to -80 is considered inaudible for decibels.
public const float VOLUME_DB_INAUDIBLE = -80f;
public const double FADE_DURATION = 3d; // seconds
#endregion Constants
public ITween? FadeTween { get; set; }
public float InitialVolumeDb;
public override void _Ready()
{
InitialVolumeDb = VolumeDb;
VolumeDb = VOLUME_DB_INAUDIBLE;
}
public void FadeIn()
{
SetupFade(InitialVolumeDb, Tween.EaseType.Out);
Play();
}
public void FadeOut()
{
SetupFade(VOLUME_DB_INAUDIBLE, Tween.EaseType.In);
FadeTween!.TweenCallback(Callable.From(Stop));
}
public void SetupFade(float volumeDb, Tween.EaseType ease)
{
FadeTween?.Kill();
FadeTween = GodotInterfaces.Adapt<ITween>(CreateTween());
FadeTween.TweenProperty(
this,
"volume_db",
volumeDb,
FADE_DURATION
).SetTrans(Tween.TransitionType.Circ).SetEase(ease);
}
}

View File

@@ -4,6 +4,7 @@ using Chickensoft.Introspection;
using Godot;
using Zennysoft.Game.Abstractions;
using Zennysoft.Ma.Adapter;
using Zennysoft.Ma.Adapter;
namespace Zennysoft.Game.Ma;
@@ -55,85 +56,85 @@ public partial class InGameAudio : Node
public void Setup()
{
InGameAudioLogic = new InGameAudioLogic();
InGameAudioLogic = new InGameAudioLogic();
}
public void OnResolved()
{
InGameAudioLogic.Set(AppRepo);
InGameAudioLogic.Set(GameEventDepot);
InGameAudioLogic.Set(Player);
InGameAudioLogic.Set(GameRepo);
InGameAudioLogic.Set(AppRepo);
InGameAudioLogic.Set(GameEventDepot);
InGameAudioLogic.Set(Player);
InGameAudioLogic.Set(GameRepo);
InGameAudioBinding = InGameAudioLogic.Bind();
InGameAudioBinding = InGameAudioLogic.Bind();
InGameAudioBinding
.Handle((in InGameAudioLogic.Output.PlayOverworldMusic _) => StartOverworldMusic())
.Handle((in InGameAudioLogic.Output.PlayDungeonThemeAMusic _) => StartDungeonThemeA())
.Handle((in InGameAudioLogic.Output.PlayMenuScrollSound _) => PlayMenuScrollSound())
.Handle((in InGameAudioLogic.Output.PlayEquipSound _) => PlayEquipSound())
.Handle((in InGameAudioLogic.Output.PlayMenuBackSound _) => PlayMenuBackSound())
.Handle((in InGameAudioLogic.Output.PlayInventorySortedSound _) => PlayInventorySortedSound())
.Handle((in InGameAudioLogic.Output.PlayHealingItemSound _) => PlayHealingItemSound())
.Handle((in InGameAudioLogic.Output.PlayTeleportSound _) => PlayTeleportSound())
.Handle((in InGameAudioLogic.Output.PlayPlayerAttackSound _) => { PlayerAttackSFX.Stop(); PlayerAttackSFX.Play(); })
.Handle((in InGameAudioLogic.Output.PlayPlayerAttackWallSound _) => { PlayerAttackWallSFX.Stop(); PlayerAttackWallSFX.Play(); });
InGameAudioBinding
.Handle((in InGameAudioLogic.Output.PlayOverworldMusic _) => StartOverworldMusic())
.Handle((in InGameAudioLogic.Output.PlayDungeonThemeAMusic _) => StartDungeonThemeA())
.Handle((in InGameAudioLogic.Output.PlayMenuScrollSound _) => PlayMenuScrollSound())
.Handle((in InGameAudioLogic.Output.PlayEquipSound _) => PlayEquipSound())
.Handle((in InGameAudioLogic.Output.PlayMenuBackSound _) => PlayMenuBackSound())
.Handle((in InGameAudioLogic.Output.PlayInventorySortedSound _) => PlayInventorySortedSound())
.Handle((in InGameAudioLogic.Output.PlayHealingItemSound _) => PlayHealingItemSound())
.Handle((in InGameAudioLogic.Output.PlayTeleportSound _) => PlayTeleportSound())
.Handle((in InGameAudioLogic.Output.PlayPlayerAttackSound _) => { PlayerAttackSFX.Stop(); PlayerAttackSFX.Play(); })
.Handle((in InGameAudioLogic.Output.PlayPlayerAttackWallSound _) => { PlayerAttackWallSFX.Stop(); PlayerAttackWallSFX.Play(); });
InGameAudioLogic.Start();
InGameAudioLogic.Start();
}
public void OnExitTree()
{
InGameAudioLogic.Stop();
InGameAudioBinding.Dispose();
InGameAudioLogic.Stop();
InGameAudioBinding.Dispose();
}
private void StartOverworldMusic()
{
OverworldBgm.Stop();
OverworldBgm.FadeIn();
OverworldBgm.Stop();
OverworldBgm.FadeIn();
}
private void StartDungeonThemeA()
{
OverworldBgm.FadeOut();
DungeonThemeABgm.Stop();
DungeonThemeABgm.FadeIn();
OverworldBgm.FadeOut();
DungeonThemeABgm.Stop();
DungeonThemeABgm.FadeIn();
}
private void PlayMenuScrollSound()
{
MenuScrollSFX.Stop();
MenuScrollSFX.Play();
MenuScrollSFX.Stop();
MenuScrollSFX.Play();
}
private void PlayEquipSound()
{
EquipSFX.Stop();
EquipSFX.Play();
EquipSFX.Stop();
EquipSFX.Play();
}
private void PlayMenuBackSound()
{
MenuBackSFX.Stop();
MenuBackSFX.Play();
MenuBackSFX.Stop();
MenuBackSFX.Play();
}
private void PlayInventorySortedSound()
{
InventorySortedSFX.Stop();
InventorySortedSFX.Play();
InventorySortedSFX.Stop();
InventorySortedSFX.Play();
}
private void PlayHealingItemSound()
{
HealingItemSFX.Stop();
HealingItemSFX.Play();
HealingItemSFX.Stop();
HealingItemSFX.Play();
}
private void PlayTeleportSound()
{
TeleportSFX.Stop();
TeleportSFX.Play();
TeleportSFX.Stop();
TeleportSFX.Play();
}
}

View File

@@ -2,8 +2,8 @@
[ext_resource type="Script" uid="uid://2mnouyn1jcqs" path="res://src/audio/InGameAudio.cs" id="1_gpmcr"]
[ext_resource type="AudioStream" uid="uid://dfu0fksb6slhx" path="res://src/audio/music/droney.mp3" id="2_8hfyr"]
[ext_resource type="Script" uid="uid://br4e8xfwd73if" path="res://src/audio/DimmableAudioStreamPlayer.cs" id="2_857rw"]
[ext_resource type="AudioStream" uid="uid://d2jrktp06xsba" path="res://src/audio/music/crossing-the-gate.mp3" id="3_wbmd6"]
[ext_resource type="Script" path="res://src/audio/BGMPlayer.cs" id="3_wtvpb"]
[ext_resource type="AudioStream" uid="uid://dn2e2hqujlia1" path="res://src/audio/music/tar-winds.mp3" id="4_surnl"]
[ext_resource type="AudioStream" uid="uid://t3g04u722f2k" path="res://src/audio/music/useless immune system-1.mp3" id="6_agr3r"]
[ext_resource type="AudioStream" uid="uid://cn8cugshq3o8k" path="res://src/audio/sfx/PlayerHitWallSFX.wav" id="7_8vh2f"]
@@ -19,84 +19,88 @@
process_mode = 3
script = ExtResource("1_gpmcr")
[node name="MenuBgm" type="AudioStreamPlayer" parent="."]
[node name="BGM" type="Node" parent="."]
[node name="MenuBgm" type="AudioStreamPlayer" parent="BGM"]
unique_name_in_owner = true
stream = ExtResource("2_8hfyr")
parameters/looping = true
script = ExtResource("2_857rw")
script = ExtResource("3_wtvpb")
[node name="OverworldBgm" type="AudioStreamPlayer" parent="."]
[node name="OverworldBgm" type="AudioStreamPlayer" parent="BGM"]
unique_name_in_owner = true
stream = ExtResource("3_wbmd6")
volume_db = -15.0
parameters/looping = true
script = ExtResource("2_857rw")
script = ExtResource("3_wtvpb")
[node name="DungeonThemeABgm" type="AudioStreamPlayer" parent="."]
[node name="DungeonThemeABgm" type="AudioStreamPlayer" parent="BGM"]
unique_name_in_owner = true
stream = ExtResource("4_surnl")
volume_db = -15.0
parameters/looping = true
script = ExtResource("2_857rw")
script = ExtResource("3_wtvpb")
[node name="DungeonThemeBBgm" type="AudioStreamPlayer" parent="."]
[node name="DungeonThemeBBgm" type="AudioStreamPlayer" parent="BGM"]
unique_name_in_owner = true
stream = ExtResource("6_agr3r")
volume_db = -15.0
parameters/looping = true
script = ExtResource("2_857rw")
script = ExtResource("3_wtvpb")
[node name="DungeonThemeCBgm" type="AudioStreamPlayer" parent="."]
[node name="DungeonThemeCBgm" type="AudioStreamPlayer" parent="BGM"]
unique_name_in_owner = true
volume_db = -15.0
script = ExtResource("2_857rw")
script = ExtResource("3_wtvpb")
[node name="Title_Bgm" type="AudioStreamPlayer" parent="."]
[node name="Title_Bgm" type="AudioStreamPlayer" parent="BGM"]
unique_name_in_owner = true
volume_db = -15.0
script = ExtResource("2_857rw")
script = ExtResource("3_wtvpb")
[node name="PlayerAttackSFX" type="AudioStreamPlayer" parent="."]
[node name="SFX" type="Node" parent="."]
[node name="PlayerAttackSFX" type="AudioStreamPlayer" parent="SFX"]
unique_name_in_owner = true
stream = ExtResource("7_wtvpb")
volume_db = -5.0
[node name="PlayerAttackWallSFX" type="AudioStreamPlayer" parent="."]
[node name="PlayerAttackWallSFX" type="AudioStreamPlayer" parent="SFX"]
unique_name_in_owner = true
stream = ExtResource("7_8vh2f")
volume_db = -5.0
[node name="MenuScrollSFX" type="AudioStreamPlayer" parent="."]
[node name="MenuScrollSFX" type="AudioStreamPlayer" parent="SFX"]
unique_name_in_owner = true
stream = ExtResource("7_777nl")
volume_db = -10.0
[node name="MenuBackSFX" type="AudioStreamPlayer" parent="."]
[node name="MenuBackSFX" type="AudioStreamPlayer" parent="SFX"]
unique_name_in_owner = true
stream = ExtResource("8_1xcgo")
volume_db = -10.0
[node name="EquipSFX" type="AudioStreamPlayer" parent="."]
[node name="EquipSFX" type="AudioStreamPlayer" parent="SFX"]
unique_name_in_owner = true
stream = ExtResource("8_kwybb")
volume_db = -10.0
[node name="HealSFX" type="AudioStreamPlayer" parent="."]
[node name="HealSFX" type="AudioStreamPlayer" parent="SFX"]
unique_name_in_owner = true
stream = ExtResource("10_3lcw5")
volume_db = -10.0
[node name="TeleportSFX" type="AudioStreamPlayer" parent="."]
[node name="TeleportSFX" type="AudioStreamPlayer" parent="SFX"]
unique_name_in_owner = true
stream = ExtResource("11_offhc")
volume_db = -18.0
[node name="InventorySortedSFX" type="AudioStreamPlayer" parent="."]
[node name="InventorySortedSFX" type="AudioStreamPlayer" parent="SFX"]
unique_name_in_owner = true
stream = ExtResource("12_wprjr")
volume_db = -15.0
[node name="HealingItemSFX" type="AudioStreamPlayer" parent="."]
[node name="HealingItemSFX" type="AudioStreamPlayer" parent="SFX"]
unique_name_in_owner = true
stream = ExtResource("10_3lcw5")
volume_db = -15.0

View File

@@ -1,35 +0,0 @@
namespace Zennysoft.Game.Ma;
public partial class InGameAudioLogic
{
public static class Output
{
#region BGM
public readonly record struct PlayOverworldMusic;
public readonly record struct PlayDungeonThemeAMusic;
#endregion
#region SFX
public readonly record struct PlayPlayerAttackSound;
public readonly record struct PlayPlayerAttackWallSound;
public readonly record struct PlayMenuScrollSound;
public readonly record struct PlayEquipSound;
public readonly record struct PlayInventorySortedSound;
public readonly record struct PlayMenuBackSound;
public readonly record struct PlayHealingItemSound;
public readonly record struct PlayTeleportSound;
#endregion
public readonly record struct PlayGameMusic;
public readonly record struct StopGameMusic;
}
}

View File

@@ -1 +0,0 @@
uid://bfnbplmd35454

View File

@@ -1,12 +0,0 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma;
public partial class InGameAudioLogic
{
[Meta]
public partial record State : StateLogic<State>
{
}
}

View File

@@ -1 +0,0 @@
uid://c8cfpu81338hk

View File

@@ -1,13 +0,0 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma;
public interface IInGameAudioLogic : ILogicBlock<InGameAudioLogic.State>;
[Meta]
[LogicBlock(typeof(State))]
public partial class InGameAudioLogic :
LogicBlock<InGameAudioLogic.State>, IInGameAudioLogic
{
public override Transition GetInitialState() => To<Enabled>();
}

View File

@@ -1 +0,0 @@
uid://on5thilbaogw

View File

@@ -1,11 +0,0 @@
using Chickensoft.Introspection;
namespace Zennysoft.Game.Ma;
public partial class InGameAudioLogic
{
[Meta]
public partial record Disabled : State
{
}
}

View File

@@ -1,74 +0,0 @@
using Chickensoft.Introspection;
using Zennysoft.Ma.Adapter;
namespace Zennysoft.Game.Ma;
public partial class InGameAudioLogic
{
[Meta]
public partial record Enabled : State
{
public Enabled()
{
OnAttach(() =>
{
var player = Get<IPlayer>();
OnOverworldEntered();
var gameEventDepot = Get<IGameEventDepot>();
var gameRepo = Get<IGameRepo>();
gameEventDepot.OverworldEntered += OnOverworldEntered;
gameEventDepot.DungeonAThemeAreaEntered += OnDungeonAThemeEntered;
gameEventDepot.MenuScrolled += OnMenuScrolled;
gameEventDepot.MenuBackedOut += OnMenuBackedOut;
player.EquippedWeapon.Changed += OnEquippedItem;
player.EquippedArmor.Changed += OnEquippedItem;
player.EquippedAccessory.Changed += OnEquippedItem;
gameEventDepot.InventorySorted += OnInventorySorted;
gameEventDepot.HealingItemConsumed += OnHealingItemConsumed;
gameEventDepot.RestorativePickedUp += OnRestorativePickedUp;
gameEventDepot.TeleportEntered += OnTeleportEntered;
gameRepo.PlayerAttack += OnPlayerAttack;
gameRepo.PlayerAttackedWall += OnPlayerAttackWall;
});
OnDetach(() =>
{
var gameEventDepot = Get<IGameEventDepot>();
var player = Get<IPlayer>();
var gameRepo = Get<IGameRepo>();
gameEventDepot.OverworldEntered -= OnOverworldEntered;
gameEventDepot.DungeonAThemeAreaEntered -= OnDungeonAThemeEntered;
gameEventDepot.MenuScrolled -= OnMenuScrolled;
gameEventDepot.MenuBackedOut -= OnMenuBackedOut;
player.EquippedWeapon.Changed -= OnEquippedItem;
player.EquippedArmor.Changed -= OnEquippedItem;
player.EquippedAccessory.Changed -= OnEquippedItem;
gameEventDepot.InventorySorted -= OnInventorySorted;
gameEventDepot.TeleportEntered -= OnTeleportEntered;
gameRepo.PlayerAttack -= OnPlayerAttack;
gameRepo.PlayerAttackedWall -= OnPlayerAttackWall;
});
}
private void OnPlayerAttack() => Output(new Output.PlayPlayerAttackSound());
private void OnPlayerAttackWall() => Output(new Output.PlayPlayerAttackWallSound());
private void OnRestorativePickedUp(Restorative restorative) => Output(new Output.PlayHealingItemSound());
private void OnMenuBackedOut() => Output(new Output.PlayMenuBackSound());
private void OnHealingItemConsumed(ConsumableItemStats stats) => Output(new Output.PlayHealingItemSound());
private void OnInventorySorted() => Output(new Output.PlayInventorySortedSound());
private void OnEquippedItem(EquipableItem equipableItem) => Output(new Output.PlayEquipSound());
private void OnOverworldEntered() => Output(new Output.PlayOverworldMusic());
private void OnDungeonAThemeEntered() => Output(new Output.PlayDungeonThemeAMusic());
private void OnMenuScrolled() => Output(new Output.PlayMenuScrollSound());
private void OnTeleportEntered() => Output(new Output.PlayTeleportSound());
}
}