Jump to content
  • Advertisement
Sign in to follow this  
hiddendragon

Hp recovery?

This topic is 3490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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..?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!