Lesson 07.1: Creating Monsters

In this lesson, we will create the game monsters.

Like most of the other game objects, we will use a factory class to create the monsters. When we create a monster object, we also need to give it random loot, for the player to collect after they defeat the monster.

We are also going to add images of the monsters, to display on the game screen.

 

 

Steps

Step 1: Create the new class Engine\Models\Monster.cs.

This class inherits from BaseNotificationClass, so it can update the UI when the monster’s CurrentHitPoints property changes.

The class also has an ImageName property, which is the image file we will add for the monster in Step 4.

Inside the constructor, we accept the parameters, and use them to populate the properties – including adding the location of the image file for the monster.

 

Monster.cs

 

 

Step 2: Update Engine\Factories\ItemFactory.cs

We need to create some more items in the game world, to add to the monster’s inventory – for the player to loot from them.

For the monster loot items, I’m using a starting ID of 9001 – just to keep all these items grouped, and easier for us to find.

 

ItemFactory.cs

 

 

Step 3: Create Engine\RandomNumberGenerator.cs

To make the monster loot random, we need a random number generator. The standard random class is not very random. So, we can add this version, which creates numbers more randomly.

If you don’t need a complex random number generator, or if you’re using a project that does not have access to System.Security.Cryptography, you can use the SimpleNumberBetween function, at the bottom of the class.

 

RandomNumberGenerator.cs

 

 

Step 4: Add monster images to Engine\Images\Monsters folder.

Create the new folder, download the images (using the link below), and add the image files to the new folder.

 

 

Rat.png   Snake.png   GiantSpider.png

All images, in a zip file (right-click and save)

After adding the image files to the solution, right-click on each one, select “Properties”, and set:

Build Action = Resource

Copy to Output Directory = Do not copy

 

 

Step 5: Create Engine\Factories\MonsterFactory.cs

Now that we have all the pieces, we can create monster objects.

We are going to use a factory to create a new monster object for each battle. By creating a new monster object each time, each monster will have its own random loot.

We want a new Monster object, each time we call the MonsterFactory – like how we need a new, unique Item object, each time we call the ItemFactory.

In the ItemFactory, we created a list of standard items. When we needed a new Item, the factory found the standard Item and called the Clone function, to create the new, unique object.

For the MosnterFactory, I’m using a “switch” statement, so you can see how that works.

The “switch” line (line 10) looks at the monsterID value that was passed in to GetMonster. Then, it looks for the “case” line with a matching value (lines 12, 20, and 28). When it finds the matching value, it runs the code after it, until it gets to a “return” (which returns a value, and does not run any more code in that function), or a “break” line (which goes to the first line of code after the closing line for the “switch” statement – line 38, here).

On line 36, we have “default”. This is the code that should run if there is no “case” statement with a matching value. For this function, that should never happen – because we are passing in the monsterID value. But, if it does happen, we create an ArgumentException object, with a message about the invalid monsterID.

On line 41, we have a function “AddLootItem”. This function accepts parameters of the monster object, the ID of the loot item, and the percentage change that the monster has that loot item.

To make the monster loot random, this function will pick a random number between 1 and 100. If that number is less than, or equal to, the “percentage” parameter, the item will be added to the monster’s Inventory. If the random number is higher than the “percentage” parameter, the item is not added.

 

MonsterFactory.cs

 

 

In lesson 7.2, we will add monsters to locations, and let the player encounter them, when they move to a location that has a monster. After that, in lesson 7.3, we will add combat – letting the player fight monsters.

 

Return to main page

Leave a Reply

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