Skip to content

Commit

Permalink
A very hacky manual-save command.
Browse files Browse the repository at this point in the history
And, we tell the user when we've saved.
  • Loading branch information
nightblade9 committed Nov 22, 2024
1 parent b55e2fc commit d6d963a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
14 changes: 13 additions & 1 deletion source/TextBlade.ConsoleRunner/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public void Run()
AnsiConsole.MarkupLine(message);
}

/// This area stinks: type-specific things...
ApplyResultsIfBattle(command);
if (command is ManuallySaveCommand)
{
SaveGame();
}
}
}

Expand Down Expand Up @@ -100,10 +105,17 @@ private void ApplyResultsIfBattle(ICommand command)
{ "CurrentFloor", dungeon.CurrentFloorNumber },
{ "IsClear", battleCommand.IsVictory }
};
SaveGameManager.SaveGame("default", _currentLocation.LocationId, _party, _inventory, dungeonSaveData);

SaveGame(dungeonSaveData);
}
}

private void SaveGame(Dictionary<string, object>? locationSpecificData = null)
{
SaveGameManager.SaveGame("default", _currentLocation.LocationId, _party, _inventory, locationSpecificData);
AnsiConsole.MarkupLine("[green]Game saved.[/]");
}

private void LoadGameOrStartNewGame()
{
if (SaveGameManager.HasSave("default"))
Expand Down
7 changes: 5 additions & 2 deletions source/TextBlade.ConsoleRunner/IO/InputProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ public static ICommand PromptForAction(Location currentLocation)
// If you update this, update the help listing in ShowHelpCommand.
switch (rawResponse)
{
case "credits":
return new ShowCreditsCommand();
case "quit":
case "q":
return new QuitGameCommand();
case "help":
case "h":
case "p":
return new ShowPartyStatusCommand();
case "credits":
return new ShowCreditsCommand();
case "s":
case "save":
return new ManuallySaveCommand();
case "?":
return new ShowHelpCommand();
}
Expand Down
17 changes: 17 additions & 0 deletions source/TextBlade.Core/Commands/ManuallySaveCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using TextBlade.Core.Characters;
using TextBlade.Core.Game;

namespace TextBlade.Core.Commands;

/// <summary>
/// This stinks. This command is used as a message, of sorts, to communicate to Game.
/// Because Game references TextBlade.Core, we can't reference it the other way around.
/// </summary>
public class ManuallySaveCommand : ICommand
{
public IEnumerable<string> Execute(IGame game, List<Character> party)
{
// This stinks.
return [];
}
}
2 changes: 1 addition & 1 deletion source/TextBlade.Core/IO/SaveData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct SaveData
public string CurrentLocationId { get; set; }
public Inventory Inventory { get; set; }

public Dictionary<string, object> LocationSpecificData { get; set; }
public Dictionary<string, object>? LocationSpecificData { get; set; }

// For user code, i.e. ggame-specific things
public Dictionary<string, object> GameSpecificData { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion source/TextBlade.Core/IO/SaveGameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class SaveGameManager
private const string SaveFolder = "SaveData";
private const string SaveFileExtension = ".save";

public static void SaveGame(string saveSlot, string currentLocationId, List<Character> party, Inventory inventory, Dictionary<string, object> locationSpecificData = null)
public static void SaveGame(string saveSlot, string currentLocationId, List<Character> party, Inventory inventory, Dictionary<string, object>? locationSpecificData = null)
{
var saveData = new SaveData()
{
Expand Down

0 comments on commit d6d963a

Please sign in to comment.