I am building a simple adventure game that has "upgrades" feature.
In this game, user controls one player, that can be upgraded to increase things such as:
- Amount of gold earned
- Maximum health
- Attack power
- (more of this would be added later, for example: defense upgrade, experience, etc.)
An "upgrade" has a max level cap, effects on each level of upgrade, and the cost for each upgrade.
Here's my current approach.
This is my Upgrades
class, containing all possible upgrades:
public class Upgrades {
public static final int GOLD_LEVEL_CAP = 10;
public static final float GOLD_EFFECT_PER_LEVEL = 0.01f;
public static final int[] GOLD_UPGRADE_COSTS = {
100, 200, 300, 400, 500,
600, 700, 800, 900, 1000
};
public static final int HEALTH_LEVEL_CAP = 10;
public static final int HEALTH_EFFECTS_PER_LEVEL = 100;
public static final int[] HEALTH_UPGRADE_COSTS = {
100, 200, 300, 400, 500,
600, 700, 800, 900, 1000
};
public static final int POWER_LEVEL_CAP = 5;
public static final float POWER_EFFECT_PER_LEVEL = 0.05f;
public static final int[] POWER_UPGRADE_COSTS = {
1000, 2000, 3000, 4000, 5000
};
}
And this is the class that stores the current upgrades: (owned by Player
class)
public class UpgradeProgress {
private int goldUpgradeLevel = 0;
private int healthUpgradeLevel = 0;
private int powerUpgradeLevel = 0;
public void upgradeGold() {
if (goldUpgradeLevel < Upgrades.GOLD_LEVEL_CAP) {
goldUpgradeLevel++;
}
}
// Getter
public int getGoldUpgradeLevel() {
return goldUpgradeLevel;
}
// and so on..
// same for Health and Power
}
However, if I want to add a new upgrade, for example, I wanted to add a "Defense Upgrade". I should modify both Upgrades
and UpgradeProgress
classes.
Is this efficient and acceptable?
Or is there any better approach?