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).





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).





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).





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

Leave a Reply

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