Skip to content

Health Bar Component

Rowan Gray edited this page Oct 19, 2023 · 4 revisions

First see Components.

The HealthBarComponent is a custom UI component used to display the current health of the entity. The health bar requires the entity to also have a CombatStatsComponent attached since an Entity doesn't have a health property by default.

The health bar is only visible when the entities health is not equal to its maxHealth. This is to avoid cluttering the screen unnecessarily.

Constructors

The HealthBarComponent can be created in two ways. The first way takes in a boolean value which indicates whether the health bar should be centred with the entity on the y axis.

public HealthBarComponent(boolean center);

It can alternatively be created by providing a boolean value which indicates whether the health bar should be centred with the entity on the y axis, an offset on the y axis, and a width.

public HealthBarComponent(boolean center, float offsetY, float width);

Default values

If you call the first constructor (with one parameter) the default value for offsetY will be 0, and the default value for width will be 0.9f.

Usage

First ensure that the entity you wish to add this to also has a CombatStatsComponent.

You can add it to an entity as follows.

entity.addComponent(new HealthBarComponent(true));

If you want to place the health bar above an entity, you can do so as follows.

entity.addComponent(new HealthBarComponent(true, entity.getScale().y + 0.2f, 0.9f));

The EntityHealthComponent will automatically update the entity's health when it changes.

Sequence Diagram

The following sequence diagram shows you how the HealthBarComponent interacts with the CombatStatsComponent and Entity classes.

sequenceDiagram
    participant HealthBarComponent
    participant CombatStatsComponent

    HealthBarComponent ->>+ Entity: getEvents();
    Entity ->>- HealthBarComponent: events
    HealthBarComponent ->>+ EventHandler: events.addListener("updateHealth", this.updateHealth)
    CombatStatsComponent ->>+ Entity: getEvents();
    Entity ->>- CombatStatsComponent: events
    CombatStatsComponent ->>+ EventHandler: events.trigger("updateHealth", newHealth);
    EventHandler ->>- HealthBarComponent: updateHealth(newHealth)
    alt is full health
    HealthBarComponent ->>+ HealthBarComponent: hide()
    else
    HealthBarComponent ->>+ HealthBarComponent: show()
    end
Loading

Testing

Testing was implemented using Mockito and JUnit to test each function for its expected functionality. Due to the fact that our understanding of Mockito isn't fully developed, we were unable to determine a way to test the draw function in time for the end of the sprint. This is something which we will come back to in Sprint 2 once we have a more solid understanding of Mockito.

Clone this wiki locally