Last active
June 27, 2024 10:36
-
-
Save JakeSteam/4d843cc69dff4275acd742b70d4523b6 to your computer and use it in GitHub Desktop.
"Converting Levels Into XP & Vice Versa" for GameDevAlgorithms.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int convertXpToLevel(int xp) { | |
// Level = 0.05 * sqrt(xp) | |
return (int) (Constants.LEVEL_MODIFIER * Math.sqrt(xp)); | |
} | |
public static int convertLevelToXp(int level) { | |
// XP = (Level / 0.05) ^ 2 | |
return (int) Math.pow(level / Constants.LEVEL_MODIFIER, 2); | |
} | |
public static int getXp() { | |
Statistic xpInfo = Select.from(Statistic.class).where( | |
Condition.prop("statistic_id").eq(Constants.STATISTIC_XP)).first(); | |
return xpInfo.getIntValue(); | |
} | |
public static int getLevel() { | |
return convertXpToLevel(getXp()); | |
} | |
public static int getLevelProgress() { | |
int currentXP = getXp(); | |
int currentLevelXP = convertLevelToXp(getLevel()); | |
int nextLevelXP = convertLevelToXp(getLevel() + 1); | |
double neededXP = nextLevelXP - currentLevelXP; | |
double earnedXP = nextLevelXP - currentXP; | |
return 100 - (int) Math.ceil((earnedXP / neededXP) * 100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment