The next feature I want to add to the game is the ability to craft consumable items – food and potions the player can use to to heal themselves. But, this will require several changes to the program.
We need to make GameItems that can be eaten and give an affect (healing, in this case). We also need to add the ability for the player to craft items. This means adding recipes.
We could make this change by create new sub-classes of the GameItem class. But, we don’t want to need to create new GameItem Sub-classes for each new type of item.
So, we’re going to use two design patterns: Composition Over Inheritance and the Command Pattern. If you aren’t familiar with design patterns, they’re a set of common techniques to follow when trying to write a program.
Because there are many changes to make, I’ll split these changes into multiple lessons. In each lesson, we’ll make a small change and ensure we can still play the game.
This is safer than making hundreds of changes at the same time. If we did that, and something didn’t work, it would be difficult to find out which change broke the program. By making small changes, there won’t be many places to look, if we break something.
In this lesson, we’ll eliminate the Weapon class, and move its properties into the GameItem class.
Step 1: Modify Engine\Models\GameItem.cs
We need a way to identify what type of item a GameItem is (Weapon, Food, Armor, etc.). So, we’ll add a Category property (line 11). Its datatype is the new ItemCategory enum (lines 5-9).
An “enum” is like a list of constants. One enum that already exists in the .NET Framework is “DayOfWeek”. It lets you write code like this:
“if(DateTime.Now.DayOfWeek = DayOfWeek.Monday)”
So, you aren’t forced to remember which integer value represents Monday. When you compile your program, the enums are converted to integer values. But, when you write your code, you get to use the easier-to-understand value.
On lines 16 and 17, we have the MinimumDamage and MaximumDamage properties that used to be in the Weapon class.
Finally, we changed the constructor and Clone functions to accept and use the new properties.
GameItem.cs
namespace Engine.Models
{
public class GameItem
{
public enum ItemCategory
{
Miscellaneous,
Weapon
}
public ItemCategory Category { get; }
public int ItemTypeID { get; }
public string Name { get; }
public int Price { get; }
public bool IsUnique { get; }
public int MinimumDamage { get; }
public int MaximumDamage { get; }
public GameItem(ItemCategory category, int itemTypeID, string name, int price,
bool isUnique = false, int minimumDamage = 0, int maximumDamage = 0)
{
Category = category;
ItemTypeID = itemTypeID;
Name = name;
Price = price;
IsUnique = isUnique;
MinimumDamage = minimumDamage;
MaximumDamage = maximumDamage;
}
public GameItem Clone()
{
return new GameItem(Category, ItemTypeID, Name, Price,
IsUnique, MinimumDamage, MaximumDamage);
}
}
}
Step 2: Delete Engine\Models\Weapon.cs
Because we have the MinimumDamage and MaximumDamage properties in GameItem, we don’t need the Weapon sub-class any more.
When you delete this class, your project will have several errors – where the program was using the Weapon class. We need to fix those by having the code look at GameItem.Category to determine if a GameItem is a weapon.
Step 3: Modify Engine\Models\LivingEntity.cs
The Weapons property (lines 72-73) used to look in the player’s Inventory property and return a list of everything that “is Weapon” (whose datatype is Weapon).
Now, we’ll change it to check if the GameItem’s Category equals ItemCategory.Weapon.
LivingEntity.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Engine.Models
{
public abstract class LivingEntity : BaseNotificationClass
{
#region Properties
private string _name;
private int _currentHitPoints;
private int _maximumHitPoints;
private int _gold;
private int _level;
public string Name
{
get { return _name; }
private set
{
_name = value;
OnPropertyChanged();
}
}
public int CurrentHitPoints
{
get { return _currentHitPoints; }
private set
{
_currentHitPoints = value;
OnPropertyChanged();
}
}
public int MaximumHitPoints
{
get { return _maximumHitPoints; }
protected set
{
_maximumHitPoints = value;
OnPropertyChanged();
}
}
public int Gold
{
get { return _gold; }
private set
{
_gold = value;
OnPropertyChanged();
}
}
public int Level
{
get { return _level; }
protected set
{
_level = value;
OnPropertyChanged();
}
}
public ObservableCollection<GameItem> Inventory { get; }
public ObservableCollection<GroupedInventoryItem> GroupedInventory { get; }
public List<GameItem> Weapons =>
Inventory.Where(i => i.Category == GameItem.ItemCategory.Weapon).ToList();
public bool IsDead => CurrentHitPoints <= 0;
#endregion
public event EventHandler OnKilled;
protected LivingEntity(string name, int maximumHitPoints, int currentHitPoints,
int gold, int level = 1)
{
Name = name;
MaximumHitPoints = maximumHitPoints;
CurrentHitPoints = currentHitPoints;
Gold = gold;
Level = level;
Inventory = new ObservableCollection<GameItem>();
GroupedInventory = new ObservableCollection<GroupedInventoryItem>();
}
public void TakeDamage(int hitPointsOfDamage)
{
CurrentHitPoints -= hitPointsOfDamage;
if(IsDead)
{
CurrentHitPoints = 0;
RaiseOnKilledEvent();
}
}
public void Heal(int hitPointsToHeal)
{
CurrentHitPoints += hitPointsToHeal;
if(CurrentHitPoints > MaximumHitPoints)
{
CurrentHitPoints = MaximumHitPoints;
}
}
public void CompletelyHeal()
{
CurrentHitPoints = MaximumHitPoints;
}
public void ReceiveGold(int amountOfGold)
{
Gold += amountOfGold;
}
public void SpendGold(int amountOfGold)
{
if(amountOfGold > Gold)
{
throw new ArgumentOutOfRangeException($"{Name} only has {Gold} gold, and cannot spend {amountOfGold} gold");
}
Gold -= amountOfGold;
}
public void AddItemToInventory(GameItem item)
{
Inventory.Add(item);
if(item.IsUnique)
{
GroupedInventory.Add(new GroupedInventoryItem(item, 1));
}
else
{
if(!GroupedInventory.Any(gi => gi.Item.ItemTypeID == item.ItemTypeID))
{
GroupedInventory.Add(new GroupedInventoryItem(item, 0));
}
GroupedInventory.First(gi => gi.Item.ItemTypeID == item.ItemTypeID).Quantity++;
}
OnPropertyChanged(nameof(Weapons));
}
public void RemoveItemFromInventory(GameItem item)
{
Inventory.Remove(item);
GroupedInventoryItem groupedInventoryItemToRemove = item.IsUnique ?
GroupedInventory.FirstOrDefault(gi => gi.Item == item) :
GroupedInventory.FirstOrDefault(gi => gi.Item.ItemTypeID == item.ItemTypeID);
if(groupedInventoryItemToRemove != null)
{
if(groupedInventoryItemToRemove.Quantity == 1)
{
GroupedInventory.Remove(groupedInventoryItemToRemove);
}
else
{
groupedInventoryItemToRemove.Quantity--;
}
}
OnPropertyChanged(nameof(Weapons));
}
#region Private functions
private void RaiseOnKilledEvent()
{
OnKilled?.Invoke(this, new System.EventArgs());
}
#endregion
}
}
Step 4: Modify Engine\ViewModels\GameSession.cs
The CurrentWeapon property (line 101) had a datatype of “Weapon”. Change it to “GameItem”.
GameSession.cs
using System;
using System.Linq;
using Engine.EventArgs;
using Engine.Factories;
using Engine.Models;
namespace Engine.ViewModels
{
public class GameSession : BaseNotificationClass
{
public event EventHandler<GameMessageEventArgs> OnMessageRaised;
#region Properties
private Player _currentPlayer;
private Location _currentLocation;
private Monster _currentMonster;
private Trader _currentTrader;
public World CurrentWorld { get; }
public Player CurrentPlayer
{
get { return _currentPlayer; }
set
{
if(_currentPlayer != null)
{
_currentPlayer.OnLeveledUp -= OnCurrentPlayerLeveledUp;
_currentPlayer.OnKilled -= OnCurrentPlayerKilled;
}
_currentPlayer = value;
if (_currentPlayer != null)
{
_currentPlayer.OnLeveledUp += OnCurrentPlayerLeveledUp;
_currentPlayer.OnKilled += OnCurrentPlayerKilled;
}
}
}
public Location CurrentLocation
{
get { return _currentLocation; }
set
{
_currentLocation = value;
OnPropertyChanged();
OnPropertyChanged(nameof(HasLocationToNorth));
OnPropertyChanged(nameof(HasLocationToEast));
OnPropertyChanged(nameof(HasLocationToWest));
OnPropertyChanged(nameof(HasLocationToSouth));
CompleteQuestsAtLocation();
GivePlayerQuestsAtLocation();
GetMonsterAtLocation();
CurrentTrader = CurrentLocation.TraderHere;
}
}
public Monster CurrentMonster
{
get { return _currentMonster; }
set
{
if(_currentMonster != null)
{
_currentMonster.OnKilled -= OnCurrentMonsterKilled;
}
_currentMonster = value;
if(_currentMonster != null)
{
_currentMonster.OnKilled += OnCurrentMonsterKilled;
RaiseMessage("");
RaiseMessage($"You see a {CurrentMonster.Name} here!");
}
OnPropertyChanged();
OnPropertyChanged(nameof(HasMonster));
}
}
public Trader CurrentTrader
{
get { return _currentTrader; }
set
{
_currentTrader = value;
OnPropertyChanged();
OnPropertyChanged(nameof(HasTrader));
}
}
public GameItem CurrentWeapon { get; set; }
public bool HasLocationToNorth =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate + 1) != null;
public bool HasLocationToEast =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate + 1, CurrentLocation.YCoordinate) != null;
public bool HasLocationToSouth =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate - 1) != null;
public bool HasLocationToWest =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate - 1, CurrentLocation.YCoordinate) != null;
public bool HasMonster => CurrentMonster != null;
public bool HasTrader => CurrentTrader != null;
#endregion
public GameSession()
{
CurrentPlayer = new Player("Scott", "Fighter", 0, 10, 10, 1000000);
if (!CurrentPlayer.Weapons.Any())
{
CurrentPlayer.AddItemToInventory(ItemFactory.CreateGameItem(1001));
}
CurrentWorld = WorldFactory.CreateWorld();
CurrentLocation = CurrentWorld.LocationAt(0, 0);
}
public void MoveNorth()
{
if(HasLocationToNorth)
{
CurrentLocation = CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate + 1);
}
}
public void MoveEast()
{
if(HasLocationToEast)
{
CurrentLocation = CurrentWorld.LocationAt(CurrentLocation.XCoordinate + 1, CurrentLocation.YCoordinate);
}
}
public void MoveSouth()
{
if(HasLocationToSouth)
{
CurrentLocation = CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate - 1);
}
}
public void MoveWest()
{
if(HasLocationToWest)
{
CurrentLocation = CurrentWorld.LocationAt(CurrentLocation.XCoordinate - 1, CurrentLocation.YCoordinate);
}
}
private void CompleteQuestsAtLocation()
{
foreach(Quest quest in CurrentLocation.QuestsAvailableHere)
{
QuestStatus questToComplete =
CurrentPlayer.Quests.FirstOrDefault(q => q.PlayerQuest.ID == quest.ID &&
!q.IsCompleted);
if(questToComplete != null)
{
if(CurrentPlayer.HasAllTheseItems(quest.ItemsToComplete))
{
// Remove the quest completion items from the player's inventory
foreach (ItemQuantity itemQuantity in quest.ItemsToComplete)
{
for(int i = 0; i < itemQuantity.Quantity; i++)
{
CurrentPlayer.RemoveItemFromInventory(CurrentPlayer.Inventory.First(item => item.ItemTypeID == itemQuantity.ItemID));
}
}
RaiseMessage("");
RaiseMessage($"You completed the '{quest.Name}' quest");
// Give the player the quest rewards
RaiseMessage($"You receive {quest.RewardExperiencePoints} experience points");
CurrentPlayer.AddExperience(quest.RewardExperiencePoints);
RaiseMessage($"You receive {quest.RewardGold} gold");
CurrentPlayer.ReceiveGold(quest.RewardGold);
foreach(ItemQuantity itemQuantity in quest.RewardItems)
{
GameItem rewardItem = ItemFactory.CreateGameItem(itemQuantity.ItemID);
RaiseMessage($"You receive a {rewardItem.Name}");
CurrentPlayer.AddItemToInventory(rewardItem);
}
// Mark the Quest as completed
questToComplete.IsCompleted = true;
}
}
}
}
private void GivePlayerQuestsAtLocation()
{
foreach(Quest quest in CurrentLocation.QuestsAvailableHere)
{
if(!CurrentPlayer.Quests.Any(q => q.PlayerQuest.ID == quest.ID))
{
CurrentPlayer.Quests.Add(new QuestStatus(quest));
RaiseMessage("");
RaiseMessage($"You receive the '{quest.Name}' quest");
RaiseMessage(quest.Description);
RaiseMessage("Return with:");
foreach(ItemQuantity itemQuantity in quest.ItemsToComplete)
{
RaiseMessage($" {itemQuantity.Quantity} {ItemFactory.CreateGameItem(itemQuantity.ItemID).Name}");
}
RaiseMessage("And you will receive:");
RaiseMessage($" {quest.RewardExperiencePoints} experience points");
RaiseMessage($" {quest.RewardGold} gold");
foreach(ItemQuantity itemQuantity in quest.RewardItems)
{
RaiseMessage($" {itemQuantity.Quantity} {ItemFactory.CreateGameItem(itemQuantity.ItemID).Name}");
}
}
}
}
private void GetMonsterAtLocation()
{
CurrentMonster = CurrentLocation.GetMonster();
}
public void AttackCurrentMonster()
{
if (CurrentWeapon == null)
{
RaiseMessage("You must select a weapon, to attack.");
return;
}
// Determine damage to monster
int damageToMonster = RandomNumberGenerator.NumberBetween(CurrentWeapon.MinimumDamage, CurrentWeapon.MaximumDamage);
if (damageToMonster == 0)
{
RaiseMessage($"You missed the {CurrentMonster.Name}.");
}
else
{
RaiseMessage($"You hit the {CurrentMonster.Name} for {damageToMonster} points.");
CurrentMonster.TakeDamage(damageToMonster);
}
if(CurrentMonster.IsDead)
{
// Get another monster to fight
GetMonsterAtLocation();
}
else
{
// Let the monster attack
int damageToPlayer = RandomNumberGenerator.NumberBetween(CurrentMonster.MinimumDamage, CurrentMonster.MaximumDamage);
if (damageToPlayer == 0)
{
RaiseMessage($"The {CurrentMonster.Name} attacks, but misses you.");
}
else
{
RaiseMessage($"The {CurrentMonster.Name} hit you for {damageToPlayer} points.");
CurrentPlayer.TakeDamage(damageToPlayer);
}
}
}
private void OnCurrentPlayerKilled(object sender, System.EventArgs eventArgs)
{
RaiseMessage("");
RaiseMessage("You have been killed.");
CurrentLocation = CurrentWorld.LocationAt(0, -1);
CurrentPlayer.CompletelyHeal();
}
private void OnCurrentMonsterKilled(object sender, System.EventArgs eventArgs)
{
RaiseMessage("");
RaiseMessage($"You defeated the {CurrentMonster.Name}!");
RaiseMessage($"You receive {CurrentMonster.RewardExperiencePoints} experience points.");
CurrentPlayer.AddExperience(CurrentMonster.RewardExperiencePoints);
RaiseMessage($"You receive {CurrentMonster.Gold} gold.");
CurrentPlayer.ReceiveGold(CurrentMonster.Gold);
foreach(GameItem gameItem in CurrentMonster.Inventory)
{
RaiseMessage($"You receive one {gameItem.Name}.");
CurrentPlayer.AddItemToInventory(gameItem);
}
}
private void OnCurrentPlayerLeveledUp(object sender, System.EventArgs eventArgs)
{
RaiseMessage($"You are now level {CurrentPlayer.Level}!");
}
private void RaiseMessage(string message)
{
OnMessageRaised?.Invoke(this, new GameMessageEventArgs(message));
}
}
}
Step 5: Modify Engine\Factories\ItemFactory.cs
Because we changed the GameItem constructor, and deleted the Weapon class, we need to change how the factory constructs the standard game items.
Before, the CreateGame function needed to check if the item was a Weapon, so it could use the Clone function from Weapon (instead of the one from GameItem). Now, it can always use the one in GameItem. We can reduce this function to one line.
To construct the standard GameItem objects, we could have just changed lines 13-21, to pass in the new values needed in the GameItem constructor. However, I created two new “builder” functions: BuildMiscellaneousItem and BuildWeapon. We’re going to need them soon, and they make the code a little cleaner (in my opinion).
There’s a principle in Agile programming known as YAGNI – You Aren’t Gonna Need It. You shouldn’t write code for things you think you’re going to add in the future. You may not need it. So, you would have wasted your time writing that code.
However, I’m 99.9% sure we’ll need this. I started writing all the changes for food items (using Composition Over Inheritance and the Command Pattern), and I quickly needed these functions.
ItemFactory.cs
using System.Collections.Generic;
using System.Linq;
using Engine.Models;
namespace Engine.Factories
{
public static class ItemFactory
{
private static readonly List<GameItem> _standardGameItems = new List<GameItem>();
static ItemFactory()
{
BuildWeapon(1001, "Pointy Stick", 1, 1, 2);
BuildWeapon(1002, "Rusty Sword", 5, 1, 3);
BuildMiscellaneousItem(9001, "Snake fang", 1);
BuildMiscellaneousItem(9002, "Snakeskin", 2);
BuildMiscellaneousItem(9003, "Rat tail", 1);
BuildMiscellaneousItem(9004, "Rat fur", 2);
BuildMiscellaneousItem(9005, "Spider fang", 1);
BuildMiscellaneousItem(9006, "Spider silk", 2);
}
public static GameItem CreateGameItem(int itemTypeID)
{
return _standardGameItems.FirstOrDefault(item => item.ItemTypeID == itemTypeID)?.Clone();
}
private static void BuildMiscellaneousItem(int id, string name, int price)
{
_standardGameItems.Add(new GameItem(GameItem.ItemCategory.Miscellaneous, id, name, price));
}
private static void BuildWeapon(int id, string name, int price,
int minimumDamage, int maximumDamage)
{
_standardGameItems.Add(new GameItem(GameItem.ItemCategory.Weapon, id, name, price,
true, minimumDamage, maximumDamage));
}
}
}
Step 6: Run the unit tests and test the game
Even though we only have two unit tests right now, we’ll run them to see if we broke any of the code they cover.
Since we don’t have many unit tests, it’s good to also play the game and see if the changes broke anything.
NEXT LESSON: Lesson 12.2: Creating the AttackWithWeapon command
PREVIOUS LESSON: Lesson 11.1: Creating the Unit Test Project