I would like some advice on the architecture I am using, and if there's perhaps a better way to go about doing this / if I am over-complicating things.
I have a combo meter go up on the screen based off successive hits. My high level design is like this:
DamageCalculator.cs
: a static class that is used on every successful hitStatsManager.cs
: a singleton that contains a lot of fields relating to stats likecomboCount
(an int that is the current number of combos)ComboGUI.cs
: a component that updates the GUI
The combo counter UI works as follows:
When the player successfully hits an enemy more than once,
DamageCalculator.cs
triggers an eventOnCombo()
StatsManager.cs
is subscribed to theOnCombo
event, and when this triggers it doescomboCount++
ComboGui.cs
is also subscribed to theOnCombo
event, and when this triggers, it updates the UI to show the value ofcomboCount
The code:
DamageCalculator.cs
public static Action OnCombo;
// ...
public static void CalculateDamage(...)
{
// ...
if ( combo )
{
// ...
OnCombo();
}
// ...
}
StatsManager.cs
public int comboCount
private void OnEnable()
{
CalculateDamage.OnCombo += IncrementCombo;
}
private void OnDisable()
{
CalculateDamage.OnCombo -= IncrementCombo;
}
private void IncrementCombo()
{
comboCount++;
}
// ...
ComboGUI.cs
private TextMeshProUGUI tmp;
private void OnEnable()
{
DamageCalculator.OnCombo += SetCombo;
}
private void OnDisable()
{
DamageCalculator.OnCombo -= SetCombo;
}
// ...
private void SetCombo()
{
string comboText = StatsManager.Instance.comboCount + " COMBO";
tmp.text = comboText; // tmp is a TextMeshProUGUI
}
What makes me go 🤔🤔🤔 like there is probably a better way for this, is that I have no guarantee which events in StatsManager
and ComboGUI
will get triggered first when OnCombo fires. So I wonder if there is a pattern or better way of doing this, since it seems to be a pretty "routine" thing in games?
StatsManager
? I'd call the methods directly fromDamageCalculator
then post a message for the UI to check the new value and update. Conversely, if multiple entities can do combos, I'd store the state on the entity itself so everyone has their own counter. \$\endgroup\$