Search code examples
javaandroidlibgdx

LibGDX HealthBar: How can i improve it


I have been making a simple game using LibGDX and now I am trying to add a health bar in my game. I have successfully added the health bar in the game and it is working fine at this moment.

The problem , however, is how to further modify this code like professionals do. I am still in learning process so I have been trying to figure how to further make it look better, succinct, and professional but wan't able to furnish it further.

I have pasted my HealthBar class code below. I will appreciate any suggestion so that I can think better and efficient when writing any codes in future.

public class HealthBar extends Actor{

protected float MaxHealthX;
protected static float HealthBarY= 36;
float decreaseRate = 0;
float addHealth = 0;
float deductHealth = 0;
ShapeRenderer sr;
static private boolean projectionMatrixSet;
private boolean isHitE = false;
private boolean isHitI = false;
float totalSubractedHP = 0;
float totalAddedHP = 0;

public HealthBar(){

    sr = new ShapeRenderer();
    projectionMatrixSet = false;

}

@Override
public void draw(Batch batch, float parentAlpha){

    //if a main character is hit by enemy
    if(isHitE==true){
        //subtractHP();
        totalSubractedHP += 1;
        isHitE = false;
    }

    if(isHitI==true){
        totalAddedHP += 10;
        isHitI = false;
    }
    MaxHealthX=296-decreaseRate+totalAddedHP-totalSubractedHP;

    batch.end();
    if(!projectionMatrixSet){
        sr.setProjectionMatrix(batch.getProjectionMatrix());
    }
    sr.begin(ShapeType.Filled);
    sr.setColor(Color.BLACK);
    sr.rect(40,650,300,40);

    if(isOutOfHealth() == false){
    sr.setColor(Color.RED);
    sr.rect(42, 652, MaxHealthX, HealthBarY);
    }

    sr.end();

    batch.begin();

}

//updating health decrease rate as time passes by
public void updateHealthBar(float delta) {
    decreaseRate += 10*delta;       

}

//checks whether health is greater than zero
public boolean isOutOfHealth(){
    if(MaxHealthX > 0){
        return false;
    }else{
        return true;
    }
}


//checking if hit by enemy
public boolean isHitByEnemy(){
    isHitE = true;
    return isHitE;
}

public boolean isHitByItem(){
    isHitI = true;
    return isHitI;
}

Solution

  • ShapeRenderer is normaly only used for debuging. In the endgame most times only SpriteBatch is used.
    In the draw method you call batch.end() which calls flush(). This method is a bit "heavy" and should only be called, if it is really neccessary. So in your case it would be better to use batch instead of ShapeRenderer to draw the whole HealthBar.
    Even better:
    Use the Scene2D Progressbar, which should be exactly what you are looking for.
    This ProgressBar need a range (for example min = 0, max = 100), a stepSize (for example 1.0) a boolean vertical, and a ProgressBarStyle.
    The ProgressBarStyle in your case just needs 2 Drawables:
    backgroung
    disabledBackground

    You can then use setValue(maxHealthX), which will set the background from 0-maxHealthX (rounded to the nearest stepSize) and disabledBackground from maxHealthX-end of progressbar.