in my app I get a var $curxp
which holds an int, now I want to create a function that automatically returns $xplvlup
(which holds an int how much total XP is needed for the next level and a function that returns the current level.
Now I could simply hardcode with switch statements and calculated numbers like:
switch($curxp){
case <10: $xplvlup = 10; break;
}
But it would be much nicer if I could use an algorithm so there is no max level. I know I开发者_如何学Go have to work with exponents to get a nice curve, but I just dont know how to start things up.
UPDATE
Thanks to Oltarus I came to the following solution:
$curxp = 20;
function level($lvl){
return $xp = pow($lvl,2) + 5 * $lvl;
}
$lvl = 0;
while (level($lvl) < $curxp) $lvl++;
$totxp = level($lvl);
$xplvlup = level($lvl) - $curxp;
echo 'Level: '.$lvl."<br />";
echo 'Total XP: '.$totxp."<br />";
echo 'XP needed for Levelup: '.$xplvlup;
If for instance you would have the first level-up require 500 xp, and then every level you will need 10% more xp you could do something like this:
function xp_needed($cur_lvl){
$start = 500;
return $start*pow(1.1,($cur_lvl-1));
}
For every level, the xp is calculated by 500 * 1.1^(level-1)
Edit
Woops, $cur_lvl
should be substracted by 1.
I don't know how you calculate the levels, but let's say you have a function level($n)
that returns how many XP are needed to have level $n
.
$n = 0;
while (level($n) < $curxp) $n++;
$xplvlup = level($n) - $curxp;
精彩评论