Skip to content

Commit

Permalink
Somewhat simplify code.
Browse files Browse the repository at this point in the history
Pass around save data instead of a party list or game.
It mostly works.
  • Loading branch information
nightblade9 committed Dec 18, 2024
1 parent 222f1b9 commit 59c5821
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 51 deletions.
8 changes: 4 additions & 4 deletions source/TextBlade.ConsoleRunner/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public void Run()
_locationDisplayer.ShowLocation(_currentLocation);
}

var command = new InputProcessor(_console).PromptForAction(_currentLocation);
var command = new InputProcessor(this, _console).PromptForAction(_currentLocation);
previousLocation = _currentLocation;

command.Execute(this, _saveData.Party);
command.Execute(_saveData);

/// This area stinks: type-specific things...
new BattleResultsApplier(_console).ApplyResultsIfBattle(command, _currentLocation, _saveData);
Expand Down Expand Up @@ -150,7 +150,7 @@ private void LoadGameOrStartNewGame()
{
_saveData = SaveGameManager.LoadGame("default");
GameSwitches.Switches = _saveData.Switches;
new ChangeLocationCommand(_saveData.CurrentLocationId).Execute(this, _saveData.Party);
new ChangeLocationCommand(this, _saveData.CurrentLocationId).Execute(_saveData);

if (_saveData.LocationSpecificDataLocationId == _currentLocation.LocationId)
{
Expand All @@ -167,7 +167,7 @@ private void LoadGameOrStartNewGame()
_saveData.Inventory = new();

var startLocationId = runner.GetStartingLocationId();
new ChangeLocationCommand(startLocationId).Execute(this, _saveData.Party);
new ChangeLocationCommand(this, startLocationId).Execute(_saveData);
_console.WriteLine("New game started. For help, type \"help\"");
}
}
Expand Down
10 changes: 8 additions & 2 deletions source/TextBlade.ConsoleRunner/IO/InputProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using TextBlade.Core.Commands;
using TextBlade.Core.Commands.Display;
using TextBlade.Core.Game;
using TextBlade.Core.IO;
using TextBlade.Core.Locations;

Expand All @@ -8,9 +9,14 @@ namespace TextBlade.ConsoleRunner.IO;
public class InputProcessor
{
private readonly IConsole _console;
private readonly IGame _game;

public InputProcessor(IConsole console)
public InputProcessor(IGame game, IConsole console)
{
ArgumentNullException.ThrowIfNull(game);
ArgumentNullException.ThrowIfNull(console);

_game = game;
_console = console;
}

Expand Down Expand Up @@ -39,7 +45,7 @@ public ICommand PromptForAction(Location currentLocation)
}

var destination = currentLocation.LinkedLocations[destinationOption - 1];
return new ChangeLocationCommand(destination.Id);
return new ChangeLocationCommand(_game, destination.Id);
}

// Nah, nah, it's not a destination, just a global command.
Expand Down
14 changes: 5 additions & 9 deletions source/TextBlade.Core/Battle/CharacterTurnProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
using System.Text;
using TextBlade.Core.Characters;
using TextBlade.Core.Commands.Display;
using TextBlade.Core.Game;
using TextBlade.Core.IO;

namespace TextBlade.Core.Battle;

public class CharacterTurnProcessor
{
private readonly List<Monster> _monsters;
private readonly IGame _game;
private readonly SaveData _saveData;
private readonly IConsole _console;

private readonly List<Character> _party;
private readonly char[] validInputs = ['a', 'i', 's', 'd'];

public CharacterTurnProcessor(IGame game, IConsole console, List<Character> party, List<Monster> monsters)
public CharacterTurnProcessor(IConsole console, SaveData saveData, List<Monster> monsters)

Check warning on line 16 in source/TextBlade.Core/Battle/CharacterTurnProcessor.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_party' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
ArgumentNullException.ThrowIfNull(game);
ArgumentNullException.ThrowIfNull(console);
ArgumentNullException.ThrowIfNull(party);
ArgumentNullException.ThrowIfNull(saveData);
ArgumentNullException.ThrowIfNull(monsters);

_game = game;
_console = console;
_party = party;
_saveData = saveData;
_monsters = monsters;
}

Expand Down Expand Up @@ -59,7 +55,7 @@ internal void ProcessTurnFor(Character character)
new SkillApplier(_console).Apply(character, skill, targets);
break;
case 'i':
new ShowInventoryCommand(_console, true).Execute(_game, _party);
new ShowInventoryCommand(_console, true).Execute(_saveData);
break;
default:
_console.WriteLine("Invalid input!");
Expand Down
11 changes: 7 additions & 4 deletions source/TextBlade.Core/Commands/ChangeLocationCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using TextBlade.Core.Characters;
using TextBlade.Core.Game;
using TextBlade.Core.IO;
using TextBlade.Core.Locations;
Expand All @@ -10,24 +9,28 @@ public class ChangeLocationCommand : ICommand
// Used for saving, so we know the ID of our current location
private readonly string _locationId;
private readonly string _locationPath;
private readonly IGame _game;

public ChangeLocationCommand(string destinationId)
public ChangeLocationCommand(IGame game, string destinationId)
{
ArgumentNullException.ThrowIfNull(game);

var locationFileName = destinationId.ToString().Replace('/', Path.DirectorySeparatorChar);
var locationPath = Path.Join("Content", $"{locationFileName}.json");
if (!File.Exists(locationPath))
{
throw new InvalidOperationException($"{locationPath} doesn't seem to exist!");
}

_game = game;
_locationId = destinationId;
_locationPath = locationPath;
}

public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
var locationData = Serializer.Deserialize<Location>(File.ReadAllText(_locationPath));
locationData.LocationId = _locationId;
game.SetLocation(locationData);
_game.SetLocation(locationData);
}
}
5 changes: 2 additions & 3 deletions source/TextBlade.Core/Commands/Display/LookCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using TextBlade.Core.Characters;
using TextBlade.Core.Game;
using TextBlade.Core.IO;

namespace TextBlade.Core.Commands.Display;

Expand All @@ -9,7 +8,7 @@ namespace TextBlade.Core.Commands.Display;
/// </summary>
public class LookCommand : ICommand
{
public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
// This stinks.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public ShowCreditsCommand(IConsole console)
_console = console;
}

public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
var creditsFilePath = Path.Join("Content", "Credits.txt");
if (File.Exists(creditsFilePath))
Expand Down
2 changes: 1 addition & 1 deletion source/TextBlade.Core/Commands/Display/ShowHelpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ShowHelpCommand(IConsole console)
{ "credits", "Shows the credits" },
};

public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
var toReturn = new List<string>
{
Expand Down
13 changes: 7 additions & 6 deletions source/TextBlade.Core/Commands/Display/ShowInventoryCommand.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
using TextBlade.Core.Battle;
using TextBlade.Core.Characters;
using TextBlade.Core.Characters.PartyManagement;
using TextBlade.Core.Game;
using TextBlade.Core.IO;

namespace TextBlade.Core.Commands.Display;

public class ShowInventoryCommand : ICommand
{
private readonly IConsole _console;

private readonly EquipmentEquipper _equipper;
private readonly ItemUser _itemUser;

private bool _isInBattle = false;

public ShowInventoryCommand(IConsole console, bool isInBattle = false)
{
ArgumentNullException.ThrowIfNull(console);

_console = console;
_equipper = new(console);
_itemUser = new(console);

_isInBattle = isInBattle;
}

public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
_console.WriteLine("Inventory:");

var inventory = game.Inventory;
var inventory = saveData.Inventory;
var items = inventory.ItemsInOrder;
if (_isInBattle)
{
Expand Down Expand Up @@ -73,10 +74,10 @@ public void Execute(IGame game, List<Character> party)
case Inv.ItemType.Helmet:
case Inv.ItemType.Armour:
case Inv.ItemType.Weapon:
_equipper.EquipIfRequested(itemData, inventory, party);
_equipper.EquipIfRequested(itemData, inventory, saveData.Party);
break;
case Inv.ItemType.Consumable:
_itemUser.UseIfRequested(itemData, inventory, party);
_itemUser.UseIfRequested(itemData, inventory, saveData.Party);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public ShowPartyStatusCommand(IConsole console)
_console = console;
}

public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
_console.WriteLine("Party status:");

foreach (var member in party)
foreach (var member in saveData.Party)
{
var equipment = string.Join(", ", member.Equipment.Values.Select(e => $"{e.Name}: {e}"));

Expand Down
4 changes: 2 additions & 2 deletions source/TextBlade.Core/Commands/DoNothingCommand.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using TextBlade.Core.Characters;
using TextBlade.Core.Game;
using TextBlade.Core.IO;

namespace TextBlade.Core.Commands;

public class DoNothingCommand : ICommand
{
public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
}
}
4 changes: 2 additions & 2 deletions source/TextBlade.Core/Commands/ICommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using TextBlade.Core.Characters;
using TextBlade.Core.Game;
using TextBlade.Core.IO;

namespace TextBlade.Core.Commands;

Expand All @@ -9,5 +9,5 @@ public interface ICommand
/// Execute a command. And return strings to run through console.
/// Everything else is here so we can act on it.
/// </summary>
public void Execute(IGame game, List<Character> party);
public void Execute(SaveData saveData);
}
4 changes: 2 additions & 2 deletions source/TextBlade.Core/Commands/ManuallySaveCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using TextBlade.Core.Characters;
using TextBlade.Core.Game;
using TextBlade.Core.IO;

namespace TextBlade.Core.Commands;

Expand All @@ -9,7 +9,7 @@ namespace TextBlade.Core.Commands;
/// </summary>
public class ManuallySaveCommand : ICommand
{
public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
// This stinks.
}
Expand Down
4 changes: 1 addition & 3 deletions source/TextBlade.Core/Commands/QuitGameCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using TextBlade.Core.Characters;
using TextBlade.Core.Game;
using TextBlade.Core.IO;

namespace TextBlade.Core.Commands;
Expand All @@ -13,7 +11,7 @@ public QuitGameCommand(IConsole console)
_console = console;
}

public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
_console.WriteLine($"Quit the game? Are you sure? [{Colours.Command}]y[/]/[{Colours.Command}]n[/]");
var input = _console.ReadKey();
Expand Down
5 changes: 2 additions & 3 deletions source/TextBlade.Core/Commands/SleepAtInnCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public SleepAtInnCommand(IConsole console, int innCost)
_innCost = innCost;
}

public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
// Check if we have enough gold BEFORE THIS POINT. Subtract if we do!

foreach (var character in party)
foreach (var character in saveData.Party)
{
character.FullyHeal();
}
Expand Down
15 changes: 8 additions & 7 deletions source/TextBlade.Core/Commands/TakeTurnsBattleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class TakeTurnsBattleCommand : ICommand, IBattleCommand
public bool IsVictory { get; private set; }

private readonly List<Monster> _monsters = new();
private readonly IGame _game;
private readonly IConsole _console;

static TakeTurnsBattleCommand()
Expand Down Expand Up @@ -77,23 +78,23 @@ public TakeTurnsBattleCommand(IConsole console, List<string> monsterNames)
}
}

public void Execute(IGame game, List<Character> party)
public void Execute(SaveData saveData)
{
// Problem: we don't have access to AnsiConsole in this layer. Nor can we wait for the Game class
// to process it, because it's an interactive battle. That ... sucks...
var isPartyWipedOut = () => party.TrueForAll(p => p.CurrentHealth <= 0);
var isPartyWipedOut = () => saveData.Party.TrueForAll(p => p.CurrentHealth <= 0);
var areMonstersDefeated = () => _monsters.TrueForAll(m => m.CurrentHealth <= 0);
var isBattleOver = () => isPartyWipedOut() || areMonstersDefeated();
var characterTurnProcessor = new CharacterTurnProcessor(game, _console, party, _monsters);
var characterTurnProcessor = new CharacterTurnProcessor(_console, saveData, _monsters);

while (!isBattleOver())
{
var monstersStatus = string.Join(", ", _monsters.Select(m => $"{m.Name}: {m.CurrentHealth}/{m.TotalHealth} health"));
_console.WriteLine($"You face: [{Colours.Highlight}]{monstersStatus}[/]");
var partyStatus = string.Join(", ", party);
var partyStatus = string.Join(", ", saveData.Party);
_console.WriteLine($"Your party: [{Colours.Highlight}]{partyStatus}[/]");

foreach (var character in party)
foreach (var character in saveData.Party)
{
if (character.CurrentHealth <= 0)
{
Expand All @@ -115,7 +116,7 @@ public void Execute(IGame game, List<Character> party)
continue;
}

new BasicMonsterAi(_console, party).ProcessTurnFor(monster);
new BasicMonsterAi(_console, saveData.Party).ProcessTurnFor(monster);
}

foreach (var e in _monsters)
Expand All @@ -131,7 +132,7 @@ public void Execute(IGame game, List<Character> party)
}
}

foreach (var e in party)
foreach (var e in saveData.Party)
{
if (e.CurrentHealth <= 0)
{
Expand Down

0 comments on commit 59c5821

Please sign in to comment.