"Converting Levels Into XP & Vice Versa" for GameDevAlgorithms.com
This file contains 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