Skip to content

Commit

Permalink
Game auto-saves every minute.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightblade9 committed Nov 22, 2024
1 parent d6d963a commit c84cc28
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions source/TextBlade.ConsoleRunner/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ 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 const int AutoSaveIntervalMinutes = 1;

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

// TODO: investigate something cross-platform with minimal OS-specific dependencies.
// NAudio, System.Windows.Extensions, etc. are all Windows-only. Sigh.
Expand All @@ -43,6 +45,7 @@ public void SetLocation(Location location)
{
_currentLocation = location;
PlayBackgroundAudio();
AutoSaveIfItsBeenAWhile();
}

public void Run()
Expand Down Expand Up @@ -168,4 +171,14 @@ private void PlayBackgroundAudio()
_backgroundAudioPlayer.SoundLocation = Path.Join("Content", "Audio", $"{_currentLocation.BackgroundAudio}.{SupportedAudioExtension}");
_backgroundAudioPlayer.Load();
}

private void AutoSaveIfItsBeenAWhile()
{
var elapsed = DateTime.Now - _lastSaveOn;
if (elapsed.TotalMinutes >= AutoSaveIntervalMinutes)
{
_lastSaveOn = DateTime.Now;
SaveGame();
}
}
}

0 comments on commit c84cc28

Please sign in to comment.