英文:
Leveling System - Carry extra XP over to next Level
问题
我正在创建一个基本的级别系统,但我遇到了一些问题,这更像是一个数学问题而不是Java问题。我想要动态地将任何额外的经验点转移到下一个级别,目前一旦达到升级要求,经验点数就会被设置为0。请注意,我希望这能处理玩家一次获得足够经验升级多个级别的情况。
我的当前代码如下:
public void giveXP(String name, int xp) {
int pXP = Main.storedUsers.get(name).getXP();
this.xp = (pXP + xp);
int pLevel = Main.storedUsers.get(name).getLevel();
double constant = 2;
double fixed = 450;
int nextLevelXP = Math.round((pLevel * constant) + fixed);
int xpNeededToLevelUp = nextLevelXP - pXP;
if (xpNeededToLevelUp > 0) {
Admin.debugMessage("You gained " + xp + " XP, " + xpNeededToLevelUp + " needed for next Level.");
} else if (xpNeededToLevelUp <= 0) {
Admin.debugMessage(name + " leveled up to level " + (pLevel+1));
UserFetch.setLevel(name, (pLevel+1));
Main.storedUsers.get(name).setLevel(pLevel+1);
this.xp = 0;
}
}
这是您提供的Java代码的翻译。
英文:
I am creating a basic Levelling system however I have hit a bit of a snag, this is more of a Math problem than a Java one. I would like to dynamically carry over any additional XP over to the next level, currently once the Level up requirement has been it, the XP count simply gets set to 0. Please note that I would like this to be able to handle the use case if the Player gets enough XP to go up several levels at once.
My current code is below :
public void giveXP(String name, int xp) {
int pXP = Main.storedUsers.get(name).getXP();
this.xp = (pXP + xp);
int pLevel = Main.storedUsers.get(name).getLevel();
double constant = 2;
double fixed = 450;
int nextLevelXP = Math.round((pLevel * constant) + fixed);
int xpNeededToLevelUp = nextLevelXP - pXP;
if (xpNeededToLevelUp > 0) {
Admin.debugMessage("You gained " + xp + " XP, "+ xpNeededToLevelUp + " needed for next Level.");
}
else if (xpNeededToLevelUp <= 0) {
Admin.debugMessage(name + " leveled up to level " + (pLevel+1));
UserFetch.setLevel(name, (pLevel+1));
Main.storedUsers.get(name).setLevel(pLevel+1);
this.xp = 0;
}
}
答案1
得分: 0
只是为了更新一下,我解决了我之前遇到的问题,同时还能够处理多个连续的升级,代码如下:(唯一的问题是低等级中存在小的经验值差距,但那是一个数学问题)
public static int getLevelXPThreshold(int level) {
int value = (int) (75+(400+(75*(Math.pow(1.10409,level)))));
return value;
}
public static Map<Integer, Integer> getXPThresholds() {
int maxLevel = 90;
Map<Integer, Integer> levelXPThresholds = new HashMap<Integer, Integer>();
for (int level = 1; level < maxLevel; level++) {
int levelXP = getLevelXPThreshold(level);
levelXPThresholds.put(level, levelXP);
}
return levelXPThresholds;
}
public static int canUserLevelUp(User user) {
int xp = user.getXp();
Map<Integer, Integer> levelXPThresholds = getXPThresholds();
for (int levels : levelXPThresholds.keySet()) {
if (!(xp >= levelXPThresholds.get(levels))) {
if (user.getLevel() != levels) {
UserFetch.setLevel(user.getName(), (levels));
Main.storedUsers.get(user.getName()).setLevel(levels);
}
int nextLevelXP = getLevelXPThreshold(levels);
return nextLevelXP -= xp;
}
}
return 0;
}
英文:
Just to update how I solved the issue I was having while also being able to handle multiple level ups at once, the code is below : (The only issue is the small XP gaps in lower levels but that is a math issue)
public static int getLevelXPThreshold(int level) {
int value = (int) (75+(400+(75*(Math.pow(1.10409,level)))));
return value;
}
public static Map<Integer, Integer> getXPThresholds() {
int maxLevel = 90;
Map<Integer, Integer> levelXPThresholds = new HashMap<Integer, Integer>();
for (int level = 1; level < maxLevel; level++) {
int levelXP = getLevelXPThreshold(level);
levelXPThresholds.put(level, levelXP);
}
return levelXPThresholds;
}
public static int canUserLevelUp(User user) {
int xp = user.getXp();
Map<Integer, Integer> levelXPThresholds = getXPThresholds();
for (int levels : levelXPThresholds.keySet()) {
if (!(xp >= levelXPThresholds.get(levels))) {
if (user.getLevel() != levels) {
UserFetch.setLevel(user.getName(), (levels));
Main.storedUsers.get(user.getName()).setLevel(levels);
}
int nextLevelXP = getLevelXPThreshold(levels); //<--- this means it will always return the level higher than the current user level based on xp value.
return nextLevelXP -= xp;
}
}
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论