Skip to content

Commit

Permalink
Refactor: redo skill selection, without recursion.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightblade9 committed Dec 18, 2024
1 parent 3fee18a commit a9ff735
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
24 changes: 11 additions & 13 deletions source/TextBlade.Core/Battle/CharacterTurnProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,11 @@ internal void ProcessTurnFor(Character character)
new AttackExecutor(_console).Attack(character, targets.First() as Monster);
break;
case 'd':
character.Defend();
_console.WriteLine($"{character.Name} defends!");
character.Defend(_console);
break;
case 's':
// Assumes you get back a valid skill: something you have SP for.
var skill = PickSkillFor(character);
if (character.CurrentSkillPoints < skill.Cost)
{
_console.WriteLine("You don't have enough skill points for that!");
// Recursion is risky, very risky ... hmm.
ProcessTurnFor(character);
}
targets = PickTargetsFor(skill);
// Depending on the skill, the target is an instance of Character or Monster.
// For now, assume monster.
new SkillApplier(_console).Apply(character, skill, targets);
break;
case 'i':
Expand All @@ -77,7 +67,7 @@ internal void ProcessTurnFor(Character character)
}
}

private IEnumerable<Entity> PickTargetsFor(Skill skill)
private IEnumerable<Entity>? PickTargetsFor(Skill skill)
{
switch (skill.Target)
{
Expand Down Expand Up @@ -141,8 +131,16 @@ private T PickFromList<T>(IEnumerable<T> items)

private Skill PickSkillFor(Character character)
{
_console.WriteLine("Pick a skill:");
_console.WriteLine("Pick a skill: ");
var skill = PickFromList(character.Skills);

if (character.CurrentSkillPoints < skill.Cost)
{
_console.WriteLine($"{character.Name} has {character.CurrentSkillPoints} skill points, which isn't enough for {skill.Name}.");
_console.WriteLine("Pick a skill: ");
skill = PickFromList(character.Skills);
}

return skill;
}
}
3 changes: 2 additions & 1 deletion source/TextBlade.Core/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ internal void GainExperiencePoints(IConsole console, int experiencePoints)
return base.OnRoundComplete();
}

internal void Defend()
internal void Defend(IConsole console)
{
this.IsDefending = true;
console.WriteLine($"{Name} defends!");
}

public override string ToString()
Expand Down

0 comments on commit a9ff735

Please sign in to comment.