Now that the property setters don’t call OnPropertyChanged, we can convert many properties to auto-properties.
This isn’t a big change, but it will let us replace 8 lines of code with one line, in many places. This reduces “cognitive load” – the amount of mental energy we need to use to understand our code.
We aren’t changing the program’s behavior. We’re just making the code easier for us to read, understand, and hold in our memory.
This change also lets us see more of the class at once, without needing to scroll up and down to see what happened elsewhere in the class. With our small classes, this isn’t a big problem. However, with larger programs (and larger classes), this can be a problem.
We’ll also delete unnecessary “set”s from the auto-properties.
Some properties are only populated by the constructor. So, we don’t need a “set” for them. For properties that don’t need them, we’ll delete the “set”.
This eliminates a little bit for us to read, but also gives us additional information about the property. Without a “set”, we know the property’s value is never changed by something else in the program.
Some “set”s can also be given different visibility – making them private, protected, or init (can only be set at the same time as object instantiation). This prevents us from changing the property’s values in places where they shouldn’t be changed and makes this more apparent when we read the code.
Step 1: Clean up GameDetails.cs
Remove unnecessary “set” for Title, SubTitle, and Version
using System.Collections.Generic;
namespace Engine.Models
{
public class GameDetails
{
public string Title { get; }
public string SubTitle { get; }
public string Version { get; }
public List<PlayerAttribute> PlayerAttributes { get; } =
new List<PlayerAttribute>();
public List<Race> Races { get; } =
new List<Race>();
public GameDetails(string title, string subTitle, string version)
{
Title = title;
SubTitle = subTitle;
Version = version;
}
}
}
Step 2: Clean up GroupedInventoryItem.cs
Convert these properties to auto-properties: Item and Quantity
using System.ComponentModel;
namespace Engine.Models
{
public class GroupedInventoryItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public GameItem Item { get; set; }
public int Quantity { get; set; }
public GroupedInventoryItem(GameItem item, int quantity)
{
Item = item;
Quantity = quantity;
}
}
}
Step 3: Clean up LivingEntity.cs
Convert these properties to auto-properties: Name, CurrentHitPoints, MaximumHitPoints, Gold, Level, and Inventory
Make the “set”s for MaximumHitPoints and Level “protected”
Make the “set”s for CurrentHitPoints, Gold, and Inventory “private”
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Engine.Services;
using Newtonsoft.Json;
namespace Engine.Models
{
public abstract class LivingEntity : INotifyPropertyChanged
{
#region Properties
private GameItem _currentWeapon;
private GameItem _currentConsumable;
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<PlayerAttribute> Attributes { get; } =
new ObservableCollection<PlayerAttribute>();
public string Name { get; }
public int CurrentHitPoints { get; private set; }
public int MaximumHitPoints { get; protected set; }
public int Gold { get; private set; }
public int Level { get; protected set; }
public Inventory Inventory { get; private set; }
public GameItem CurrentWeapon
{
get => _currentWeapon;
set
{
if (_currentWeapon != null)
{
_currentWeapon.Action.OnActionPerformed -= RaiseActionPerformedEvent;
}
_currentWeapon = value;
if (_currentWeapon != null)
{
_currentWeapon.Action.OnActionPerformed += RaiseActionPerformedEvent;
}
}
}
public GameItem CurrentConsumable
{
get => _currentConsumable;
set
{
if(_currentConsumable != null)
{
_currentConsumable.Action.OnActionPerformed -= RaiseActionPerformedEvent;
}
_currentConsumable = value;
if (_currentConsumable != null)
{
_currentConsumable.Action.OnActionPerformed += RaiseActionPerformedEvent;
}
}
}
[JsonIgnore]
public bool IsAlive => CurrentHitPoints > 0;
[JsonIgnore]
public bool IsDead => !IsAlive;
#endregion
public event EventHandler<string> OnActionPerformed;
public event EventHandler OnKilled;
protected LivingEntity(string name, int maximumHitPoints, int currentHitPoints,
IEnumerable<PlayerAttribute> attributes, int gold, int level = 1)
{
Name = name;
MaximumHitPoints = maximumHitPoints;
CurrentHitPoints = currentHitPoints;
Gold = gold;
Level = level;
foreach (PlayerAttribute attribute in attributes)
{
Attributes.Add(attribute);
}
Inventory = new Inventory();
}
public void UseCurrentWeaponOn(LivingEntity target)
{
CurrentWeapon.PerformAction(this, target);
}
public void UseCurrentConsumable()
{
CurrentConsumable.PerformAction(this, this);
RemoveItemFromInventory(CurrentConsumable);
}
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 = Inventory.AddItem(item);
}
public void RemoveItemFromInventory(GameItem item)
{
Inventory = Inventory.RemoveItem(item);
}
public void RemoveItemsFromInventory(IEnumerable<ItemQuantity> itemQuantities)
{
Inventory = Inventory.RemoveItems(itemQuantities);
}
#region Private functions
private void RaiseOnKilledEvent()
{
OnKilled?.Invoke(this, new System.EventArgs());
}
private void RaiseActionPerformedEvent(object sender, string result)
{
OnActionPerformed?.Invoke(this, result);
}
#endregion
}
}
Step 4: Clean up PlayerAttribute.cs
Convert ModifiedValue to auto-property
using System.ComponentModel;
using Engine.Services;
namespace Engine.Models
{
public class PlayerAttribute : INotifyPropertyChanged
{
public string Key { get; }
public string DisplayName { get; }
public string DiceNotation { get; }
public int BaseValue { get; set; }
public int ModifiedValue { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
// Constructor that will use DiceService to create a BaseValue.
// The constructor this calls will put that same value into BaseValue and ModifiedValue
public PlayerAttribute(string key, string displayName, string diceNotation)
: this(key, displayName, diceNotation, DiceService.Instance.Roll(diceNotation).Value)
{
}
// Constructor that takes a baseValue and also uses it for modifiedValue,
// for when we're creating a new attribute
public PlayerAttribute(string key, string displayName, string diceNotation,
int baseValue) :
this(key, displayName, diceNotation, baseValue, baseValue)
{
}
// This constructor is eventually called by the others,
// or used when reading a Player's attributes from a saved game file.
public PlayerAttribute(string key, string displayName, string diceNotation,
int baseValue, int modifiedValue)
{
Key = key;
DisplayName = displayName;
DiceNotation = diceNotation;
BaseValue = baseValue;
ModifiedValue = modifiedValue;
}
public void ReRoll()
{
BaseValue = DiceService.Instance.Roll(DiceNotation).Value;
ModifiedValue = BaseValue;
}
}
}
Step 5: Clean up PlayerAttributeModifier.cs
Make AttributeKey and Modifier “init”
namespace Engine.Models
{
public class PlayerAttributeModifier
{
public string AttributeKey { get; init; }
public int Modifier { get; init; }
}
}
Step 6: Clean up QuestStatus.cs
Convert IsCompleted to auto-property
using System.ComponentModel;
namespace Engine.Models
{
public class QuestStatus : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Quest PlayerQuest { get; }
public bool IsCompleted { get; set; }
public QuestStatus(Quest quest)
{
PlayerQuest = quest;
IsCompleted = false;
}
}
}
Step 7: Clean up CharacterCreationViewModel.cs
Convert SelectedRace to auto-property and “init”
Convert Name to “init”
Remove “set” from PlayerAttributes property
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using Engine.Factories;
using Engine.Models;
using Engine.Services;
namespace Engine.ViewModels
{
public class CharacterCreationViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public GameDetails GameDetails { get; }
public Race SelectedRace { get; init; }
public string Name { get; init; }
public ObservableCollection<PlayerAttribute> PlayerAttributes { get; } =
new ObservableCollection<PlayerAttribute>();
public bool HasRaces =>
GameDetails.Races.Any();
public bool HasRaceAttributeModifiers =>
HasRaces && GameDetails.Races.Any(r => r.PlayerAttributeModifiers.Any());
public CharacterCreationViewModel()
{
GameDetails = GameDetailsService.ReadGameDetails();
if(HasRaces)
{
SelectedRace = GameDetails.Races.First();
}
RollNewCharacter();
}
public void RollNewCharacter()
{
PlayerAttributes.Clear();
foreach(PlayerAttribute playerAttribute in GameDetails.PlayerAttributes)
{
playerAttribute.ReRoll();
PlayerAttributes.Add(playerAttribute);
}
ApplyAttributeModifiers();
}
public void ApplyAttributeModifiers()
{
foreach(PlayerAttribute playerAttribute in PlayerAttributes)
{
var attributeRaceModifier =
SelectedRace.PlayerAttributeModifiers
.FirstOrDefault(pam => pam.AttributeKey.Equals(playerAttribute.Key));
playerAttribute.ModifiedValue =
playerAttribute.BaseValue + (attributeRaceModifier?.Modifier ?? 0);
}
}
public Player GetPlayer()
{
Player player = new Player(Name, 0, 10, 10, PlayerAttributes, 10);
// Give player default inventory items, weapons, recipes, etc.
player.AddItemToInventory(ItemFactory.CreateGameItem(1001));
player.AddItemToInventory(ItemFactory.CreateGameItem(2001));
player.LearnRecipe(RecipeFactory.RecipeByID(1));
player.AddItemToInventory(ItemFactory.CreateGameItem(3001));
player.AddItemToInventory(ItemFactory.CreateGameItem(3002));
player.AddItemToInventory(ItemFactory.CreateGameItem(3003));
return player;
}
}
}
Step 8: Clean up GameSession.cs
Convert GameDetails and CurrentTrader to auto-properties with “private set”
using System.ComponentModel;
using System.Linq;
using Engine.Factories;
using Engine.Models;
using Engine.Services;
using Newtonsoft.Json;
namespace Engine.ViewModels
{
public class GameSession : INotifyPropertyChanged
{
private readonly MessageBroker _messageBroker = MessageBroker.GetInstance();
#region Properties
private Player _currentPlayer;
private Location _currentLocation;
private Battle _currentBattle;
private Monster _currentMonster;
public event PropertyChangedEventHandler PropertyChanged;
[JsonIgnore]
public GameDetails GameDetails { get; private set; }
[JsonIgnore]
public World CurrentWorld { get; }
public Player CurrentPlayer
{
get => _currentPlayer;
set
{
if(_currentPlayer != null)
{
_currentPlayer.OnLeveledUp -= OnCurrentPlayerLeveledUp;
_currentPlayer.OnKilled -= OnPlayerKilled;
}
_currentPlayer = value;
if(_currentPlayer != null)
{
_currentPlayer.OnLeveledUp += OnCurrentPlayerLeveledUp;
_currentPlayer.OnKilled += OnPlayerKilled;
}
}
}
public Location CurrentLocation
{
get => _currentLocation;
set
{
_currentLocation = value;
CompleteQuestsAtLocation();
GivePlayerQuestsAtLocation();
CurrentMonster = CurrentLocation.GetMonster();
CurrentTrader = CurrentLocation.TraderHere;
}
}
[JsonIgnore]
public Monster CurrentMonster
{
get => _currentMonster;
set
{
if(_currentBattle != null)
{
_currentBattle.OnCombatVictory -= OnCurrentMonsterKilled;
_currentBattle.Dispose();
_currentBattle = null;
}
_currentMonster = value;
if(_currentMonster != null)
{
_currentBattle = new Battle(CurrentPlayer, CurrentMonster);
_currentBattle.OnCombatVictory += OnCurrentMonsterKilled;
}
}
}
[JsonIgnore]
public Trader CurrentTrader { get; private set; }
[JsonIgnore]
public bool HasLocationToNorth =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate + 1) != null;
[JsonIgnore]
public bool HasLocationToEast =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate + 1, CurrentLocation.YCoordinate) != null;
[JsonIgnore]
public bool HasLocationToSouth =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate - 1) != null;
[JsonIgnore]
public bool HasLocationToWest =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate - 1, CurrentLocation.YCoordinate) != null;
[JsonIgnore]
public bool HasMonster => CurrentMonster != null;
[JsonIgnore]
public bool HasTrader => CurrentTrader != null;
#endregion
public GameSession(Player player, int xCoordinate, int yCoordinate)
{
PopulateGameDetails();
CurrentWorld = WorldFactory.CreateWorld();
CurrentPlayer = player;
CurrentLocation = CurrentWorld.LocationAt(xCoordinate, yCoordinate);
}
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 PopulateGameDetails()
{
GameDetails = GameDetailsService.ReadGameDetails();
}
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.Inventory.HasAllTheseItems(quest.ItemsToComplete))
{
CurrentPlayer.RemoveItemsFromInventory(quest.ItemsToComplete);
_messageBroker.RaiseMessage("");
_messageBroker.RaiseMessage($"You completed the '{quest.Name}' quest");
// Give the player the quest rewards
_messageBroker.RaiseMessage($"You receive {quest.RewardExperiencePoints} experience points");
CurrentPlayer.AddExperience(quest.RewardExperiencePoints);
_messageBroker.RaiseMessage($"You receive {quest.RewardGold} gold");
CurrentPlayer.ReceiveGold(quest.RewardGold);
foreach(ItemQuantity itemQuantity in quest.RewardItems)
{
GameItem rewardItem = ItemFactory.CreateGameItem(itemQuantity.ItemID);
_messageBroker.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));
_messageBroker.RaiseMessage("");
_messageBroker.RaiseMessage($"You receive the '{quest.Name}' quest");
_messageBroker.RaiseMessage(quest.Description);
_messageBroker.RaiseMessage("Return with:");
foreach(ItemQuantity itemQuantity in quest.ItemsToComplete)
{
_messageBroker
.RaiseMessage($" {itemQuantity.Quantity} {ItemFactory.CreateGameItem(itemQuantity.ItemID).Name}");
}
_messageBroker.RaiseMessage("And you will receive:");
_messageBroker.RaiseMessage($" {quest.RewardExperiencePoints} experience points");
_messageBroker.RaiseMessage($" {quest.RewardGold} gold");
foreach(ItemQuantity itemQuantity in quest.RewardItems)
{
_messageBroker
.RaiseMessage($" {itemQuantity.Quantity} {ItemFactory.CreateGameItem(itemQuantity.ItemID).Name}");
}
}
}
}
public void AttackCurrentMonster()
{
_currentBattle?.AttackOpponent();
}
public void UseCurrentConsumable()
{
if(CurrentPlayer.CurrentConsumable != null)
{
if (_currentBattle == null)
{
CurrentPlayer.OnActionPerformed += OnConsumableActionPerformed;
}
CurrentPlayer.UseCurrentConsumable();
if (_currentBattle == null)
{
CurrentPlayer.OnActionPerformed -= OnConsumableActionPerformed;
}
}
}
private void OnConsumableActionPerformed(object sender, string result)
{
_messageBroker.RaiseMessage(result);
}
public void CraftItemUsing(Recipe recipe)
{
if(CurrentPlayer.Inventory.HasAllTheseItems(recipe.Ingredients))
{
CurrentPlayer.RemoveItemsFromInventory(recipe.Ingredients);
foreach(ItemQuantity itemQuantity in recipe.OutputItems)
{
for(int i = 0; i < itemQuantity.Quantity; i++)
{
GameItem outputItem = ItemFactory.CreateGameItem(itemQuantity.ItemID);
CurrentPlayer.AddItemToInventory(outputItem);
_messageBroker.RaiseMessage($"You craft 1 {outputItem.Name}");
}
}
}
else
{
_messageBroker.RaiseMessage("You do not have the required ingredients:");
foreach(ItemQuantity itemQuantity in recipe.Ingredients)
{
_messageBroker
.RaiseMessage($" {itemQuantity.Quantity} {ItemFactory.ItemName(itemQuantity.ItemID)}");
}
}
}
private void OnPlayerKilled(object sender, System.EventArgs e)
{
_messageBroker.RaiseMessage("");
_messageBroker.RaiseMessage("You have been killed.");
CurrentLocation = CurrentWorld.LocationAt(0, -1);
CurrentPlayer.CompletelyHeal();
}
private void OnCurrentMonsterKilled(object sender, System.EventArgs eventArgs)
{
// Get another monster to fight
CurrentMonster = CurrentLocation.GetMonster();
}
private void OnCurrentPlayerLeveledUp(object sender, System.EventArgs eventArgs)
{
_messageBroker.RaiseMessage($"You are now level {CurrentPlayer.Level}!");
}
}
}
Step 9: Test the game
NEXT LESSON: Lesson 19.6: Move files out of Engine project
PRVIOUS LESSON: Lesson 19.4: Replace BaseNotificationClass with Fody PropertyChanged Notification
Couple of typos:
CharacterCreationViewModel.cs – The instruction is to remove set from the PlayerAttributes property, your example code has also removed the ‘get, which then disables populating the DataGrid on the player creation form.
LivingEntity.cs – the instruction for CurrentHitPoints is repeated, with contradictory content.
Otherwise, all done and still works, although I have taken my courage in both hands and moved the App to .Net 6.0.
Regrds.
Phil
Hi Phil,
The “get” for the PlayerAttributes property in CharacterCreationViewModel is not immediately visible because that line is long enough that you need to use the horizontal scroll bar at the bottom of source code.
I fixed the other line.