Lesson 10.5: Encapsulating Level and ExperiencePoints Properties

Since we’ll eventually add levels to monsters, and (maybe) traders, I want to move the Level property to the LivingEntity base class.

We won’t move ExperiencePoints to LivingEntity, because Monsters and Traders won’t ever gain or lose experience – only Players. However, we will encapsulate the Player’s ExperiencePoints property, and use it to update their Level, as they gain experience.

 

 

 

Lesson Steps

Step 1: Modify Engine\Models\LivingEntity.cs

We’ll add the Level property and its backing variable to LivingEntity (line 16 and lines 58-66). You can cut-and-paste this code from the Player class.

Make the Level setter “protected” (line 61), so it can be modified by the Player class.

Add a “level” parameter to the constructor, and have it use a default value of “1” (lines 81-82). Eventually, we will pass in values for this parameter, when constructing monsters. But, for now, the default value is good enough to let us continue.

In the constructor, set the Level property from the parameter value (line 88).

We want to change the MaximumHitPoints value when the Player gains a level. So, we need to change the MaximumHitPoints setter to protected (line 41).

 

LivingEntity.cs

 

 

Step 2: Modify Engine\Models\Player.cs

Remove Level property and its backing variable.

When the player gains a level, we’ll notify the UI with an event – OnLeveledUp (line 42).

To encapsulate the ExperiencePoints property, change its setter to “private” (line 28).

Create a new public AddExperience() function, to let the game give the player experience (lines 67-70).

Create a new SetLevelAndMaximumHitPoints() function (lines 72-84).

This function saves the original Level value (line 74), re-calculates the Level (line 76), and handles levelling up by increasing the player’s MaximumHitPoints and raising the event to notify the UI of the level change (lines 78-83).

Finally, change the ExperiencePoints setter to call this function (line 34).

 

Player.cs

 

 

Step 3: Modify Engine\ViewModels\GameSession.cs

Update the GameSession class to call the new AddExperience function, since it cannot directly update the ExperiencePoints property any longer (lines 193 and 305).

I also changed the order of some lines in CompleteQuestsAtLocation (lines 193, 196, and 203), so they call RaiseMessage before making any property changes – the new pattern we want to use, in case the function to change the property also changes something else.

Update the CurrentPlayer setter to subscribe to, and unsubscribe from, the OnLeveledUp event (lines 29 and 37).

Finally, create the OnCurrentPlayerLeveledUp() function to raise a message when the OnLeveledUp event is raised (lines 317-320).

 

GameSession.cs

 

 

Step 4: Test the game.

Fight some monsters and check that the player’s Level changes to “2” when they have 100 experience points. Continue fighting until the player dies and moves to their home. Check that CompletelyHeal healed them to 20 hit points – the new MaximumHitPoints for a level 2 player.

 

Return to main page

7 thoughts on “Lesson 10.5: Encapsulating Level and ExperiencePoints Properties

  1. Hey, Scott, Great job, wanted to thank you. I have been Following both sets of tutorials, and enjoyed them, learned a great deal. Hope your able to get the next few videos out soon, looking forward to seeing how you progress the series.

    1. You’re welcome. I have the code for the next lesson written, but stillneed to make (and edit) the recording. I’ve been spending most of my time lately on a presentation I need to give next week for my new consulting business. Once that is over, I can put time into new lessons again.

  2. Hey Scott, I love this tutorial. It has been enjoyable and really taught me a lot. When will your next lessons for this WPF game be coming out? Will you have one on the automated unit testing? How about the others in your list of future features you had at the end of the lesson list? I’ve been following along and studying everything you do! I plan to code along with you as you continue to evolve and expand the game and teach more software development and .NET concepts. Thanks!

    1. Hi Gary,

      Thanks. I’ve been wanting to get more lessons out, but I started a consulting company at the beginning of the year and have been putting all my time and energy into trying to find clients for that. However, I’m going to record the unit test lesson right now. It might be good for me to take a break from marketing and do something a little different for a while. It will take a day or two to edit and post the lesson.

  3. Hey Scott. I’ve been following along with the same code as yours and I’ve noticed that when I complete the quest to kill the snakes in the herb garden, the datagrid in the quests tab still shows false in the “Done?” column. The five snake fangs are removed from my inventory and the rusty sword is added so that part is working.  When I click on the header for the datagrid column the value changes to true all of a sudden. So, it looks like maybe the datagrid isn’t refreshing when the quest is completed.  Is there a way to programmatically refresh the datagrid at the moment it has been determined that a quest is completed?

     

    Thanks!

    1. I just checked out the code and saw the problem. It’s probably happening because the QuestStatus.IsCompleted property does not raise a PropertyChanged event – and it should, since it does get updated, and the UI needs to know if it’s changed.

      I’ve made a note to fix that in a new lesson. If you want to fix it now, you can have QuestStatus inherit from BaseNotificationClass, create a backing variable for IsCompleted, and have the setter call OnPropertyChanged. That’s the way I’ll fix it.

Leave a Reply

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