Created
December 1, 2022 14:23
-
-
Save Mordo95/024e008815fd6aa6c60027e7ac268843 to your computer and use it in GitHub Desktop.
A simple XP table / Leveling system in Typescript. Because why not. Most of the formulas have been sucked out of my thumb but work according to how I want it to
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
import XpTable from "./XpTable"; | |
export default class Skillable { | |
protected _xp: number = 0; | |
get xp() { | |
return this._xp; | |
} | |
set xp(val) { | |
this._xp = val; | |
} | |
get level() { | |
let level = 0; | |
let totalXp = this._xp; | |
while (totalXp > 0) { | |
totalXp -= XpTable.xpNeededForLevel(level); | |
if (totalXp > 0) level++; | |
} | |
return level == 0 ? 1 : level; | |
} | |
get xpNeededForNextLevel() { | |
const next = XpTable.xpUntil(this.level + 1); | |
return next - this._xp; | |
} | |
} |
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
// Base XP, does nothing in this formula besides increase numbers | |
const B = 4 as const; | |
// Modifier, change this to change the XP curve. Lower means faster leveling, higher means slower leveling. | |
const M = 0.8 as const; | |
export default class XpTable { | |
static xpUntil(level: number) { | |
return [...Array(level).keys()].reduce((sum, l) => (sum += this.xpNeededForLevel(l)), 0) + B; | |
} | |
static xpNeededForLevel(level: number) { | |
return B + Math.round(B * (M * level) * ((level * level) / 100)); | |
} | |
static calculateAwardableXp(level: number) { | |
const base = Math.floor(B * (1 + level / 2)); | |
const maxXp = base; | |
const minXp = base - base / 2; | |
//return Math.random() * (maxXp - minXp + 1) + minXp; | |
return maxXp; | |
} | |
static XpToLevel(totalXp: number) { | |
let level = 0; | |
while (totalXp > 0) { | |
totalXp -= this.xpNeededForLevel(level); | |
if (totalXp > 0) level++; | |
} | |
return level == 0 ? 1 : level; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment