Here's the updated code that adds floating damage numbers to your existing enemy health bar mod while keeping the health bar functionality intact:
local mod = RegisterMod("Enemy Health Bar with Damage Numbers", 1)
local game = Game()
local font = Font()
font:Load("font/terminus.fnt") -- Ensure this font exists in your game files
function mod:PostRender()
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
-- Existing code for health bar
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
-- Now handle floating damage numbers
local data = entity:GetData()
if data.damageNumbers then
for i = #data.damageNumbers, 1, -1 do
local dmgNum = data.damageNumbers[i]
dmgNum.time = dmgNum.time + 1 -- Increment time
local duration = 30 -- Frames to display the number
-- Update position (move up)
local offsetY = -40 - (dmgNum.time * 0.5)
local pos = Isaac.WorldToScreen(entity.Position + Vector(0, offsetY))
-- Fade out
local alpha = 1 - (dmgNum.time / duration)
if alpha < 0 then alpha = 0 end
-- Draw damage number
font:DrawString(tostring(math.floor(dmgNum.amount)), pos.X, pos.Y, KColor(1, 1, 1, alpha), 0, false)
-- Remove after duration
if dmgNum.time >= duration then
table.remove(data.damageNumbers, i)
end
end
end
end
end
end
function mod:EntityTakeDamage(entity, amount, flags, source, countdownFrames)
if entity:IsActiveEnemy() and not entity:IsDead() and amount > 0 then
local data = entity:GetData()
if not data.damageNumbers then data.damageNumbers = {} end
-- Create a new damage number entry
table.insert(data.damageNumbers, {
amount = amount,
position = entity.Position, -- Starting position
time = 0 -- Time since damage was taken
})
end
end
mod:AddCallback(ModCallbacks.MC_POST_RENDER, mod.PostRender)
mod:AddCallback(ModCallbacks.MC_ENTITY_TAKE_DMG, mod.EntityTakeDamage)
Explanation:
-
Entity Damage Tracking:
- Added a new function
mod:EntityTakeDamage that is called whenever an enemy takes damage.
- This function checks if the damage amount is greater than 0 and that the entity is a valid enemy.
- It then stores the damage amount along with the current position and initializes a timer (
time) to track how long the damage number has been displayed.
-
Displaying Damage Numbers:
- In the
mod:PostRender function, after drawing the health bar, the code now checks for any damage numbers associated with the enemy.
- It iterates over these damage numbers, updates their display time, and calculates their new position (moving upwards).
- The damage numbers fade out over a duration of 30 frames, controlled by adjusting the alpha value.
- Once a damage number has been displayed for the full duration, it is removed from the list.
-
Visual Effects:
- Damage numbers move upwards by decreasing the
offsetY over time.
- They fade out smoothly by decreasing the
alpha value.
- Damage numbers are drawn using the same font, ensuring consistency with the health bar.
-
Maintaining Health Bar Functionality:
- The existing health bar code remains unchanged, ensuring that both the health bar and damage numbers are displayed together without interference.
Usage:
- Copy the above code and replace your existing mod code with it.
- Ensure that the font file
"font/terminus.fnt" exists in your game files.
- The mod will now display floating damage numbers above enemies when they take damage, alongside the health bar.
Note:
- The
amount parameter in the EntityTakeDamage function represents the amount of damage actually taken by the enemy, considering any resistances or modifiers.
- The damage numbers will display even if the enemy takes multiple hits in quick succession, as each damage instance is tracked separately.
- Adjust the
duration variable if you want the damage numbers to display for a longer or shorter time.
- You can customize the appearance (such as color, font, movement speed) by modifying the corresponding variables and parameters in the code.