Lesson 12.8: Crafting items with recipes

It’s finally time to add crafting to the game – after seven lessons getting ready to do this.




Lesson Steps

Step 1: Modify Engine\Factories\ItemFactory.cs

The Recipe classes uses ItemQuantity objects for the Ingredients and OutputItems.

ItemQuantity only holds the ItemTypeID, and not the item’s name. For this change, I want to display the name without creating a new GameItem object. So, I added a new function ItemName (lines 62-65). This static function lets us pass in an itemTypeID and get the item’s name.

Without this function, we’d need to instantiate a GameItem object every time we were working with an ItemQuantity object and wanted the GameItem’s name.





Step 2: Modify Engine\Models\LivingEntity.cs

When we craft an item from a recipe, we need to check if the player has all the ingredients and remove those items from their inventory (if they do have them all).

We have some existing code for that in Player.cs and GameSession.cs. But, I want to move it into the LivingEntity class. That’s where we have the Inventory property and other inventory functions. So, let’s put all the inventory code in the same class.


On lines 244-253, add the RemoveItemsFromInventory function that accepts a List of ItemQuantity objects.


On 255-266, add the HasAllTheseItems function. We already use this code (in the Player class) to check if the player can complete a quest. Now, we’ll also use it to see if the player has all the ingredients needed to craft a recipe.





Step 3: Modify Engine\Models\Player.cs

Remove the HasAllTheseItems function from lines 57-68. You can also remove the “using System.Collections.Generic;” line from the top of the class.





Step 4: Modify Engine\ViewModels\GameSession.cs

First, let’s give the player some ingredients, so they can craft a granola bar. On lines 134-136, add these items to the player’s inventory.


Because we created the RemoveItemsFromInventory function in LivingEntity, we can replace the existing lines 184-191 (from your existing code) with the one function call on line 187 (from the code below).


To craft an item, add the new CraftItemUsing function on lines 274-298.

This function checks if the player has all the required ingredients. If they do, it removes them from the player’s inventory, gives the player the recipe’s output items, and displays a message for each item they crafted.

If the player does not have the required ingredients, we tell them and show them a list of all the required ingredients. Notice that we use the new ItemFactory.ItemName() function here – since we don’t have the ingredient’s name in the ItemQuantity object but want to display the name in the message in the UI.





Step 5: Modify WPFUI\MainWindow.xaml and MainWindow.xaml.cs

Now we can connect all this logic to the UI. I’m doing this by adding a “Craft” button to the recipe datagrid, like how we have “Buy 1” and “Sell 1″ buttons on the TradeScreen.


On lines 209-217 of MainWindow.xaml, add a new DataGridTemplateColumn with a button that calls the OnClick_Craft” function.


In MainWindow.xaml.cs, add the new OnClick_Craft function (lines 69-73). This determines the recipe from the row where the user clicked the button (the “sender”) and passes that Recipe object into the GameSession’s new crafting function.


MainWindow.xaml (lines 201-220)






Step 6: Run unit tests and the game, to ensure our latest changes works.


Return to main page

Leave a Reply

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