Hey!
So I've been working on a health bar which shrinks and grows according to how much health your character has.
The problem is I don't know how to add a smooth increment to health regen.
Say if I want to gain 600 life over 4 seconds, how would I go about achieving this? Will I have to use float numbers, and in what way? The health bar checks how many pixels the bar should shrink and grow depending on the health value.
This is my code right now.
CHealthBart.cpp
void CHealthBar::OnRender(SDL_Surface* _destination)
{
SDL_FillRect(_destination, &BarBackground, SDL_MapRGB(_destination->format, 0, 0, 0));
if( (float)Owner->GetCurrentHealth() / Owner->GetMaxHealth() > 0.6f )
{
SDL_FillRect(_destination, &Bar, SDL_MapRGB(_destination->format, HighHealth.r, HighHealth.g, HighHealth.b));
}
if ( (float)Owner->GetCurrentHealth() / Owner->GetMaxHealth() <= 0.6f && Owner->GetCurrentHealth() / Owner->GetMaxHealth() < 0.25f )
{
SDL_FillRect(_destination, &Bar, SDL_MapRGB(_destination->format, AverageHealth.r, AverageHealth.g, AverageHealth.b));
}
if( (float)Owner->GetCurrentHealth() / Owner->GetMaxHealth() <= 0.25f)
{
SDL_FillRect(_destination, &Bar, SDL_MapRGB(_destination->format, LowHealth.r, LowHealth.g, LowHealth.b));
}
}
void CHealthBar::OnLoop()
{
BarBackground.x = Owner->GetX();
BarBackground.y = ( Owner->GetY() - 5 );
Bar.x = ( Owner->GetX() + 1 );
Bar.y = ( Owner->GetY() - 4 );
float tempw = (float)Owner->GetCurrentHealth() / Owner->GetMaxHealth();
Bar.w = MaxBarLength * tempw;
}
Would be great if someone could give a good answer on this.