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.

 

 

Steps

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.

 

ItemQuantity.cs

 

 

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.

 

Quest.cs

 

 

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.

 

ItemFactory.cs

 

 

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.

 

QuestFactory.cs

 

 

Return to main page

2 thoughts on “Lesson 06.1: Creating the Quest Factory

  1. If I add another quest to this factory with different quest items and rewards required for completion do I just add them to itemsToComplete and then in the _quests.add() use itemsToComplete. and then use index of added item in the itemToComplete list?

    1. Hi Trevor,

      The way I would do it is to add code like this:

      _quests.Add(new Quest(2,
      "Clear the farmer's field",
      "Defeat the rats that are in the Farmer's field",
      new List {new ItemQuantity(9003, 5)},
      25, 10,
      new List
      {new ItemQuantity(1001, 1)}));

      This will create the quest without using the temporary variables “itemsToComplete” and “rewardItems”. Your parameters are completely new lists, with the items you want for the quest. This is safer than trying to re-use the temporary variables.

Leave a Reply

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