Skip to content

Instantly share code, notes, and snippets.

@Eng-Bunnys
Created September 28, 2022 17:31
Show Gist options
  • Save Eng-Bunnys/b1dfc65512aa453a98fee94d10e881a6 to your computer and use it in GitHub Desktop.
Save Eng-Bunnys/b1dfc65512aa453a98fee94d10e881a6 to your computer and use it in GitHub Desktop.
Level System with carry over XP and extra levels
/**
What this does:
It checks if the users RP [XP] is larger than or equal to the required XP to level up, if larger than we have extra XP, we check if that extra XP can level the user up again and if it can't we set it as carryOverXp
*/
//This is the function that calculates the amount of XP required to level up once, you can of course change this to your own equation
//If you'd like to learn more about the mathmatics behind level systems go here: https://pavcreations.com/level-systems-and-character-growth-in-rpg-games/
function RPRequiredToLevelUp(rank) {
return rank * 800 + (rank - 1) * 400;
}
//This function takes in the user's current rank (before it's updated), currentRP : the user's current XP before any addition, addedRP (the user's XP after addition)
//5 (this is the user's level before any update), 200 (this is the XP that the user has), 400 (this is the XP that the user has after addition)
function checkRank(currentRank, currentRP, addedRP) {
let addedLevels = 0; //This is the number of extra levels added incase the user has XP to level up multiple times
let hasRankedUp = false; //This is a boolean to see if the user has leveled up or not, you can check if addedLevels is equal to 0 or not but I like to have this, makes my heart feel at ease
//This is the amount of XP required to go up a rank, here we add the user's current level with any added levels
let requiredRP = RPRequiredToLevelUp( currentRank + addedLevels, currentRP);
//5,000 is the max level
if (currentRank >= 5000) return;
//If the user has more XP than required
if (addedRP > requiredRP) {
hasRankedUp = true;
addedLevels++;
}
//Calculating how much XP the user now has after leveling up
let remainingRP = addedRP - requiredRP;
//Checking if there's any remaining XP, we're checking if the absolute function of the remainingRP is equal to the normal remainingRP, this is a method of checking if a number is negative or not
if (Math.abs(remainingRP) === remainingRP) {
//Looping until the remainingRP is no longer enough to level up the user
for (remainingRP; remainingRP > requiredRP; remainingRP -= requiredRP) {
//Adding an extra level
addedLevels++;
//Stopping if the user hit max rank
if (currentRank + addedLevels >= 5000) {
addedLevels--; //After testing I ran into a bug where the user would hit level 5,001 so to prevent this I added a -1 to the levels
break;
}
//Re-calculating the amount of XP required to level up so it calculates the amount of XP required for the next rank rather than the one before
requiredRP = RPRequiredToLevelUp(currentRank + addedLevels, currentRP);
}
}
//Returning an array that contains the boolean ranked up value, the amount of extra levels, the carry over XP
return [hasRankedUp, addedLevels, remainingRP];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment