diff --git a/source/TextBlade.ConsoleRunner/Game.cs b/source/TextBlade.ConsoleRunner/Game.cs index 70496b2..964ff3a 100644 --- a/source/TextBlade.ConsoleRunner/Game.cs +++ b/source/TextBlade.ConsoleRunner/Game.cs @@ -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 _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. @@ -43,6 +45,7 @@ public void SetLocation(Location location) { _currentLocation = location; PlayBackgroundAudio(); + AutoSaveIfItsBeenAWhile(); } public void Run() @@ -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(); + } + } }