java – Learn how to retailer situation dependent merchandise info in JSON

java – Learn how to retailer situation dependent merchandise info in JSON

[ad_1]

I would not do that based mostly on merchandise title. As a substitute, I might implement a system of modifiers to explain what an merchandise does, one thing like…

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

Once you load this merchandise from JSON, you string collectively an identical sample of polymorphic impact nodes, every with their very own Apply technique, one thing 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 impact : effectsIfMet)
                impact.Apply(char);
        } else {
            for(ItemEffect impact : effectsIfNotMet)
                impact.Apply(char);
        }
    }
}

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

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

When the merchandise is supplied, you run the Apply() strategies on all its top-level results. When the character’s standing modifications in a method which may have an effect on gear properties, you reset the character’s stats to base and re-run the Apply() strategies of all geared up gear to search out the brand new internet end result. (Extra subtle caching to keep away from redundant processing is feasible, however do it the easy method first to verify if you actually need the added complexity)

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply