Lesson 12.4: Letting the Monster use AttackWithWeapon

Now, we’ll change the Monster’s attack to use the AttackWithWeapon class. To do this, we’ll need to give the Monster a weapon to use.




Lesson Steps

Step 1: Modify Engine\Factories\ItemFactory.cs

We’ll create two new weapons on lines 17-19 – fangs and a claw.

Right now, we won’t use the weapon description. The important parts are the minimum and maximum damage values that we will use in the BuildWeapon function, where we build the AttackWithWeapon object for the Monster’s “weapon”.





Step 2: Modify Engine\Models\Monster.cs

Remove the MinimumDamage and MaximuDamage properties (lines 6-7), along with their parameters in the constructor (line 13) and the lines where the parameters were assigned to the properties (lines 19-20).





Step 3: Modify Engine\Factories\MonsterFactory.cs

Now, well modify the Monster object creation to give them a CurrentWeapon that uses an IAction object.

Modify the calls to the Monster constructor (lines 14, 23, and 32) to remove the old minimum and maximum damage parameters.


Then, give the Monsters weapons to use on the new lines 16, 27, and 38. Notice that we are not adding these items to the monsters’ loot. We’re only adding them to the CurrentWeapon. So, the Player won’t ever receive these weapons when they defeat a monster and loot their items.

If we wanted the weapon to be lootable, we would need to use the AddLootItem function to put it in the monster’s inventory.





Step 4: Modify Engine\ViewModels\GameSession.cs

Now, we’ll switch the monster’s combat logic to use the IAction from its CurrentWeapon.

We’ll do this the same way we do for the Player class. The Monster class inherits from LivingEntity. So, LivingEntity already subscribes to its CurrentWeapon object’s OnActionPerformed event. When the Action raises a message, the LivingEntity object catches it and raises its own OnActionPerformed event that the UI is looking at.

So, we need to have the GameSession class watch the CurrentMonster’s OnActionPerformed event.


Subscribe to the Monster’s action event by adding the new OnCurrentMonsterPerformedAction function (lines 275-278). Subscribe to, and unsubscribe from, the CurrentMonster’s OnActionPerformed event in the CurrentMonster setter on lines 73 and 81.


Then, remove the existing code from lines 265-275 and replace it with the monster’s equivalent to line 255. This will make the CurrentMonster use its CurrentWeapon’s Action when it attacks the CurrentPlayer.





Step 5: Modify Engine\Actions\AttackWithWeapon.cs

Finally, we need to change the message from the AttackWithWeapon so it displays the correct measure – whether a Player is attacking a Monster, or a Monster is attacking a Player.


On lines 40 and 41, we create two variables to handle using “You” in the damage report message. This checks to see if the player is the actor or the target, and places “you” in the correct place.

On lines 45 and 49, we change the message text to use these new variables. On line 49, I also added a conditional display of the letter “s”, when the amount of damage is more than one hit point.





Step 6: Run the unit tests and test the game


Return to main page

2 thoughts on “Lesson 12.4: Letting the Monster use AttackWithWeapon

  1. Hey Scott,

    I’ve been testing the combat in the game and noticed that it is always the monster that misses the player. I don’t think I’ve seen an encounter yet where the player misses the monster.  I looked at the ItemFactory class and noticed that the minimum damage for the stick and sword were “built” with a minimum of 1 and the monsters have a minimum of  0.  Is this the reason?  I wondered if this was by design.


    1. There’s no real reason for that, other than having some weapons that could do zero damage, to test that the different message would display. The combat logic is extremely simple right now. But, I want to make it more complex soon – breaking the hit/miss into its own logic (instead of just checking if damage equals zero), applying attack bonuses, adding armor, etc. The existing code is mostly a placeholder, until we get to the time where we can add better logic.

Leave a Reply

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