Lesson 07.2: Adding Monsters to Locations

In this lesson, we will add the monsters to the locations.

 

 

Steps

Step 1: Create \Engine\Models\MonsterEncounter.cs

We want to have the ability to have different types of monsters at a Location, and different odds of encountering each type of monster. This will let us have “rare” monsters, that are difficult to find.

We will use this class to hold the ID of the monster, and the chance of encountering it. The “ChanceOfEncountering” values do not need to total up to a specific value. You can put in any numbers, and our “get random monster” function will handle it,

 

MonsterEncounter.cs

 

 

Step 2: Modify \Engine\Models\Location.cs

To store the MonsterEncounter objects, we add a new MonstersHere property – which is a List of MonsterEncounter objects.

I created an AddMonster function, to populate this property.

We could directly add values to the property. However, this function lets us check if the monster has already been added to the list. If it has, we change its ChanceOfEncountering value to the new value. If the monster has not already been added to the list, we create a new MonsterEncounter object, and add it to MonstersHere.

 

The GetMonster function will determine which monster the location has, and instantiate a new Monster object for the Player to fight.

If there are not any objects in the MonstersHere list, the function returns null.

Otherwise, if adds up all the ChancesOfEncountering, for the MonsterEncounter objects. It gets a random number between 1 and the total ChancesOfEncountering. Then, it loops through the MonsterEncounter objects until it finds the monster to select.

When it determines the random monster, it instantiates a new Mosnter object, through the MonsterFactory, and returns that object.

If the function hasn’t selected a monster, the final line of the function will return a Monster from the last MonsterEncounter object. This is just in case there is a problem. We want to make sure this function always returns a monster, if there are monsters at the location.

 

Location.cs

 

 

Step 3: Modify \Engine\Factories\WorldFactory.cs

Now we can add monsters to the locations, inside the CreateWorld function.

We will add rats (MonsterID = 2) to the farmer’s field (at coordinates -2, -1), snakes (MonsterID = 1) to the herbalist’s garden (at coordinates 0, 2), and spiders (MonsterID = 3) to the Spider Forest (at coordinates 2, 0).

For now, these will be the only locations with monsters, and they will only have one monster each.

 

WorldFactory.cs

 

 

Step 4: Modify \Engine\ViewModels\GameSession.cs

When the player moves to a new location, we want to see if the location has monsters. If so, we will get a monster object and display its information (and image) on the UI.

We’ll start by adding a new CurrentMonster property to hold the Monster object in the ViewModel. We want to use a property with a backing variable, so we can raise an OnPropertyChanged function – to notify the UI that there is a new monster (or no monster).

We also will add a boolean HasMonster property, so we can easily hide and show controls in the UI, depending on whether or not there is a monster for the player to fight.

Instead of using a “set” and “get” for this property, we will use an “expression body”. This is a calculated value – in this case, true (if CurrentMonster is not null) or false (if CurrentMonster is null).

Because this property does not have a backing variable, we need to raise its OnPropertyChanged event inside the CurrentMonster “set” code – like we did for the HasLocationTo<direction> properties.

 

When the player moves to a new location, the CurrentLocation “set” code already calls GivePlayerQuestsAtLocation(). We will add a new function “GetMonsterAtLocation”.

This new function only needs to call the GetMonster function on the CurrentLocation. If the location does not have any monsters, the function will return “null”, CurrentMonster will be set to “null”, and HasMonster will be “false”. If there are monsters at the location, GetMonster will instantiate a new Monster object, which will go into GameSession’s CurrentMonster property. When the CurrentMonster is set to a monster, HasMonster will be “true”.

 

GameSession.cs

 

 

Step 5: Modify \WPFUI\MainWindow.xaml

The last step of this lesson is to display the monster information on the screen.

Beneath the Location section, we will display the CurrentMonster’s name, image, and current hit points. We do this by creating a grid (like the one for the location section).

The StackPanel control lets us display multiple values on the same line, or in the same column. To display the monster’s current hit points, we want a label that says “Current Hit Points” on the same line (horizontally) as the monster’s current hit points. The StackPanel lets us easily display the two TextBlocks on the same line.

 

To make it easier to identify the different sections of the XAML file, I added comments. A XAML comment starts with “<!–” and ends with “à” (that’s two hyphens/dashes in there).

 

I also added two new lines in the “Window” section, at the top of the page.

The line with “xmlns:ViewModels” tells the Window that we are using objects in the Engine project (assembly), and in the Engine.ViewModels namespace (folder structure).

The line with “d:DataContext” lets the XAML editor know what type of object is in this window’s DataContext. With this line, we can use IntelliSense, when binding properties.

 

MainWindow.xaml

 

 

Now, if you run the game, you should see monsters when the player moves to a location with a monster.

In the next lesson, we will let the player fight the monster and (hopefully) collect loot from them.

 

 

Return to main page

2 thoughts on “Lesson 07.2: Adding Monsters to Locations

  1. I am having issues getting the images and monster names to show up in the game. It has text for current hp but does not show number, image or text for names… I copied all the text as shown from the 7.1 and this one, and still no luck. Not sure what to do now.

Leave a Reply

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