Lesson 10.2: Grouping GameItems in Inventories

Inventories currently hold one object for each GameItem.

We need to do this because we will eventually have effects that are unique for each GameItem – such as enchanting a weapon. However, there are some GameItems that will never have special effects – like snakeskins.

We’re going to make changes to allow us to distinguish between unique items and non-unique items. This will let us have fewer lines in our Inventory datagrids.

 

 

 

 

Lesson Steps

Step 1: Modify Engine\Models\GameItem.cs

Add the Boolean “IsUnique” property to the GameItem class.

Add a parameter to the constructor, to let us pass in a Boolean value for the IsUnique property. The “= false” is the default value for this parameter. If we call the constructor without passing in this parameter, it will use a value of “false”.

You can only setup default values for parameters at the end of the parameter list. If you had a default value for a parameter in the middle of the parameter list, the compiler might not know which parameter is missing.

 

In the Clone function, on line 20, we want to pass the current object’s IsUnique value into its clone.

 

GameItem.cs

 

 

Step 2: Modify Engine\Models\Weapon.cs

In the Weapon constructor, where we call the base class constructor (GameItem), pass “true” for the “isUnique” parameter.

This is because we want all weapons to be unique. Weapons will eventually be able to have enchantments, and maybe wear out from use. So, two “pointy sticks” could be different from each other.

 

Weapon.cs

 

 

Step 3: Create the new class Engine\Models\GroupedInventoryItem.cs

This class holds a GameItem object and a Quantity. It’s like ItemQuantity, except it holds a complete GameItem – instead of only the GameItem’s ID. This is because we need some of the GameItem’s properties (Name and Price), to display in the UI.

GroupedInventoryItem needs to inherit from BaseNotificationClass, so we can raise a property changed event when the Quantity increases or decreases.

 

GroupedInventoryItem.cs

 

 

Step 4: Modify Engine\Models\LivingEntity.cs

On line 56, add the new property GroupedInventory, whose datatype is ObservableCollection<GroupedInventoryItem>.

Initialize that property on line 64 of the constructor.

 

Change the AddItemToInventory function on line 67. After adding the item to the Inventory, we need to either:

1) add it to GroupedInventory (if it is unique, or not already in GroupedInventory), or

2) increment its Quantity (if it’s already in GroupedInventory).

On line 79, notice that we add new non-unique items with a quantity of zero. That’s because we’re going to increment the quantity by one on line 82.

 

Change the RemoveItemFromInventory function on line 88.

On line 92, we get the first GroupedInventoryItem from the GroupedInventory property. If that value is not null (it should never be null, but it’s good to check), we’ll either:

1) completely remove the GroupedInventoryItem from GroupedInventory if its quantity is one (which it will always be for unique items, and could possibly be for non-unique items), or

2) decrease its quantity by one on line 103

 

We might make more changes in the future, so there is only one property for the inventory. But, this is how we will make the changes now.

 

LivingEntity.cs

 

 

Step 5: Modify WPFUI\MainWindow.xaml

Now we need to change the UI datagrid to use the new GroupedInventory properties.

On line 168, change the DataGrid’s ItemSource to “CurrentPlayer.GroupedInventory”.

Change the Paths for the DataGridTextColumns (lines 173 and 180) to include “Item.”, because GroupedInventory holds a complete GameItem object as its “Item” property, and we want to display the “Name” or “Price” property from “Item”.

I also added a new DataGridTextColumn on 175-178, to display the Quantity value.

 

MainWindow.xaml

 

 

Step 6: Modify WPFUI\TradeScreen.xaml and TradeScreen.xaml.cs

We also need to update the inventory datagrids on the trading screen. These are the same changes as we made for MainWindow.

Lines 40 and 71 need to use GroupedInventory, instead of Inventory.

Lines 51, 59, 86, and 94 need to have “Item.” Added to the property paths.

Add new DataGridTextColumns for the Quantity on lines 52-55 and 87-90.

Finally, change the button text to include “1”, so the player knows they are selling or buying one of the item, and not the total quantity of that item. The button changes are on lines 65 and 100.

 

In TradeScreen.xaml.cs, we need to change the buy and sell functions, because the datagrids now contain GroupedInventoryItem objects, instead of ItemQuantity objects.

 

In OnClick_Sell and OnClick_Buy, we need to cast the event sender to a GroupedInventoryItem object. Then, change the buying and selling code to use groupedInventoryItem.Item.Price and groupedInventoryItem.Item.

 

TradeScreen.xaml

 

 

TradeScreen.xaml.cs

 

 

Step 7: Test the game

Fight some monsters, collect loot items, and visit the trader. make sure there are multiple lines for weapons, and only one line for each non-unique item (like snakeskins).

 

Return to main page

Leave a Reply

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