Skip to main content
Elaborating with code sample
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

When you load this item from JSON, you string together a matching pattern of polymorphic effect nodes, each with their own Apply method., something like this:

public class StatCheckEffect implements ItemEffect {
    public Stat stat;
    public int threshold;
    
    public ItemEffect[] effectsIfMet;
    public ItemEffect[] effectsIfNotMet;

    public void Apply(Character char) {
        if (char.GetStat(stat) >= threshold) {
            for(ItemEffect effect : effectsIfMet)
                effect.Apply(char);
        } else {
            for(ItemEffect effect : effectsIfNotMet)
                effect.Apply(char);
        }
    }
}

public class StatBoostEffect implements ItemEffect {
    public Stat stat;
    public int value;

    public void Apply(Character char) {
        char.ModifyStat(stat, value);
    }
}

When the item is equipped, or whenyou run the Apply() methods on all its top-level effects. When the character's status changes in a way that might affect equipment properties, you reset the character's stats to base and re-run the Apply() methods of all equipped gear to find the new net result. (More sophisticated caching to avoid redundant processing is possible, but do it the simple way first to check if you really need the added complexity)

When you load this item from JSON, you string together a matching pattern of polymorphic effect nodes, each with their own Apply method.

When the item is equipped, or when the character's status changes in a way that might affect equipment properties, you re-run the Apply() methods of all equipped gear.

When you load this item from JSON, you string together a matching pattern of polymorphic effect nodes, each with their own Apply method, something like this:

public class StatCheckEffect implements ItemEffect {
    public Stat stat;
    public int threshold;
    
    public ItemEffect[] effectsIfMet;
    public ItemEffect[] effectsIfNotMet;

    public void Apply(Character char) {
        if (char.GetStat(stat) >= threshold) {
            for(ItemEffect effect : effectsIfMet)
                effect.Apply(char);
        } else {
            for(ItemEffect effect : effectsIfNotMet)
                effect.Apply(char);
        }
    }
}

public class StatBoostEffect implements ItemEffect {
    public Stat stat;
    public int value;

    public void Apply(Character char) {
        char.ModifyStat(stat, value);
    }
}

When the item is equipped, you run the Apply() methods on all its top-level effects. When the character's status changes in a way that might affect equipment properties, you reset the character's stats to base and re-run the Apply() methods of all equipped gear to find the new net result. (More sophisticated caching to avoid redundant processing is possible, but do it the simple way first to check if you really need the added complexity)

Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

I wouldn't do this based on item name. Instead, I'd implement a system of modifiers to describe what an item does, something like...

{
    name: "itemA",
    displayName: "Slippers of Mighty Grace",
    effects: [
        {
            type: "statCheck",
            stat: "strength",
            threshold: 31,
            effectsIfMet: [
                {
                    type: "statBoost",
                    stat: "agility",
                    value: 20
                }
            ],
            effectsIfNotMet: [
                {
                    type: "statBoost",
                    stat: "agility",
                    value: 10
                }
            ]
        }
    ]
}

When you load this item from JSON, you string together a matching pattern of polymorphic effect nodes, each with their own Apply method.

When the item is equipped, or when the character's status changes in a way that might affect equipment properties, you re-run the Apply() methods of all equipped gear.