Lesson 06.1: Creating the Quest Factory

Now that the player can move around, let’s start working on building quests for the player to complete.




Step 1: Create a new ItemQuantity.cs class, in \Engine\Models.

To complete these quests, the player will need to turn in items. For example, a farmer might ask the player to kill rats in his field, and return five rat tails to complete the quest.

So, we need a way to store which items need to be turned in, and how many of them need to be turned it.

To allow for more complex quests, the player may need to turn in more than one type of item, to complete a quest. This requires another List property, because there may be one, or more, items.

Our first class is ItemQuantity. It will store an item ID, and a quantity. You can think of it like a shopping list:


Quantity       Item

1                      pizza

6                      sodas


For this class, we won’t store a complete GameItem object. We can store the ID of the GameItem.





Step 2: Create a new Quest.cs class, in \Engine\Models.

Next, we will create the Quest class.

This has string and int properties for the ID, Name, Description, RewardExperiencePoints, and RewardGold. The datatype of the ItemsToCompelte and RewardItems properties, will be List<ItemQuantity>, because they will hold a list of the new ItemQuantity objects.





Step 3: Edit Engine\ItemFactory.cs

Before creating the quests, we’ll need to create some more game items, to turn in to complete the quests.

Our first quest will be to kill snakes, and return with five snake fangs. The reward for that quest will be 25 experience points, 10 gold, and a rusty sword.

Inside the “static ItemFactory” function, add two more items to the _standardGameItems list: Snake Fang and Snakeskin.





Step 4: Create a new QuestFactory.cs class, in \Engine\Factories.

Now we can build the factory that will create the Quest objects. Like the ItemFactory, we will have a static variable (_quests) that holds all the Quests, and a constructor-like function that populates _quests the first time someone uses the QuestFactory class. We’ll also create the GetQuestByID function, to return the Quest object, wherever the rest of the program needs it.

Because we need to pass a list of ItemQuantity objects into the Quest constructor (for the itemsToComplete and rewardItems parameters), we’ll create variables, add items to the variables, and pass them as the parameters. In the refactoring for the Quest classes, we will change this a little.





Return to main page

Leave a Reply

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