HealthBar
Model implementation for the Health Bar entity. It uses dynamically drawn shapes to render a health bar above entities like players, towers, and zombies.
HealthBar
Extends Entity.
Properties
| Property | Type | Description |
|---|---|---|
barColor | object | The RGB color of the health bar. Defaults to green { r: 100, g: 161, b: 10 }. |
width | number | The width of the health bar. Defaults to 84. |
height | number | The height of the health bar. Defaults to 12. |
percent | number | The current health percentage (from 0.0 to 1.0). |
backgroundNode | DrawEntity | The dark, semi-transparent background shape. |
barNode | DrawEntity | The colored shape representing current health. |
health | number | The current health value. |
maxHealth | number | The maximum health value. |
Methods
constructor()
function constructor(barColor?: object): voidInitializes the model. Sets up the backgroundNode and barNode drawing them as rounded rectangles, attaching them, and defining a default pivot point of (width / 2, -64). Sets default health and max health to 100. An optional barColor can be provided to override the default green.
setSize()
function setSize(width: number, height: number): voidReconfigures the width and height of the bar. Redraws the backgroundNode and barNode, updates the pivot point, and reapplies the current percentage.
setHealth()
function setHealth(health: number): voidUpdates the current health value and recalculates the percentage by calling setPercent.
setMaxHealth()
function setMaxHealth(max: number): voidUpdates the maximum health value and recalculates the percentage by calling setPercent.
setPercent()
function setPercent(percent: number): voidUpdates the visual representation of the health bar. Uses setScaleX on the barNode for efficient resizing without needing to redraw the shape.
update()
function update(dt: number, user: any): voidUpdates the model every frame. Updates health and max health from tick.health and tick.maxHealth. Counter-rotates the health bar to negate the parent entity's rotation, ensuring the bar always remains horizontal.