Implementing Alerts for Low Player Health - UQcsse3200/2023-studio-2 GitHub Wiki

Introduction:

Java class called CompanionStatsDisplay is responsible for displaying companion statistics, such as health, as a User Interface (UI) component. Additionally, it implements a feature to display alerts when the player's health is low, and these alerts disappear after a few seconds. This wiki will provide a detailed explanation of how this feature is implemented, along with a UML diagram to visualize the class structure.

Class Overview:

The CompanionStatsDisplay class extends UIComponent and is used for managing the UI elements related to companion statistics. It has several instance variables and methods for updating and displaying companion health and handling low health alerts for the player.

1. Instance Variables:

Table table:

A UI layout element used for organizing UI components.

Table table2:

Another UI layout element for displaying low health alerts.

Entity playerEntity:

Represents the player entity in the game.

Label messageLabel:

Used for displaying companion health.

Label label:

Used for displaying low health alerts.

2. Constructor:

The class has one constructor which takes an Entity representing the player entity as a parameter.

3. create() Method:

This method is called to create the UI elements and set up event listeners for updating health and handling low health alerts.

4. addActors() Method:

Creates and positions UI elements (labels) for displaying companion health.

5. addAlert(int health) Method:

Displays a low health alert if the player's health is below a certain threshold. It creates a new table and label to display the alert and positions it near the companion.

6. updatePlayerHealthUI(int health) Method:

Updates the player's health on the UI and displays a low health alert if the player's health is low. It also schedules a task to remove the alert after a delay.

7. updateCompanionHealthUI(int health) Method:

Updates the companion's health on the UI.

8. dispose() Method:

Cleans up resources and removes UI elements when they are no longer needed.

The CompanionStatsDisplay class provides a comprehensive solution for displaying companion statistics and handling low health alerts for the player in a game application. It uses LibGDX UI components and Timer tasks to achieve this functionality.

Contributors:

Saakshi Gupta(@saakshigupta2002)