I'm developing a RPG-game. After searching about various ways to save item information in RPG-game, I determined to save information in JSON files. The problem is that in my game, some equipment's stat depends on the character condition.
class Equipment {
String name;
double strength;
double agility;
Equipment(String name) {
this.name = name;
}
checkCondition(Character character) {
switch (this.name) {
case "itemA" -> {
strength = 20;
//This is what I mean by "condition dependent item"
agility = character.strength > 30 ? 20 : 10;
}
case "itemB" -> strength = 40;
}
}
}
As you can see, "itemA"'s stat is dependent to characters' strength.
{
"name": "itemB",
"strength": 40,
"agility": 0
}
This is how I planned to save the items, but "itemA" doesn't seem to fit in this form. Please let me know you have an idea to solve this problem.
checkCondition
method is a textbook OCP violation which should ideally be refactored to not explicitly list every possible string value that could be used for the item's name, instead relying on either inheritance or composition to figure out how to calculate stats based on the item's type. – Flater Dec 19 '23 at 00:36