diff --git a/source/TextBlade.ConsoleRunner/Game.cs b/source/TextBlade.ConsoleRunner/Game.cs index e95fd86..331fa7b 100644 --- a/source/TextBlade.ConsoleRunner/Game.cs +++ b/source/TextBlade.ConsoleRunner/Game.cs @@ -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); @@ -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) { @@ -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\""); } } diff --git a/source/TextBlade.ConsoleRunner/IO/InputProcessor.cs b/source/TextBlade.ConsoleRunner/IO/InputProcessor.cs index 54be2e7..4d7b681 100644 --- a/source/TextBlade.ConsoleRunner/IO/InputProcessor.cs +++ b/source/TextBlade.ConsoleRunner/IO/InputProcessor.cs @@ -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; @@ -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; } @@ -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. diff --git a/source/TextBlade.Core/Battle/CharacterTurnProcessor.cs b/source/TextBlade.Core/Battle/CharacterTurnProcessor.cs index babedcf..1d41f99 100644 --- a/source/TextBlade.Core/Battle/CharacterTurnProcessor.cs +++ b/source/TextBlade.Core/Battle/CharacterTurnProcessor.cs @@ -1,7 +1,5 @@ -using System.Text; using TextBlade.Core.Characters; using TextBlade.Core.Commands.Display; -using TextBlade.Core.Game; using TextBlade.Core.IO; namespace TextBlade.Core.Battle; @@ -9,22 +7,20 @@ namespace TextBlade.Core.Battle; public class CharacterTurnProcessor { private readonly List _monsters; - private readonly IGame _game; + private readonly SaveData _saveData; private readonly IConsole _console; private readonly List _party; private readonly char[] validInputs = ['a', 'i', 's', 'd']; - public CharacterTurnProcessor(IGame game, IConsole console, List party, List monsters) + public CharacterTurnProcessor(IConsole console, SaveData saveData, List monsters) { - 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; } @@ -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!"); diff --git a/source/TextBlade.Core/Commands/ChangeLocationCommand.cs b/source/TextBlade.Core/Commands/ChangeLocationCommand.cs index eb46869..3eef263 100644 --- a/source/TextBlade.Core/Commands/ChangeLocationCommand.cs +++ b/source/TextBlade.Core/Commands/ChangeLocationCommand.cs @@ -1,4 +1,3 @@ -using TextBlade.Core.Characters; using TextBlade.Core.Game; using TextBlade.Core.IO; using TextBlade.Core.Locations; @@ -10,9 +9,12 @@ 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)) @@ -20,14 +22,15 @@ public ChangeLocationCommand(string destinationId) throw new InvalidOperationException($"{locationPath} doesn't seem to exist!"); } + _game = game; _locationId = destinationId; _locationPath = locationPath; } - public void Execute(IGame game, List party) + public void Execute(SaveData saveData) { var locationData = Serializer.Deserialize(File.ReadAllText(_locationPath)); locationData.LocationId = _locationId; - game.SetLocation(locationData); + _game.SetLocation(locationData); } } diff --git a/source/TextBlade.Core/Commands/Display/LookCommand.cs b/source/TextBlade.Core/Commands/Display/LookCommand.cs index dc9e66a..c9484a2 100644 --- a/source/TextBlade.Core/Commands/Display/LookCommand.cs +++ b/source/TextBlade.Core/Commands/Display/LookCommand.cs @@ -1,5 +1,4 @@ -using TextBlade.Core.Characters; -using TextBlade.Core.Game; +using TextBlade.Core.IO; namespace TextBlade.Core.Commands.Display; @@ -9,7 +8,7 @@ namespace TextBlade.Core.Commands.Display; /// public class LookCommand : ICommand { - public void Execute(IGame game, List party) + public void Execute(SaveData saveData) { // This stinks. } diff --git a/source/TextBlade.Core/Commands/Display/ShowCreditsCommand.cs b/source/TextBlade.Core/Commands/Display/ShowCreditsCommand.cs index 40cded5..0506ef4 100644 --- a/source/TextBlade.Core/Commands/Display/ShowCreditsCommand.cs +++ b/source/TextBlade.Core/Commands/Display/ShowCreditsCommand.cs @@ -13,7 +13,7 @@ public ShowCreditsCommand(IConsole console) _console = console; } - public void Execute(IGame game, List party) + public void Execute(SaveData saveData) { var creditsFilePath = Path.Join("Content", "Credits.txt"); if (File.Exists(creditsFilePath)) diff --git a/source/TextBlade.Core/Commands/Display/ShowHelpCommand.cs b/source/TextBlade.Core/Commands/Display/ShowHelpCommand.cs index 57ec76d..21a5f17 100644 --- a/source/TextBlade.Core/Commands/Display/ShowHelpCommand.cs +++ b/source/TextBlade.Core/Commands/Display/ShowHelpCommand.cs @@ -24,7 +24,7 @@ public ShowHelpCommand(IConsole console) { "credits", "Shows the credits" }, }; - public void Execute(IGame game, List party) + public void Execute(SaveData saveData) { var toReturn = new List { diff --git a/source/TextBlade.Core/Commands/Display/ShowInventoryCommand.cs b/source/TextBlade.Core/Commands/Display/ShowInventoryCommand.cs index 156ac34..f9261db 100644 --- a/source/TextBlade.Core/Commands/Display/ShowInventoryCommand.cs +++ b/source/TextBlade.Core/Commands/Display/ShowInventoryCommand.cs @@ -1,7 +1,5 @@ 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; @@ -9,6 +7,7 @@ namespace TextBlade.Core.Commands.Display; public class ShowInventoryCommand : ICommand { private readonly IConsole _console; + private readonly EquipmentEquipper _equipper; private readonly ItemUser _itemUser; @@ -16,6 +15,8 @@ public class ShowInventoryCommand : ICommand public ShowInventoryCommand(IConsole console, bool isInBattle = false) { + ArgumentNullException.ThrowIfNull(console); + _console = console; _equipper = new(console); _itemUser = new(console); @@ -23,11 +24,11 @@ public ShowInventoryCommand(IConsole console, bool isInBattle = false) _isInBattle = isInBattle; } - public void Execute(IGame game, List party) + public void Execute(SaveData saveData) { _console.WriteLine("Inventory:"); - var inventory = game.Inventory; + var inventory = saveData.Inventory; var items = inventory.ItemsInOrder; if (_isInBattle) { @@ -73,10 +74,10 @@ public void Execute(IGame game, List 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; } } diff --git a/source/TextBlade.Core/Commands/Display/ShowPartyStatusCommand.cs b/source/TextBlade.Core/Commands/Display/ShowPartyStatusCommand.cs index e329bd4..aac3980 100644 --- a/source/TextBlade.Core/Commands/Display/ShowPartyStatusCommand.cs +++ b/source/TextBlade.Core/Commands/Display/ShowPartyStatusCommand.cs @@ -13,11 +13,11 @@ public ShowPartyStatusCommand(IConsole console) _console = console; } - public void Execute(IGame game, List 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}")); diff --git a/source/TextBlade.Core/Commands/DoNothingCommand.cs b/source/TextBlade.Core/Commands/DoNothingCommand.cs index 58f3adb..0f4e778 100644 --- a/source/TextBlade.Core/Commands/DoNothingCommand.cs +++ b/source/TextBlade.Core/Commands/DoNothingCommand.cs @@ -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 party) + public void Execute(SaveData saveData) { } } diff --git a/source/TextBlade.Core/Commands/ICommand.cs b/source/TextBlade.Core/Commands/ICommand.cs index b6a2fa7..89563cf 100644 --- a/source/TextBlade.Core/Commands/ICommand.cs +++ b/source/TextBlade.Core/Commands/ICommand.cs @@ -1,5 +1,5 @@ using TextBlade.Core.Characters; -using TextBlade.Core.Game; +using TextBlade.Core.IO; namespace TextBlade.Core.Commands; @@ -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. /// - public void Execute(IGame game, List party); + public void Execute(SaveData saveData); } diff --git a/source/TextBlade.Core/Commands/ManuallySaveCommand.cs b/source/TextBlade.Core/Commands/ManuallySaveCommand.cs index 5261650..3de798c 100644 --- a/source/TextBlade.Core/Commands/ManuallySaveCommand.cs +++ b/source/TextBlade.Core/Commands/ManuallySaveCommand.cs @@ -1,5 +1,5 @@ using TextBlade.Core.Characters; -using TextBlade.Core.Game; +using TextBlade.Core.IO; namespace TextBlade.Core.Commands; @@ -9,7 +9,7 @@ namespace TextBlade.Core.Commands; /// public class ManuallySaveCommand : ICommand { - public void Execute(IGame game, List party) + public void Execute(SaveData saveData) { // This stinks. } diff --git a/source/TextBlade.Core/Commands/QuitGameCommand.cs b/source/TextBlade.Core/Commands/QuitGameCommand.cs index e3f9e95..716024a 100644 --- a/source/TextBlade.Core/Commands/QuitGameCommand.cs +++ b/source/TextBlade.Core/Commands/QuitGameCommand.cs @@ -1,5 +1,3 @@ -using TextBlade.Core.Characters; -using TextBlade.Core.Game; using TextBlade.Core.IO; namespace TextBlade.Core.Commands; @@ -13,7 +11,7 @@ public QuitGameCommand(IConsole console) _console = console; } - public void Execute(IGame game, List party) + public void Execute(SaveData saveData) { _console.WriteLine($"Quit the game? Are you sure? [{Colours.Command}]y[/]/[{Colours.Command}]n[/]"); var input = _console.ReadKey(); diff --git a/source/TextBlade.Core/Commands/SleepAtInnCommand.cs b/source/TextBlade.Core/Commands/SleepAtInnCommand.cs index 9bec478..1a0a6ba 100644 --- a/source/TextBlade.Core/Commands/SleepAtInnCommand.cs +++ b/source/TextBlade.Core/Commands/SleepAtInnCommand.cs @@ -15,11 +15,10 @@ public SleepAtInnCommand(IConsole console, int innCost) _innCost = innCost; } - public void Execute(IGame game, List 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(); } diff --git a/source/TextBlade.Core/Commands/TakeTurnsBattleCommand.cs b/source/TextBlade.Core/Commands/TakeTurnsBattleCommand.cs index 3c085f4..25db11b 100644 --- a/source/TextBlade.Core/Commands/TakeTurnsBattleCommand.cs +++ b/source/TextBlade.Core/Commands/TakeTurnsBattleCommand.cs @@ -24,6 +24,7 @@ public class TakeTurnsBattleCommand : ICommand, IBattleCommand public bool IsVictory { get; private set; } private readonly List _monsters = new(); + private readonly IGame _game; private readonly IConsole _console; static TakeTurnsBattleCommand() @@ -77,23 +78,23 @@ public TakeTurnsBattleCommand(IConsole console, List monsterNames) } } - public void Execute(IGame game, List 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) { @@ -115,7 +116,7 @@ public void Execute(IGame game, List party) continue; } - new BasicMonsterAi(_console, party).ProcessTurnFor(monster); + new BasicMonsterAi(_console, saveData.Party).ProcessTurnFor(monster); } foreach (var e in _monsters) @@ -131,7 +132,7 @@ public void Execute(IGame game, List party) } } - foreach (var e in party) + foreach (var e in saveData.Party) { if (e.CurrentHealth <= 0) {