Lesson 14.4: Read Monster data from an XML file

Now that we can display location images from XML data files, we’ll use the same method for the monsters. However, we do have another thing we’ll need to change – the random loot.

We’ll modify the MonsterFactory to work like the ItemFactory class, storing a base monster and instantiating new instances of Monster objects through a CreateMonster(ID) function.

 

 

 

Lesson Steps

Step 1: Modify image files in Engine\Images\Monsters

Just like we did with the Location image files, we need to change the Monster image files, so they are not project resources.

Set their “Build Action” to “None” and their “Copy to Output Directory” to “Copy Always”.

 

Step 2: Create Engine\Models\ItemPercentage.cs

We’re going to store the monster’s loot table inside the Monster object, instead of having the factory determine the monster’s inventory.

To do this, we’ll use this new class that stores the Item ID and the percentage chance it is in a monster’s inventory.

 

ItemPercentage.cs

 

 

Step 3: Modify Engine\Models\Monster.cs

Previously, we instantiated a new Monster object every time MonsterFactory.GetMonster() was called. Now, we’re going to build a list of “standard” Monster objects, populated from the XML data file, that we can clone whenever we need a new Monster object.

So, we’re moving the loot table information into the Monster class, to make it available when we call the Monster.GetNewInstance() function – which is how we will create a clone of the default Monster. In this function, we also populate the new Monster’s inventory, based on its loot table values.

The AddItemToLootTable is what we’ll use to populate the loot table. We could add a new parameter to the constructor whose datatype is List<ItemPercentage>, and pass in a complete loot table. But I usually prefer populating list properties with a separate function. Either way works – this is just my preference.

 

Monster.cs

 

 

Step 4: Create Engine\GameData\Monsters.xml

This is like the GameItems.xml and Locations.xml files.

In the root “Monster” node, we have the directory path to the monsters’ image files. Then, we have the details for each type of monster in the game.

 

NOTE: Remember to set this file’s “Copy to Output Directory” to “Copy Always”.

 

Monsters.xml

 

 

Step 5: Modify Engine\Factories\MonsterFactory.cs

The changes here are like the changes to the LocationFactory class.

On line 12, we have the path to the Monsters.xml file.

On line 14, we have our list of “base” monsters that we will populate from the XML file and use to create clones for the player to fight.

The LoadMonstersFromNodes function (lines 35-65) is where we build the base monsters from the XML file. Remember that the attribute names are case-sensitive. If you have any errors, double-check that you don’t have a problem like typing “Id”, when it should be “ID”.

 

MonsterFactory.cs

 

 

Step 6: Modify WPFUI\MainWindow.xaml

Finally, we need to add the FileToBitmapConverter to the Image Source for the Monster’s image file on lines 148-149.

 

MainWindow.xaml (lines 143-149)

 

 

Return to main page

Leave a Reply

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