Press "Enter" to skip to content

Lesson 19.13: Remove CombatService dependency from model classes

This should be the final refactoring code change before we move the classes into their new projects.

The Battle and AttackWithWeapon classes each call one function in CombatService. Because the functions are not used anywhere else, we’ll remove this dependency by copy-pasting the CombatService functions into the appropriate model class.

Step 1: Modify Engine\Actions\AttackWithWeapon.cs

Copy-and-paste the AttackSucceeded function from CombatService into AttackWithWeapon.cs

Change the AttackSucceeded function to “private” – because it’s only used inside the AttackWithWeapon class.

On line 33, change “CombatService.AttackSucceeded” to “AttackSucceeded” – because the function is inside the class now.

Change the “using Engine.Services;” directive to “using Engine.Shared;” – because the AttackSucceeded function calls the GetAttribute extension method from Engine\Shared\ExtensionMethods.cs

AttackWithWeapon.cs

using System;
using Engine.Models;
using Engine.Shared;
using SOSCSRPG.Core;
namespace Engine.Actions
{
    public class AttackWithWeapon : BaseAction, IAction
    {
        private readonly string _damageDice;
        public AttackWithWeapon(GameItem itemInUse, string damageDice)
            : base(itemInUse)
        {
            if (itemInUse.Category != GameItem.ItemCategory.Weapon)
            {
                throw new ArgumentException($"{itemInUse.Name} is not a weapon");
            }
            if (string.IsNullOrWhiteSpace(damageDice))
            {
                throw new ArgumentException("damageDice must be valid dice notation");
            }
            _damageDice = damageDice;
        }
        public void Execute(LivingEntity actor, LivingEntity target)
        {
            string actorName = (actor is Player) ? "You" : $"The {actor.Name.ToLower()}";
            string targetName = (target is Player) ? "you" : $"the {target.Name.ToLower()}";
            if(AttackSucceeded(actor, target))
            {
                int damage = DiceService.Instance.Roll(_damageDice).Value;
                ReportResult($"{actorName} hit {targetName} for {damage} point{(damage > 1 ? "s" : "")}.");
                target.TakeDamage(damage);
            }
            else
            {
                ReportResult($"{actorName} missed {targetName}.");
            }
        }
        private static bool AttackSucceeded(LivingEntity attacker, LivingEntity target)
        {
            // Currently using the same formula as FirstAttacker initiative.
            // This will change as we include attack/defense skills,
            // armor, weapon bonuses, enchantments/curses, etc.
            int playerDexterity = attacker.GetAttribute("DEX").ModifiedValue *
                                  attacker.GetAttribute("DEX").ModifiedValue;
            int opponentDexterity = target.GetAttribute("DEX").ModifiedValue *
                                    target.GetAttribute("DEX").ModifiedValue;
            decimal dexterityOffset = (playerDexterity - opponentDexterity) / 10m;
            int randomOffset = DiceService.Instance.Roll(20).Value - 10;
            decimal totalOffset = dexterityOffset + randomOffset;
            return DiceService.Instance.Roll(100).Value <= 50 + totalOffset;
        }
    }
}

Step 2: Modify Engine\Models\Battle.cs

Copy-and-paste the Combatant enum and the FirstAttacker function from CombatService into Battle.cs

Change the Combatant enum and the FirstAttacker function to “private” – because they’re only used inside the Battle class.

On line 34, change “CombatService.FirstAttacker” to ” FirstAttacker” – because the function is inside the class now.

Change the “using Engine.Services;” directive to “using Engine.Shared;” – because the FirstAttacker function calls the GetAttribute extension method from Engine\Shared\ExtensionMethods.cs

Battle.cs

using System;
using Engine.Shared;
using SOSCSRPG.Core;
using SOSCSRPG.Models.EventArgs;
namespace Engine.Models
{
    public class Battle : IDisposable
    {
        private readonly MessageBroker _messageBroker = MessageBroker.GetInstance();
        private readonly Player _player;
        private readonly Monster _opponent;
        private enum Combatant
        {
            Player,
            Opponent
        }
        public event EventHandler<CombatVictoryEventArgs> OnCombatVictory;
        public Battle(Player player, Monster opponent)
        {
            _player = player;
            _opponent = opponent;
            _player.OnActionPerformed += OnCombatantActionPerformed;
            _opponent.OnActionPerformed += OnCombatantActionPerformed;
            _opponent.OnKilled += OnOpponentKilled;
            _messageBroker.RaiseMessage("");
            _messageBroker.RaiseMessage($"You see a {_opponent.Name} here!");
            if(FirstAttacker(_player, _opponent) == Combatant.Opponent)
            {
                AttackPlayer();
            }
        }
        public void AttackOpponent()
        {
            if(_player.CurrentWeapon == null)
            {
                _messageBroker.RaiseMessage("You must select a weapon, to attack.");
                return;
            }
            _player.UseCurrentWeaponOn(_opponent);
            if(_opponent.IsAlive)
            {
                AttackPlayer();
            }
        }
        public void Dispose()
        {
            _player.OnActionPerformed -= OnCombatantActionPerformed;
            _opponent.OnActionPerformed -= OnCombatantActionPerformed;
            _opponent.OnKilled -= OnOpponentKilled;
        }
        private void OnOpponentKilled(object sender, System.EventArgs e)
        {
            _messageBroker.RaiseMessage("");
            _messageBroker.RaiseMessage($"You defeated the {_opponent.Name}!");
            _messageBroker.RaiseMessage($"You receive {_opponent.RewardExperiencePoints} experience points.");
            _player.AddExperience(_opponent.RewardExperiencePoints);
            _messageBroker.RaiseMessage($"You receive {_opponent.Gold} gold.");
            _player.ReceiveGold(_opponent.Gold);
            foreach(GameItem gameItem in _opponent.Inventory.Items)
            {
                _messageBroker.RaiseMessage($"You receive one {gameItem.Name}.");
                _player.AddItemToInventory(gameItem);
            }
            OnCombatVictory?.Invoke(this, new CombatVictoryEventArgs());
        }
        private void AttackPlayer()
        {
            _opponent.UseCurrentWeaponOn(_player);
        }
        private void OnCombatantActionPerformed(object sender, string result)
        {
            _messageBroker.RaiseMessage(result);
        }
        private static Combatant FirstAttacker(Player player, Monster opponent)
        {
            // Formula is: ((Dex(player)^2 - Dex(monster)^2)/10) + Random(-10/10)
            // For dexterity values from 3 to 18, this should produce an offset of +/- 41.5
            int playerDexterity = player.GetAttribute("DEX").ModifiedValue *
                                  player.GetAttribute("DEX").ModifiedValue;
            int opponentDexterity = opponent.GetAttribute("DEX").ModifiedValue *
                                    opponent.GetAttribute("DEX").ModifiedValue;
            decimal dexterityOffset = (playerDexterity - opponentDexterity) / 10m;
            int randomOffset = DiceService.Instance.Roll(20).Value - 10;
            decimal totalOffset = dexterityOffset + randomOffset;
            return DiceService.Instance.Roll(100).Value <= 50 + totalOffset
                ? Combatant.Player
                : Combatant.Opponent;
        }
    }
}

Step 3: Delete Engine\Services\CombatService.cs

Step 4: Test the game

NEXT LESSON: Lesson 19.14: Moving the Engine classes to their new projects

PREVIOUS LESSON: Lesson 19.12: Decouple InventoryService from LivingEntity

    Leave a Reply

    Your email address will not be published. Required fields are marked *