lundi 5 juillet 2021

What design pattern should I use? Different classes, same static behaviour

I'm working on an RPG videogame. As you do in most RPGs, if you want a clean interface, you try to highlight certain bits of text when it is especially relevant to the player.

For instance, every time I want to say "this attack deals 30 cold damage" I will have a string like this:

"This attack deals $VALUE$."

$VALUE$ would then be replaced in real-time with "30 [ICON] Cold Damage":

  • [ICON] would be the icon for the cold damage type (a blue snowflake, for instance).
  • The number (30, in this case) will be in bold.
  • The words "Cold Damage" will have a light blue shade.

This is all done already, and it's working fine. However, as more effects, skills, attack types and characters get added, it's bloating my code big time. Right now, my Strings parser has a bunch of static methods like:

    public static string ColdDamage(int amount) 
    {
        return <a formatted string for cold damage>
    }

Here's the thing: Attack types follow this format. So do skills, effects, weapons, and everything in between, including characters (we show a little portrait and the name highlighted).

===============================

With that said, here are my (possible) SOLUTIONS:

I change my StringsParser class, so I may do:

    public static string FancyUpMyText(int amount, string text, string colorHex, string iconRef) 
    {
        return <a formatted string for anything you want>
    }

However, look at those 4 params. This will muddle up the code severely. Not to mention that not everything follows the same format. Sometimes, I want to say "30 [ICON] Cold Damage". Sometimes it's "[ICON] Attack +10". I need a more specific solution, or I will end up with 15 methods with a bunch of parameters each, all of which have only tiny variations in their massive, bloated return statements.

Alternatively, i could add an "IFancyDisplay" interface and make them all implement it. However, I need the methods to be static, since we've used the Visitor patten to handle many situations, and I don't always have the entire object's information available to me in all situations. It would be great to use "ClassDamageCold.GetFancyText(30);", but I cannot make static methods on an interface

So, being this C#, and with multiple inheritance being out of the question, and with so many elements scattered throughout the code that should share a common behaviour...

Is there any pattern I can use, or shall my StringsParser bloat into oblivion at this point?

Note: Some objects will have multiple methods ("Health +30" and "15% Health", for instance), so a Visitor is probably not the best choice here. If I could, I would create multiple interfaces (IFancyDisplayPercentage, IFancyDisplayInteger, ...), with 1 method each, and implement them as-needed on a per-class basis.

Please help :)

Thank you very much in advance!

Aucun commentaire:

Enregistrer un commentaire