Lesson 21.2 – Create the vendor class and add it to locations

For the vendors, we will use UI binding to their inventory, the same way we do with the Player class. But vendors will not have hit points, experience, etc. So, the Vendor class will be similar to the Player class, but with less properties and functions.

 

Step 1: In the Engine project, create a new class – Vendor.cs.

We’re only going to have the vendor’s name and inventory as properties in this class. We will add, and remove items, for the vendor’s inventory with functions that will raise the property changed notification.

Vendor.cs

 

Step 2: Modify Location.cs.

Add a new property to hold the Vendor.

 

Step 3: Modify World.cs.

In the PopulateLocations() function, create a new vendor object, and give it some inventory, for any locations where you want a vendor. I decided to add a vendor to the town square. Now, that part of PopulateLocations looks like this:

 

Step 4: Run your program, and make sure it still works. We still won’t see anything new in the UI with these changes. That will happen in the next lesson.

 

Source code for this lesson

Get it from GitHub: https://gist.github.com/ScottLilly/5ff9e3a2cad705a2595e

Or DropBox: https://www.dropbox.com/sh/5a7cw02jeskfyvq/AAB7vbLpbHAj0rIcFpzaOOEka?dl=0

 

Next lesson: Lesson 21.3 – Add a button and create its eventhandler in code, without the UI design screen

Previous lesson: Lesson 21.1 – Adding a price to game items

All lessons: Learn C# by Building a Simple RPG Index

Leave a Reply

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