Lesson 06.2: Using Quests in the Game

In this lesson, we will add quests to locations, give quests to the player (when they move to a location with a quest), and display the player’s quests on the screen.

The changes will be similar to what we did to add an inventory to the Player class, and display it on a tab in the UI.

 

 

Steps

Step 1: Open Engine\Models\Location.cs

We’ll add a new List<Quest> property, to hold quests available at the Location.

I’m making it a list property, so we can have multiple quests available at a location. In the future, we may create quest “chains”, where the player needs to complete one quest, before they can receive the next quest in the “chain”.

 

Location.cs

 

 

 

 

Step 2: Open Engine\WorldFactory.cs

After we create the Herbalist’s Hut location (position 0, 1), we will add the “Clear the Herb Garden” quest to its new QuestsAvailableHere property.

newWorld.LocationAt(0, 1) returns a Location object, which has its QuestsAvailableHere property. We can directly add the quest to this location by calling the code in this order.

 

WorldFactory.cs

 

 

Step 3: Create a new class Engine\Models\QuestStatus.cs

This is like the ItemQuantity class – except, instead of tracking the quantity of an item, we will track the completion status of a quest.

When the player receives a quest, we will create a new QuestStatus object, and set the IsCompleted status to “false”. Later, when the player completes the quest, we will set IsCompleted to “true”.

 

QuestStatus.cs


 

Step 4: Open Engine\Models\Player.cs

Like the Player Inventory property, we will create a new Quests property. This is an ObservableCollection<QuestStatus>, so it can automatically update the UI when the Player receives a new quest, or completes one of their existing quests.

 

Player.cs

 

 

Step 5: Open Engine\ViewModels\GameSession.cs

This class handles player movement. So, we will modify it to check if there is a new quest available, when the player moves to a new location.

We need to add “using System.Linq;”, so we can use LINQ to search through the CurrentPlayer’s Quests list – to ensure we don’t give the player a quest they already have (in the GivePlayerQuestsAtLocation function).

We also need to update the “set” for the CurrentLocation property. Now, when the player moves to a new location, and CurrentLocation is updated, we will call GivePlayerQuestsAtLocation the new function at the end of the GaneSession.cs.

 

GameSession.cs

 

 

Step 6: Open QPFUI\MainWindow.xaml

Finally, we will add another TabItem, to display the player’s quest, and its completion status.

This is like the Inventory tab. We bind it to the Player’s Quests property and configure the columns to display.

Notice that the first column is binding to PlayerQuest.Name. This is because we are binding to a list of QuestStatus objects. The QuestStatus.PlayerQuest property is a Quest object, which has a Name property. So, we are displaying a property of a property.

The new code is the TabItem for the Quests, at lines 121 to 134.

 

MainWindow.xaml

 

 

Return to main page

5 thoughts on “Lesson 06.2: Using Quests in the Game

  1. Scott,

    I am interested in following along with this project. Is there a lesson where I can download the source code and start at a mid-point? Or, is it better to just start from the beginning. The reason I ask is I don’t have a lot of time to get started. I would like to be caught up so I can do each lesson as you release them.

    Thanks,

    Zach

        1. I am all caught up! Your tutorials are very clear and straight forward. I am going to make a copy of the solution to tinker with and keep one for when you come out with new lessons.

          Thanks again!

Leave a Reply

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