MrxGuiHudHealthCounter
Module: mrxguihudhealthcounter.lua
Overview
The MrxGuiHudHealthCounter module is responsible for managing the health counter display on the HUD. It handles various events related to health changes, visibility, and icon updates for both human and vehicle health counters. The module ensures that the health bar’s color, length, and visibility are appropriately adjusted based on the current health status.
Inheritance
-
Inherits from:
none(base/utility module) -
Imports:
MrxGui,MrxUtil
Instance pattern
This is a stateless manager/utility module. It does not track per-instance state but manages global settings and functions related to health counter display. Key fields include:
-
Constants:
-
_knShowTime = 2: Duration for which the health counter is visible. -
_knPulsingThreshold = 20: Health percentage below which pulsing effect starts. -
_knVisibleThreshold = 100: Health percentage above which the counter remains visible. -
_knPulseTime = 0.4: Duration of a single pulse animation cycle.
-
-
Tables:
-
_tDefaultIcon: Default icon for human health counter with texture coordinates.{ texture = "global_gui_hud02", u1 = 0.443359, v1 = 0.466797, u2 = 0.552734, v2 = 0.576172 } -
_tArmorToIcon: Mapping of vehicle armor labels to corresponding icons.{ ArmorVehicle = { texture = "HUD_vehicle_armor_1", u1 = 0, v1 = 0, u2 = 1, v2 = 1 }, ArmorLight = { texture = "HUD_vehicle_armor_2", u1 = 0, v1 = 0, u2 = 1, v2 = 1 }, ArmorMedium = { texture = "HUD_vehicle_armor_3", u1 = 0, v1 = 0, u2 = 1, v2 = 1 }, ArmorTank = { texture = "HUD_vehicle_armor_4", u1 = 0, v1 = 0, u2 = 1, v2 = 1 } }
-
```
Functions
HandleHealthChangedEventNew(oWidget, nCurHealth, nMaxHealth, bVehicle)
-
Updates the health counter text and handles color transitions based on health changes.
-
Adjusts the health bar’s length and color to reflect current health.
HandleInitializationNew(oWidget)
- Initializes custom data for the widget, storing its initial color and setting default values.
HandleHealthChangedEventMain(oWidget, nCurHealth, nMaxHealth, bVehicle)
-
Updates the health counter’s visibility and pulsing effect based on current health.
-
Adjusts the health bar’s length and color to reflect current health.
_SetCounterVisible(oUnused, oCounter)
- Sets the visibility of a counter widget to true.
HandleUpdateMain(oWidget, nDeltaTime)
-
Manages the countdown for hiding the health counter after it becomes fully visible.
-
Hides the counter if its remaining time reaches zero.
_LoopToRed(oWidget)
- Animates the health bar’s color to red and loops back to neutral.
_LoopToNeutral(oWidget)
- Animates the health bar’s color back to neutral and loops to red.
HandleShowHealthEvent(oWidget, tEvent)
- Shows the health counter if it is hidden and sets its remaining time based on an event.
HandleInitializationMain(oWidget)
-
Initializes various animation points and custom data for the widget.
-
Sets up event handlers for
GuiUpdateandShowAllCounters.
HandleIconUpdateHuman(oWidget, nCurHealth, nMaxHealth, bVehicle)
- Updates the translucency of the human health icon based on whether it’s a vehicle.
SetVehicleIcon(oWidget, tIcon)
- Sets the texture and coordinates for the vehicle icon widget.
HandleIconUpdateVehicle(oWidget, nCurHealth, nMaxHealth, bVehicle)
-
Updates the translucency and icon of the vehicle health counter.
-
Determines the correct armor icon based on vehicle labels.
HandleUpdateEvent(oWidget, nTimeSinceLastUpdate)
-
Updates the health bar’s length and texture coordinates based on time since last update.
-
Manages the ghost bar’s position to create a depleting effect.
Min(nA, nB)
- Returns the minimum of two numbers.
HandleHealthChangedEvent(oWidget, nCurHealth, nMaxHealth)
-
Updates the health counter text and handles color transitions based on health changes.
-
Adjusts the health bar’s length and visibility to reflect current health.
HandleInitialization(oWidget, Event)
- Initializes custom data for the widget, storing its initial health value and bar length.
HandleVehicleInitialization(oWidget, Event)
- Sets the visibility of the vehicle icon to false and initializes it like a regular health counter.
HandleUpdateEventForBackground(oWidget, nTimeSinceLastUpdate)
-
Updates the background color of the health bar based on pulsing effects.
-
Manages transitions between white and red colors for pulsing effect.
HandleVehicleEvent(oWidget, nCurHealth, nMaxHealth, bInVehicle)
- Handles vehicle-specific logic for the health counter, including visibility and transition effects.
HandleE3HudModeEvent(oWidget, tEvent)
This function handles the E3 HUD mode event for a given widget. It checks if the event is turned on or off and adjusts the visibility of the widget accordingly.
-
If
tEvent.bOnis true:-
If the widget is not already in E3 HUD mode (
oWidget.CustomData.bE3HudModeis false):-
If the widget is currently invisible, it sets a flag
bStayInvisibleto true. -
It hides the widget by setting its visibility to false.
-
It marks the widget as being in E3 HUD mode by setting
oWidget.CustomData.bE3HudModeto true.
-
-
-
If
tEvent.bOnis false:-
If the widget should not stay invisible (
bStayInvisibleis false), it sets the widget’s visibility to true. -
It marks the widget as no longer being in E3 HUD mode by setting
oWidget.CustomData.bE3HudModeto false.
-
DrawDebugRectangle(TargetWidget, t)
This function draws a debug rectangle on a target widget. It creates a text command with specified properties and adds it to the widget’s drawing commands.
-
The text command is created as a table with the following fields:
-
CommandType: “text” -
x: 100 -
y: 100 -
text: The input stringt -
font: “lucida12” -
RedLevel: 255 (red) -
GreenLevel: 0 (green) -
BlueLevel: 0 (blue) -
TranslucencyLevel: 255 (fully opaque) -
HorizontalAnchor: “left” -
VerticalAnchor: “top”
-
-
The text command is added to the first position of
TargetWidget.DrawingCommands.
Events
This module subscribes to and responds to several engine events:
-
GuiUpdate: Triggered by the GUI update loop, this event callsHandleUpdateMainto manage the countdown for hiding the health counter after it becomes fully visible. -
ShowAllCounters: This event is handled byHandleInitializationMain, which initializes various animation points and custom data for the widget. -
HealthChangedEventNew: Triggered when the health of a widget changes, this event callsHandleHealthChangedEventNewto update the health counter text and handle color transitions based on health changes. -
ShowHealthEvent: This event is handled byHandleShowHealthEvent, which shows the health counter if it is hidden and sets its remaining time based on an event. -
E3HudModeEvent: Triggered when the E3 HUD mode is toggled, this event callsHandleE3HudModeEventto adjust the visibility of the widget accordingly.
Notes for modders
-
Call-order requirements: Ensure that
HandleInitializationMainis called before any other functions to properly initialize the widget’s custom data and event handlers. -
Pitfalls: Be cautious with modifying the health counter’s visibility and pulsing effects, as incorrect handling can lead to visual inconsistencies or performance issues.
-
Tunables: The following constants can be adjusted for different behaviors:
-
_knShowTime: Duration for which the health counter is visible. -
_knPulsingThreshold: Health percentage below which pulsing effect starts. -
_knVisibleThreshold: Health percentage above which the counter remains visible. -
_knPulseTime: Duration of a single pulse animation cycle.
-
-
Decompiler artifacts: The function
Min(nA, nB)is used to return the minimum of two numbers. This function appears redundant as Lua’s built-inmath.mincould be used instead, but it is present in the decompiled code.