local mod = RegisterMod("Enemy Health Bar", 1) local game = Game() local font = Font()…
- User
local mod = RegisterMod("Enemy Health Bar", 1) local game = Game() local font = Font() font:Load("font/terminus.fnt") -- Ensure this font exists in your game files
function mod:PostRender() local room = game:GetRoom() local entities = Isaac.GetRoomEntities()
for _, entity in ipairs(entities) do if entity:IsActiveEnemy() and not entity:IsDead() then local maxHP = entity.MaxHitPoints local currentHP = entity.HitPoints if maxHP > 0 then local percentage = math.max(0, currentHP / maxHP) local pos = Isaac.WorldToScreen(entity.Position + Vector(0, -40)) -- Create health bar string local barLength = 5 -- Length of the health bar local filledLength = math.floor(barLength * percentage + 0.5) local emptyLength = barLength - filledLength local barString = "[" .. string.rep("=", filledLength) .. string.rep(" ", emptyLength) .. "]" -- Draw health bar font:DrawString(barString, pos.X - (barLength * 3), pos.Y, KColor(1, 0, 0, 1), 0, false) end end endend
mod:AddCallback(ModCallbacks.MC_POST_RENDER, mod.PostRender)
Don't show hp bar of bosses, allow us hide HP bars by pressing H button
- Assistant
To enhance your "Enemy Health Bar" mod by excluding boss enemies from displaying health bars and adding the functionality to toggle the visibility of health bars using the
Hkey, you'll need to make several adjustments to your existing Lua script. Below is the step-by-step guide along with the updated code:-
Exclude Boss Enemies from Health Bars:
To prevent health bars from appearing over boss enemies, you'll need to determine whether an entity is a boss. This can typically be done by checking if an enemy has a
TypeandVariantthat correspond to known bosses. Alternatively, some APIs provide a method likeentity:IsBoss()or a flag indicating boss status. -
Add Toggle Functionality with the
HKey:Implementing a toggle requires tracking the state of health bar visibility and responding to key presses. You'll need to:
- Register the
Hkey as a binding. - Monitor key presses to toggle the visibility state.
- Use this state to conditionally render health bars.
- Register the
-
Implement the Changes in Code:
Below is the revised code incorporating both features:
local mod = RegisterMod("Enemy Health Bar", 1) local game = Game() local font = Font() font:Load("font/terminus.fnt") -- Ensure this font exists in your game files -- Variable to track health bar visibility local showHealthBars = true -- List of boss entity types and variants -- You need to populate this list based on the game's boss definitions -- For example: local bossEntities = { { Type = EntityType.ENTITY_BOSS1, Variant = 0 }, { Type = EntityType.ENTITY_BOSS2, Variant = 0 }, -- Add more bosses as needed } -- Helper function to check if an entity is a boss local function isBoss(entity) for _, boss in ipairs(bossEntities) do if entity.Type == boss.Type and entity.Variant == boss.Variant then return true end end return false end -- Callback to handle key presses function mod:OnKeyPressed(_, key) if key == Keyboard.KEY_H then showHealthBars = not showHealthBars end end function mod:PostRender() if not showHealthBars then return end -- Don't render if health bars are hidden local room = game:GetRoom() local entities = Isaac.GetRoomEntities() for _, entity in ipairs(entities) do if entity:IsActiveEnemy() and not entity:IsDead() and not isBoss(entity) then local maxHP = entity.MaxHitPoints local currentHP = entity.HitPoints if maxHP > 0 then local percentage = math.max(0, currentHP / maxHP) local pos = Isaac.WorldToScreen(entity.Position + Vector(0, -40)) -- Create health bar string local barLength = 20 -- Increased length for better visibility local filledLength = math.floor(barLength * percentage + 0.5) local emptyLength = barLength - filledLength local barString = "[" .. string.rep("=", filledLength) .. string.rep(" ", emptyLength) .. "]" -- Define color based on health percentage local color if percentage > 0.5 then color = KColor(0, 1, 0, 1) -- Green elseif percentage > 0.25 then color = KColor(1, 1, 0, 1) -- Yellow else color = KColor(1, 0, 0, 1) -- Red end -- Draw health bar font:DrawString(barString, pos.X - (barLength * 3), pos.Y, color, 0, false) end end end end mod:AddCallback(ModCallbacks.MC_POST_RENDER, mod.PostRender) mod:AddCallback(ModCallbacks.MC_INPUT_ACTION, mod.OnKeyPressed)Detailed Explanation:
-
Tracking Health Bar Visibility:
local showHealthBars = trueThis boolean variable keeps track of whether health bars should be displayed. It starts as
true, meaning health bars are visible by default. -
Defining Boss Entities:
local bossEntities = { { Type = EntityType.ENTITY_BOSS1, Variant = 0 }, { Type = EntityType.ENTITY_BOSS2, Variant = 0 }, -- Add more bosses as needed }Replace
ENTITY_BOSS1,ENTITY_BOSS2, etc., with the actualTypeandVariantidentifiers for bosses in your game. This list allows the script to identify and exclude boss enemies from having health bars. -
Helper Function to Identify Bosses:
local function isBoss(entity) for _, boss in ipairs(bossEntities) do if entity.Type == boss.Type and entity.Variant == boss.Variant then return true end end return false endThis function checks if a given entity matches any of the defined boss types and variants.
-
Handling Key Presses:
function mod:OnKeyPressed(_, key) if key == Keyboard.KEY_H then showHealthBars = not showHealthBars end endThis function toggles the
showHealthBarsvariable each time theHkey is pressed. -
Rendering Health Bars Conditionally:
function mod:PostRender() if not showHealthBars then return end -- Don't render if health bars are hidden -- Existing rendering logic... endBefore proceeding with rendering, the function checks if
showHealthBarsistrue. If not, it exits early, preventing any health bars from being drawn. -
Excluding Bosses from Rendering:
if entity:IsActiveEnemy() and not entity:IsDead() and not isBoss(entity) then -- Render health bar endThe
not isBoss(entity)condition ensures that boss enemies are excluded from having health bars rendered. -
Improving the Health Bar Appearance:
- Bar Length: Increased from
5to20for better visibility. - Color Coding: Health bars change color based on the enemy's remaining health (Green >50%, Yellow 25-50%, Red <25%).
- Bar Length: Increased from
-
Registering the Key Press Callback:
mod:AddCallback(ModCallbacks.MC_INPUT_ACTION, mod.OnKeyPressed)This line registers the
OnKeyPressedfunction to listen for input actions, specifically theHkey press.
Additional Considerations:
-
Identifying Bosses:
Ensure that the
bossEntitiestable accurately reflects all boss types and variants in your game. If the game provides a method likeentity:IsBoss(), you can replace the customisBossfunction with that method for simplicity and reliability. -
Font and Visibility:
Adjust the
barLength, positioning, and colors as needed to fit the aesthetic of your game and ensure that health bars are clearly visible without obstructing gameplay. -
Performance Optimization:
If you notice performance issues with rendering many health bars, consider optimizing the rendering logic, such as limiting the number of health bars drawn based on the camera view or entity distance.
Testing the Mod:
-
Load the Mod:
Ensure that the mod is correctly installed and that the font file (
terminus.fnt) exists in the specified path. -
Identify Bosses:
Verify that boss enemies do not display health bars while regular enemies do.
-
Toggle Functionality:
Press the
Hkey during gameplay to toggle the visibility of all health bars. Confirm that pressingHhides all health bars and pressing it again makes them visible. -
Edge Cases:
Test scenarios with multiple enemies, including bosses, to ensure that health bars behave as expected.
By following the above implementation, your mod will now exclude boss enemies from displaying health bars and will allow players to toggle the visibility of health bars using the
Hkey. -