Character's Health Bar - UQcsse3200/2024-studio-1 GitHub Wiki

Objective

The objective of this task is to provide users with a real-time visual representation of player's health during gameplay. The health is displayed as a health bar above the player's head, offering an intuitive and immediate way to monitor health status. The health bar dynamically updates in response to changes in the player's health, ensuring that player are always aware of their current condition.

Implementation Details

Health Bar Design

The player Health Display component implements a straightforward health bar of width 1.5 and 0.1 height that represents the player's health as a coloured bar:

  • Red Bar: Indicates the total health capacity.
  • Green Bar: Fills over the red bar, it's width is proportional to the player's current health proportionally.

Positioning and Dimensions

  • Position: The health bar is positioned above the player's heads using a constant offset which enables the x coordinate of health bar match the x coordinate of player and y coordinate just slightly above the player.
  • Dimensions: The width and height of the health bar are fixed, but the green portion dynamically adjusts based on the player's health percentage.

Drawing the Health Bar

The PlayerHealthBar class uses the shapeRenderer class to draw the health bar:

  • Sprite Batch Handling: The sprite batch is ended before drawing with shapeRenderer and restarted afterward to ensure compatibility between the rendering methods.
  • Matrix Handling: The current projection matrix is copied to maintain the correct positioning of the shapes in the game world.

Event System Integration

This component does not directly rely on an event system. Instead, it continuously reflects the player's current health whenever the draw method is called. It accesses the CombatStatsComponent of the player entity to fetch the current health value.

Example Usage

Entity player = new Entity();
player.addComponent(new CombatStatsComponent(100,10));
player.addComponent(new PlayerHealthDisplay());

This example shows how to attach PlayerHealthDisplay component to a player entity with CombatStatComponent. The health bar will automatically display and update as the player's health changes.

UML Diagram

Behind The Scenes

Why ShapeRenderer?

Using ShapeRenderer to draw the health bar allow the health bar allows for precise control over the appearance and positioning of the health bar. This approach ensures that the health bar is always rendered correctly above the player's head, providing a clear and accurate display of the player's current health.

Future Reading

For more information on how to use ShapeRenderer and manage custom shapes in libGDX, refer to the Shape Renderer libGDX