Hp recovery?
hi iam new to programming
and im recently working on this very simple MMO text game
and i made a hp/energy bar
but i cant figure how to code the codes for the hp recovery over time
any helpw would be appreciated thx
If you know the lapsed time in milliseconds between two calls,you can do something like :
[Edited by - Black Knight on April 22, 2009 3:45:23 AM]
hpAccumulator += elapsedTime;int hpRegen = 2;if(hpAccumulator > 60000){ hp+=hpRegen; if(hp>hpMax) hp=hpMax; hpAccumulator -= 60000;}
[Edited by - Black Knight on April 22, 2009 3:45:23 AM]
Quote:Original post by Black Knight
If you know the lapsed time in milliseconds between two calls,you can do something like :
*** Source Snippet Removed ***
Wouldn't hpAccumulator-=60000; be more accurate in the end ? (otherwise any overflow in time would be lost)
Yes,but I don't know if it will be noticable.If you are running at 30fps elapsed time will be ~33 milliseconds,so you will get at most 33 ms delays.Though it will get worse as fps drops so its better to make it right [smile]
ok i have this code
list($usec, $sec) = explode(' ', microtime());
$script_start = (float) $sec + (float) $usec;
//function code here
list($usec, $sec) = explode(' ', microtime());
$script_end = (float) $sec + (float) $usec;
$elapsed_time = round($script_end - $script_start, 5);
$hpAccumulator += $elapsed_time;
$hpRegen = 10;
$enperc=(int) ($ir['energy']/$ir['maxenergy']*100);
$wiperc=(int) ($ir['will']/$ir['maxwill']*100);
$experc=(int) ( $ir['exp']/$ir['exp_needed']*100);
$brperc=(int) ($ir['brave']/$ir['maxbrave']*100);
$hpperc=(int) ($ir['hp']/$ir['maxhp']*100);
if($hpAccumulator > 2)
{
$enperc += $hpRegen;
if($hp > $ir['maxhp'])
$hp=$ir['maxhp'];
$hpAccumulator -= 1000;
}
but the accumulator is not adding up :/
it work, but it doesnt stay perminate, if u refresh it the the elapsed time < hpaccumlator the added hp disapear
list($usec, $sec) = explode(' ', microtime());
$script_start = (float) $sec + (float) $usec;
//function code here
list($usec, $sec) = explode(' ', microtime());
$script_end = (float) $sec + (float) $usec;
$elapsed_time = round($script_end - $script_start, 5);
$hpAccumulator += $elapsed_time;
$hpRegen = 10;
$enperc=(int) ($ir['energy']/$ir['maxenergy']*100);
$wiperc=(int) ($ir['will']/$ir['maxwill']*100);
$experc=(int) ( $ir['exp']/$ir['exp_needed']*100);
$brperc=(int) ($ir['brave']/$ir['maxbrave']*100);
$hpperc=(int) ($ir['hp']/$ir['maxhp']*100);
if($hpAccumulator > 2)
{
$enperc += $hpRegen;
if($hp > $ir['maxhp'])
$hp=$ir['maxhp'];
$hpAccumulator -= 1000;
}
but the accumulator is not adding up :/
it work, but it doesnt stay perminate, if u refresh it the the elapsed time < hpaccumlator the added hp disapear
The amount you subtract from the accumulator should be the same as the threshold value that you check:
Do you understand why?
Also, you are adding the hpRegen to "enperc", which certainly won't do what you want. You want to add the hpRegen to ir['hp'], which is where the current hitpoints are stored. Right? Similarly, you want to compare that value to ir['maxhp'] when you check for the HP limit. Thus, for example:
if($hpAccumulator > SOME_VALUE){// ...$hpAccumulator -= SOME_VALUE; // the same value as above!}
Do you understand why?
Also, you are adding the hpRegen to "enperc", which certainly won't do what you want. You want to add the hpRegen to ir['hp'], which is where the current hitpoints are stored. Right? Similarly, you want to compare that value to ir['maxhp'] when you check for the HP limit. Thus, for example:
if($hpAccumulator > 1000){$ir['hp'] += $hpRegen;if($ir['hp'] > $ir['maxhp'])$ir['hp'] = $ir['maxhp'];$hpAccumulator -= 1000;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement