NOGDUS

Articles, Tutorials, and other things. => General Game Programming => Topic started by: Richard Marks on January 14, 2012, 09:38:30 AM



Title: How to Calculate Health or Armor Percentage
Post by: Richard Marks on January 14, 2012, 09:38:30 AM
How to Calculate Health or Armor Percentage

or, what the useful formula P = C / (M / 100) actually means

In this short article I am going to teach you how you can calculate the percentage of armor a unit currently has.
This calculation is useful when you need to draw a unit's health armor, or shields as a bar gauge.

I am going to use a plain-text English based pseudo-code for all code examples.
Hopefully this will allow you to easily translate the code into any language that you are using.
Lets start off with some basic terms and declarations.

A Unit for the context of this article refers to a game entity, such as the player, or an enemy.
The Unit will have current and maximum values for the Unit's Health and Armor.

Here I define a data structure for the Unit.
Remember, this is not any specific programming language here. I'm just trying to explain things as openly as possible.
Code:
structure begin Unit
    currentHealth as integer
    maximumHealth as integer
    currentArmor as integer
    maximumArmor as integer
structure end Unit

With the Unit structure defined, let us now create an instance of the Unit and give the Unit some default values.
Code:
create unit as new Unit
set maximumHealth in unit to 100
set maximumArmor in unit to 10
set currentHealth in unit to maximumHealth in unit
set currentArmor in unit to maximumArmor in unit

In the above code, I have created an instance of our Unit data structure and started the unit off with 100 health and 10 armor, fully healed and with no damage to the armor.

Now, let us define a function which tells us the percentage of health the unit currently has.
The function takes in a reference of a Unit data structure and returns an integer.

We need to calculate using floating points, so I am going to declare local floating points values (float) to cast the unit properties.
Code:
function begin HealthPercentage
    input unit as Unit
    output percentage as integer
    local hp as float
    local maxHp as float

    set hp to currentHealth in unit as float
    set maxHp to maximumHealth in unit as float
    set percentage to hp dividedby maxHp times 0.01

function end HealthPercentage

This function will return an integer value between 0 and 100 which is the percentage of health the unit currently has.

For completeness, using the same pseudo-code, I will finish off the example with the function's usage and an example of how to draw a health bar. The Armor can be calculated in the same manner, so I will not duplicate the code to show that.
Comments in the code start off with a us dollar sign: $

Code:
$ declare a local variable to hold the health percentage
local p as integer

$ get the percentage of health the unit has
set p to HealthPercentage using unit

$ declare a local variable to hold the width of the health bar
local barWidth as integer

$ the width of the health bar when the unit is fully healed should be 32 pixels wide
$ so we multiply 32 times the percentage to get the width of the health bar
set barWidth to p times 32

$ draw the health bar as a filled rectangle
draw filled rectangle at x of 0 and y of 0 with width of barWidth and height of 8

I hope that this article has been useful. Since some people may not understand the pseudo-code I have written, here is the above examples written in C# and then ActionScript 3.0. Please note that I have not added any "main" code, just the coded examples. They will not run as-is. They are intended to be used as a guide, not a copy/paste/use scenario.

C# Example:
Code:
namespace EXAMPLE
{
public class Unit
{
public int currentHealth;
public int maximumHealth;
public int currentArmor;
public int maximumArmor;
}

public class Example
{
public Example()
{
Unit unit = new Unit();
unit.maximumHealth = 100;
unit.maximumArmor = 10;
unit.currentHealth = unit.maximumHealth;
unit.currentArmor = unit.maximumArmor;

int p = HealthPercentage(unit);
int barWidth = p * 32;

GraphicsAPI.DrawFilledRectangle(0, 0, barWidth, 8);
}

public int HealthPercentage(Unit unit)
{
float hp = (float)(unit.currentHealth);
float maxHp = (float)(unit.maximumHealth);
return (int)(hp / (maxHp * 0.01));
}
}
}

ActionScript 3.0 Example:
Code:
package
{
public class Example
{
public function Example()
{
var unit:Unit = new Unit;
unit.maximumHealth = 100;
unit.maximumArmor = 10;
unit.currentHealth = unit.maximumHealth;
unit.currentArmor = unit.maximumArmor;

var p:int = healthPercentage(unit);
var barWidth:int = p * 32;

GraphicsAPI.drawFilledRectangle(0, 0, barWidth, 8);
}

public function healthPercentage(Unit unit):int
{
var hp:Number = Number(unit.currentHealth);
var maxHp:Number = Number(unit.maximumHealth);
return hp / (maxHp * 0.01);
}
}
}

class Unit
{
public int currentHealth;
public int maximumHealth;
public int currentArmor;
public int maximumArmor;
}