Skip to content

Commit

Permalink
Background audio works. Plays per location if present. In a loop.
Browse files Browse the repository at this point in the history
Windows only.
WAVE file only.
  • Loading branch information
nightblade9 committed Nov 21, 2024
1 parent b2e2561 commit cf3fb20
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
28 changes: 27 additions & 1 deletion source/TextBlade.ConsoleRunner/Game.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Spectre.Console;
using System.Media;
using Spectre.Console;
using TextBlade.ConsoleRunner.IO;
using TextBlade.Core.Characters;
using TextBlade.Core.Commands;
Expand All @@ -14,21 +15,34 @@ namespace TextBlade.ConsoleRunner;
/// </summary>
public class Game : IGame
{
// Don't kill the messenger. I swear, it's bad enough this only works on Windows.
private const string SupportedAudioExtension = "wav";

private Location _currentLocation = null!;
private bool _isRunning = true;
private List<Character> _party = new();
private Inventory _inventory = new();

// TODO: investigate something cross-platform with minimal OS-specific dependencies.
// NAudio, System.Windows.Extensions, etc. are all Windows-only. Sigh.
private SoundPlayer _backgroundAudioPlayer = new();

public static IGame Current { get; private set; }

public Game()
{
Current = this;
_backgroundAudioPlayer.LoadCompleted += (sender, args) => _backgroundAudioPlayer.PlayLooping();
}

/// <summary>
/// Called whenever a location changes. Sleeping in an inn, descending a dungeon, do NOT trigger this.
/// </summary>
/// <param name="location"></param>
public void SetLocation(Location location)
{
_currentLocation = location;
PlayBackgroundAudio();
}

public void Run()
Expand Down Expand Up @@ -130,4 +144,16 @@ private void UnpackLocationSpecificdata(SaveData data)
dungeon.SetState(floorNumber, isClear);
}
}

private void PlayBackgroundAudio()
{
_backgroundAudioPlayer.Stop();
if (string.IsNullOrWhiteSpace(_currentLocation.BackgroundAudio))
{
return;
}

_backgroundAudioPlayer.SoundLocation = Path.Join("Content", "Audio", $"{_currentLocation.BackgroundAudio}.{SupportedAudioExtension}");
_backgroundAudioPlayer.Load();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="System.Windows.Extensions" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions source/TextBlade.Core/Locations/BaseLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public abstract class BaseLocation
{
public string Name { get; set; }
public string Description { get; set; }
public string BackgroundAudio { get; set; } = string.Empty;

/// <summary>
/// For custom code, this is the class name of the code-behind class for this location.
Expand Down
Binary file added source/VoidWalker.Main/Content/Audio/seagulls.wav
Binary file not shown.
10 changes: 7 additions & 3 deletions source/VoidWalker.Main/VoidWalker.Main.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
</Content>
</ItemGroup>

<ItemGroup>
<Compile Include="Code\\Locations\ThroneRoom.cs" />
<ItemGroup>
<Compile Include="Code\\Locations\ThroneRoom.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TextBlade.ConsoleRunner\TextBlade.ConsoleRunner.csproj" />
<ProjectReference Include="..\TextBlade.Core\TextBlade.Core.csproj" />
</ItemGroup>


<ItemGroup>
<Folder Include="Content\Audio\" />
</ItemGroup>

</Project>

0 comments on commit cf3fb20

Please sign in to comment.