Hp recovery?

Started by
9 comments, last by Zahlman 14 years, 11 months ago
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
Advertisement
"hp += 2" in your game loop perhaps?
how would you put a time frame to that?
so like +2/minute
If you know the lapsed time in milliseconds between two calls,you can do something like :
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]
oohh ic
k i get it now
thanks
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)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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
The amount you subtract from the accumulator should be the same as the threshold value that you check:

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;}
ohh lol kk i missed that

and no i actually do not understand y
$hpAccumulator -= SOME_VALUE need to the same value as if($hpAccumulator > SOME_VALUE)

and does this $ir['hp'] += $hpRegen; code actually go to the database and update the value of ['hp']?

dont u need to use the my_sql( UPDATE..?

This topic is closed to new replies.

Advertisement