Skip to content

Commit 6f8e3e8

Browse files
committed
Add save game functionality and list command to GameEngine
Implemented methods to manage save game directories and file paths, allowing for saving and loading of game states with custom names. Introduced a command to list available save files, displaying their last modified time and size. Updated UI prompts to reflect new save commands and improved error handling for file operations.
1 parent 26f3b1c commit 6f8e3e8

File tree

1 file changed

+100
-8
lines changed

1 file changed

+100
-8
lines changed

GameEngine.cs

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ public class GameEngine
2121

2222
public bool HasUnsavedChanges { get; private set; }
2323

24+
private string GetSaveDirectory()
25+
{
26+
// Create save directory in user's Documents/RealmOfAethermoor/SavedGames
27+
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
28+
string gameFolder = Path.Combine(documentsPath, "RealmOfAethermoor");
29+
string saveFolder = Path.Combine(gameFolder, "SavedGames");
30+
31+
// Ensure the directory exists
32+
Directory.CreateDirectory(saveFolder);
33+
34+
return saveFolder;
35+
}
36+
37+
private string GetSaveFilePath(string saveName)
38+
{
39+
string saveDir = GetSaveDirectory();
40+
return Path.Combine(saveDir, $"{saveName}.json");
41+
}
42+
2443
public GameEngine(Form1 form)
2544
{
2645
gameForm = form;
@@ -160,6 +179,10 @@ public void ProcessCommand(string input)
160179
case "load":
161180
LoadGame(args.FirstOrDefault());
162181
break;
182+
case "saves":
183+
case "list":
184+
ListSaveFiles();
185+
break;
163186
case "quit":
164187
case "exit":
165188
gameForm.Close();
@@ -540,18 +563,37 @@ public void ShowHelp()
540563
gameForm.DisplayText("Interaction: look, take [item], use [item]");
541564
gameForm.DisplayText("Character: inventory (inv), stats, help");
542565
gameForm.DisplayText("Combat: attack [enemy], defend, flee");
543-
gameForm.DisplayText("Game: save [name], load [name], quit");
566+
gameForm.DisplayText("Game: save [name], load [name], saves/list, quit");
567+
gameForm.DisplayText("");
568+
gameForm.DisplayText("Save Commands:");
569+
gameForm.DisplayText(" save - Quick save with timestamp");
570+
gameForm.DisplayText(" save [name] - Save with custom name");
571+
gameForm.DisplayText(" load - Load quick save");
572+
gameForm.DisplayText(" load [name] - Load specific save");
573+
gameForm.DisplayText(" saves/list - Show all available saves");
544574
gameForm.DisplayText("");
545575
gameForm.DisplayText("Keyboard shortcuts:");
546576
gameForm.DisplayText("Tab - Quick inventory, Ctrl+S - Quick save, Ctrl+L - Quick load, F1 - Help");
547577
gameForm.DisplayText("");
578+
string saveDir = GetSaveDirectory();
579+
gameForm.DisplayText($"Save files are stored in: {saveDir}", Color.Gray);
580+
gameForm.DisplayText("");
548581
}
549582

