Now it’s time to work on better combat logic.
The basic plan is to create a new Battle class that will handle all the combat information – the player, their opponent, their turns, the results, etc.
We’ll add a CurrentBattle property to GameSession and have it store a Battle object. I want to do this in a functional style, like the Inventory class. So, the Battle class will be read-only properties and the combat logic will be in a new BattleService class.
At least, that’s the current plan. As always, we’ll adapt if we see problems during development.
When I started looking at the code, I noticed the combat logic sends a lot of messages to the UI.
To make it easier to move the combat logic into its own class, I decided to start this change with creating a separate MessageBroker class that can be used by any object in the game and watched by the UI. The MessageBroker class is a “Message Broker” – a class that separates the publisher and subscribers in the Observer pattern.
Right now, we’ll only have one subscriber, the UI. But, this message broker will give us flexibility for the future, and make it easier to move our current combat code into another class – since we won’t need to create new message events in every new class, and have the UI subscribe to the different events.
This is the current structure
When we create the Battle class, we need to decide how to send messages to the UI (MainWindow.xaml.cs)
We’ll handle messages to the UI by creating a MessageBroker class. It will be a Singleton (only ever one instance of the class, that all other objects in the program will share). The UI will subscribe to the one MessageBroker, insterad of us creating infividual events and subscriptions for each different type of class we might create in the future.
Step 1: Create \Engine\Service\MessageBroker.cs
Because we want every object in the program to use one shared instance of the MessageBroker, we’re going to use the Singleton design pattern for this class. We’ll do this by:
- Making the constructor private, so it cannot be instantiated by any other class (line 13).
- Creating a private static variable that will hold the only instance of the MessageBroker class (lines 10-11).
- Creating a static GetInstance function that returns the static instance of the MessageBroker object (lines 19-22).
Now, any class that wants to use the MessageBroker (as a publisher or subscriber) will call MessageBroker.GetInstance() to store a local reference to the shared MessageBroker object.
On line 17, we have the OnMesssageRaised EventHandler that the UI will subscribe to, to get all the messages.
The RaiseMessage function on line 24 is what the publisher objects will use to publish their messages.
MessageBroker.cs
using System;
using Engine.EventArgs;
namespace Engine.Services
{
public class MessageBroker
{
// Use the Singleton design pattern for this class,
// to ensure everything in the game sends messages through this one object.
private static readonly MessageBroker s_messageBroker =
new MessageBroker();
private MessageBroker()
{
}
public event EventHandler<GameMessageEventArgs> OnMessageRaised;
public static MessageBroker GetInstance()
{
return s_messageBroker;
}
internal void RaiseMessage(string message)
{
OnMessageRaised?.Invoke(this, new GameMessageEventArgs(message));
}
}
}
Step 2: Modify \Engine\ViewModels\GameSession.cs
Now that we have our message broker, we’ll add an instance of it to the GameSession class – in the _messageBroker variable on line 10. You’ll need to add “using Engine.Service;” to be able to reference the MessageBroker class.
Then, we’ll change our existing RaiseMessage calls to use the RaiseMessage function on _messageBroker. If I counted correctly, there are 30 calls to RaiseMessage that need to be change to “_messageBroker.RaiseMessage”.
Then, delete the RaiseMessage function that was on line 350 (the last function in the class).
Finally, we can remove the “public event EventHandler
GameSession.cs
using System.Linq;
using Engine.Factories;
using Engine.Models;
using Engine.Services;
namespace Engine.ViewModels
{
public class GameSession : BaseNotificationClass
{
private readonly MessageBroker _messageBroker = MessageBroker.GetInstance();
#region Properties
private Player _currentPlayer;
private Location _currentLocation;
private Monster _currentMonster;
private Trader _currentTrader;
public World CurrentWorld { get; }
public Player CurrentPlayer
{
get => _currentPlayer;
set
{
if(_currentPlayer != null)
{
_currentPlayer.OnActionPerformed -= OnCurrentPlayerPerformedAction;
_currentPlayer.OnLeveledUp -= OnCurrentPlayerLeveledUp;
_currentPlayer.OnKilled -= OnCurrentPlayerKilled;
}
_currentPlayer = value;
if(_currentPlayer != null)
{
_currentPlayer.OnActionPerformed += OnCurrentPlayerPerformedAction;
_currentPlayer.OnLeveledUp += OnCurrentPlayerLeveledUp;
_currentPlayer.OnKilled += OnCurrentPlayerKilled;
}
}
}
public Location CurrentLocation
{
get => _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 => _currentMonster;
set
{
if(_currentMonster != null)
{
_currentMonster.OnActionPerformed -= OnCurrentMonsterPerformedAction;
_currentMonster.OnKilled -= OnCurrentMonsterKilled;
}
_currentMonster = value;
if(_currentMonster != null)
{
_currentMonster.OnActionPerformed += OnCurrentMonsterPerformedAction;
_currentMonster.OnKilled += OnCurrentMonsterKilled;
_messageBroker.RaiseMessage("");
_messageBroker.RaiseMessage($"You see a {CurrentMonster.Name} here!");
}
OnPropertyChanged();
OnPropertyChanged(nameof(HasMonster));
}
}
public Trader CurrentTrader
{
get => _currentTrader;
set
{
_currentTrader = value;
OnPropertyChanged();
OnPropertyChanged(nameof(HasTrader));
}
}
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.Inventory.Weapons.Any())
{
CurrentPlayer.AddItemToInventory(ItemFactory.CreateGameItem(1001));
}
CurrentPlayer.AddItemToInventory(ItemFactory.CreateGameItem(2001));
CurrentPlayer.LearnRecipe(RecipeFactory.RecipeByID(1));
CurrentPlayer.AddItemToInventory(ItemFactory.CreateGameItem(3001));
CurrentPlayer.AddItemToInventory(ItemFactory.CreateGameItem(3002));
CurrentPlayer.AddItemToInventory(ItemFactory.CreateGameItem(3003));
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.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}");
}
}
}
}
private void GetMonsterAtLocation()
{
CurrentMonster = CurrentLocation.GetMonster();
}
public void AttackCurrentMonster()
{
if(CurrentMonster == null)
{
return;
}
if(CurrentPlayer.CurrentWeapon == null)
{
_messageBroker.RaiseMessage("You must select a weapon, to attack.");
return;
}
CurrentPlayer.UseCurrentWeaponOn(CurrentMonster);
if(CurrentMonster.IsDead)
{
// Get another monster to fight
GetMonsterAtLocation();
}
else
{
CurrentMonster.UseCurrentWeaponOn(CurrentPlayer);
}
}
public void UseCurrentConsumable()
{
if(CurrentPlayer.CurrentConsumable != null)
{
CurrentPlayer.UseCurrentConsumable();
}
}
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 OnCurrentPlayerPerformedAction(object sender, string result)
{
_messageBroker.RaiseMessage(result);
}
private void OnCurrentMonsterPerformedAction(object sender, string result)
{
_messageBroker.RaiseMessage(result);
}
private void OnCurrentPlayerKilled(object sender, System.EventArgs eventArgs)
{
_messageBroker.RaiseMessage("");
_messageBroker.RaiseMessage("You have been killed.");
CurrentLocation = CurrentWorld.LocationAt(0, -1);
CurrentPlayer.CompletelyHeal();
}
private void OnCurrentMonsterKilled(object sender, System.EventArgs eventArgs)
{
_messageBroker.RaiseMessage("");
_messageBroker.RaiseMessage($"You defeated the {CurrentMonster.Name}!");
_messageBroker.RaiseMessage($"You receive {CurrentMonster.RewardExperiencePoints} experience points.");
CurrentPlayer.AddExperience(CurrentMonster.RewardExperiencePoints);
_messageBroker.RaiseMessage($"You receive {CurrentMonster.Gold} gold.");
CurrentPlayer.ReceiveGold(CurrentMonster.Gold);
foreach(GameItem gameItem in CurrentMonster.Inventory.Items)
{
_messageBroker.RaiseMessage($"You receive one {gameItem.Name}.");
CurrentPlayer.AddItemToInventory(gameItem);
}
}
private void OnCurrentPlayerLeveledUp(object sender, System.EventArgs eventArgs)
{
_messageBroker.RaiseMessage($"You are now level {CurrentPlayer.Level}!");
}
}
}
Step 3: Modify \WPFUI\MainWindow.xaml.cs
First, I deleted the useless comments from line 13-15, in case you read this and wonder why your line numbers might be different.
Add “using Engine.Services;”, so this class can see the MessageBroker class.
Create a “_messageBroker” variable to hold an instance of the MessageBroker object on line 16.
On line 27, change the subscription to OnMessageRaised from “_gameSession” to “_messageBroker”, since all new messages will go through the message broker object.
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using Engine.EventArgs;
using Engine.Models;
using Engine.Services;
using Engine.ViewModels;
namespace WPFUI
{
public partial class MainWindow : Window
{
private readonly MessageBroker _messageBroker = MessageBroker.GetInstance();
private readonly GameSession _gameSession = new GameSession();
private readonly Dictionary<Key, Action> _userInputActions =
new Dictionary<Key, Action>();
public MainWindow()
{
InitializeComponent();
InitializeUserInputActions();
_messageBroker.OnMessageRaised += OnGameMessageRaised;
DataContext = _gameSession;
}
private void OnClick_MoveNorth(object sender, RoutedEventArgs e)
{
_gameSession.MoveNorth();
}
private void OnClick_MoveWest(object sender, RoutedEventArgs e)
{
_gameSession.MoveWest();
}
private void OnClick_MoveEast(object sender, RoutedEventArgs e)
{
_gameSession.MoveEast();
}
private void OnClick_MoveSouth(object sender, RoutedEventArgs e)
{
_gameSession.MoveSouth();
}
private void OnClick_AttackMonster(object sender, RoutedEventArgs e)
{
_gameSession.AttackCurrentMonster();
}
private void OnClick_UseCurrentConsumable(object sender, RoutedEventArgs e)
{
_gameSession.UseCurrentConsumable();
}
private void OnGameMessageRaised(object sender, GameMessageEventArgs e)
{
GameMessages.Document.Blocks.Add(new Paragraph(new Run(e.Message)));
GameMessages.ScrollToEnd();
}
private void OnClick_DisplayTradeScreen(object sender, RoutedEventArgs e)
{
if(_gameSession.CurrentTrader != null)
{
TradeScreen tradeScreen = new TradeScreen();
tradeScreen.Owner = this;
tradeScreen.DataContext = _gameSession;
tradeScreen.ShowDialog();
}
}
private void OnClick_Craft(object sender, RoutedEventArgs e)
{
Recipe recipe = ((FrameworkElement)sender).DataContext as Recipe;
_gameSession.CraftItemUsing(recipe);
}
private void InitializeUserInputActions()
{
_userInputActions.Add(Key.W, () => _gameSession.MoveNorth());
_userInputActions.Add(Key.A, () => _gameSession.MoveWest());
_userInputActions.Add(Key.S, () => _gameSession.MoveSouth());
_userInputActions.Add(Key.D, () => _gameSession.MoveEast());
_userInputActions.Add(Key.Z, () => _gameSession.AttackCurrentMonster());
_userInputActions.Add(Key.C, () => _gameSession.UseCurrentConsumable());
_userInputActions.Add(Key.I, () => SetTabFocusTo("InventoryTabItem"));
_userInputActions.Add(Key.Q, () => SetTabFocusTo("QuestsTabItem"));
_userInputActions.Add(Key.R, () => SetTabFocusTo("RecipesTabItem"));
_userInputActions.Add(Key.T, () => OnClick_DisplayTradeScreen(this, new RoutedEventArgs()));
}
private void MainWindow_OnKeyDown(object sender, KeyEventArgs e)
{
if(_userInputActions.ContainsKey(e.Key))
{
_userInputActions[e.Key].Invoke();
}
}
private void SetTabFocusTo(string tabName)
{
foreach(object item in PlayerDataTabControl.Items)
{
if (item is TabItem tabItem)
{
if (tabItem.Name == tabName)
{
tabItem.IsSelected = true;
return;
}
}
}
}
}
}
Step 4: Test the game
Even though we didn’t change a lot of code, we did change the architecture of the program. So, test the game to make sure it still shows messages in the UI.
NEXT LESSON: Lesson 16.2: Creating the Battle class
PREVIOUS LESSON: Lesson 15.4: Using GitHub to upload and download a solution