Lesson 12.7: Creating recipes

Now, we’ll add recipes to the game. Most of this code should look similar to what we do for Quests – a model, a factory, adding a list property to the Player class, and displaying the recipes in the UI.

 

 

 

Lesson Steps

Step 1: Modify Engine\Factories\ItemFactory.cs

Add the ingredients to make our healthy granola bar.

I added three new miscellaneous items (oats, honey, and raisins) on lines 23-25.

 

ItemFactory.cs

 

 

Step 2: Create Engine\Models\Recipe.cs

This class holds a list of ingredient items (and their quantities) that need to be combined and the Output items and quantities that are created by the recipe.

I have the Output as a List of ItemQuantity objects, so we can create multiple items and have “waste” items that are a by-product of our crafting.

 

There are also two helper functions – AddIngredient and AddOutputItem – for us to populate the Ingredients and OutputItems properties without allowing duplicate items.

 

Recipe.cs

 

 

Step 3: Create Engine\Factories\RecipeFactory.cs

The recipe factory class is like the quest factory. We create a recipe, add it to our private variable, and have a “GetByID” function to get the standard Recipe object from this factory.

 

RecipeFactory.cs

 

 

Step 4: Modify Engine\Models\Player.cs

Modify the Player class, to hold a list of the Recipes they know.

Add a new Recipes property on line 40 and initialize it with an empty ObservableList<Recipe> on line 54 of the constructor.

 

I also added a helper function LearnRecipe on lines 75-81. This checks that the Player doesn’t already have the recipe before adding it to their Recipes property.

 

Player.cs

 

 

Step 5: Modify Engine\ViewModels\GameSession.cs

Let’s give the player the granola bar recipe as their first recipe.

In the constructor, on line 133, get the granola bar recipe from the RecipeFactory and have the Player learn it through the LearnRecipe function.

 

GameSession.cs

 

 

Step 6: Modify WPFUI\MainWindow.xaml

Finally, we’ll add the player’s known recipe list to the UI. We’ll place it in the lower-left section of the screen as a new tab item.

 

MainWindow.xaml (lines 201-211)

 

 

Step 7: Run your unit tests and play the game, to test the latest changes.

 

 

In the next lesson, we’ll add the ability to craft, so the player can finally make their first granola bar.

 

Return to main page

Leave a Reply

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