Game Development Reference
In-Depth Information
5.2P ROGRESS B AR
In many cases the health bar is represented as a progress bar, and we may have more than
one bar, we may have health and mana, or perhaps a progress bar that represents a shield's
integrity. It's best to create a reusable progress bar class than to implement the progress bar
behavior in each instance that uses one.
There are many ways to implement progress bars and they will vary depending on the num-
ber of features needed, the progress bar presented here will work well for deterministic pro-
gress bars, such as health bars.
Non-deterministic progressbarsarenotinanywayusefulandshouldbeavoided.Ifthedur-
ation of a task is not known or is variable, it is better to replace the display to something
that is not meant to convey a finite amount of time, like a circular dial used by online video
streaming services. Non-deterministic progress bars do not convey useful information to the
user and often are a result of confusion or frustration.
class progress_bar : public control
{
public:
void SetMaximum(float maximum)
{
if ( maximum > 0.f )
{
float ratio = m_value / m_maximum;
m_maximum = maximum;
m_value = m_maximum * ratio;
}
else
{
throw new std::exception("The maximum must be greater than zero.");
}
}
void SetRatio(float ratio)
{
m_value = m_maximum * ratio;
math::Clamp(m_value, 0.f, m_maximum);
}
float GetRatio() const
{
return (m_maximum > 0.f) ? m_value / m_maximum : 0.f;
}
void SetValue(float value)
{
m_value = value;
math::Clamp(m_value, 0.f, m_maximum);
}
Search WWH ::




Custom Search