550583
public void SaveGame(string saveName = null)
551584
{
552585
try
553586
{
554-
saveName = saveName ?? "quicksave";
587+
// Generate a timestamped save name if none provided
588+
if (string.IsNullOrEmpty(saveName))
589+
{
590+
saveName = $"AutoSave_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}";
591+
}
592+
else if (saveName == "quicksave")
593+
{
594+
saveName = "QuickSave";
595+
}
596+
555597
var saveData = new GameSave
556598
{
557599
Player = player,
@@ -561,10 +603,11 @@ public void SaveGame(string saveName = null)
561603
};
562604

563605
string saveJson = JsonSerializer.Serialize(saveData, new JsonSerializerOptions { WriteIndented = true });
564-
Directory.CreateDirectory("saves");
565-
File.WriteAllText($"saves/{saveName}.json", saveJson);
606+
string saveFilePath = GetSaveFilePath(saveName);
607+
File.WriteAllText(saveFilePath, saveJson);
566608

567609
gameForm.DisplayText($"Game saved as '{saveName}'.", Color.Green);
610+
gameForm.DisplayText($"Save location: {saveFilePath}", Color.Gray);
568611
HasUnsavedChanges = false;
569612
}
570613
catch (Exception ex)
@@ -577,16 +620,22 @@ public void LoadGame(string saveName = null)
577620
{
578621
try
579622
{
580-
saveName = saveName ?? "quicksave";
581-
string saveFile = $"saves/{saveName}.json";
623+
// Handle default save name
624+
if (string.IsNullOrEmpty(saveName) || saveName == "quicksave")
625+
{
626+
saveName = "QuickSave";
627+
}
628+
629+
string saveFilePath = GetSaveFilePath(saveName);
582630

583-
if (!File.Exists(saveFile))
631+
if (!File.Exists(saveFilePath))
584632
{
585633
gameForm.DisplayText($"Save file '{saveName}' not found.", Color.Red);
634+
gameForm.DisplayText($"Looked in: {saveFilePath}", Color.Gray);
586635
return;
587636
}
588637

589-
string saveJson = File.ReadAllText(saveFile);
638+
string saveJson = File.ReadAllText(saveFilePath);
590639
var saveData = JsonSerializer.Deserialize<GameSave>(saveJson);
591640

592641
if (saveData?.Player != null && saveData.Locations != null && !string.IsNullOrEmpty(saveData.CurrentLocationKey))
@@ -597,6 +646,7 @@ public void LoadGame(string saveName = null)
597646

598647
gameForm.ClearScreen();
599648
gameForm.DisplayText($"Game loaded from '{saveName}'.", Color.Green);
649+
gameForm.DisplayText($"Loaded from: {saveFilePath}", Color.Gray);
600650
gameForm.DisplayText("");
601651

602652
// Enable game controls since we now have a loaded game
@@ -636,5 +686,47 @@ public string GetCurrentLocationKey()
636686
{
637687
return locations.FirstOrDefault(l => l.Value == currentLocation).Key ?? "village";
638688
}
689+
690+
public void ListSaveFiles()
691+
{
692+
try
693+
{
694+
string saveDir = GetSaveDirectory();
695+
var saveFiles = Directory.GetFiles(saveDir, "*.json")
696+
.Select(f => Path.GetFileNameWithoutExtension(f))
697+
.OrderByDescending(f => File.GetLastWriteTime(Path.Combine(saveDir, $"{f}.json")))
698+
.ToList();
699+
700+
if (saveFiles.Any())
701+
{
702+
gameForm.DisplayText("=== Available Save Files ===", Color.Cyan);
703+
gameForm.DisplayText($"Save directory: {saveDir}", Color.Gray);
704+
gameForm.DisplayText("");
705+
706+
foreach (var saveFile in saveFiles)
707+
{
708+
var filePath = Path.Combine(saveDir, $"{saveFile}.json");
709+
var lastModified = File.GetLastWriteTime(filePath);
710+
var fileSize = new FileInfo(filePath).Length;
711+
712+
gameForm.DisplayText($"• {saveFile}", Color.Yellow);
713+
gameForm.DisplayText($" Modified: {lastModified:yyyy-MM-dd HH:mm:ss}");
714+
gameForm.DisplayText($" Size: {fileSize:N0} bytes");
715+
gameForm.DisplayText("");
716+
}
717+
718+
gameForm.DisplayText("Use 'load [filename]' to load a specific save.", Color.Cyan);
719+
}
720+
else
721+
{
722+
gameForm.DisplayText("No save files found.", Color.Yellow);
723+
gameForm.DisplayText($"Save directory: {saveDir}", Color.Gray);
724+
}
725+
}
726+
catch (Exception ex)
727+
{
728+
gameForm.DisplayText($"Failed to list save files: {ex.Message}", Color.Red);
729+
}
730+
}
639731
}
640732
}

0 commit comments

Comments
 (0)