To make the damage numbers appear red when the damage amount is 7 or more, you need to adjust the color of the text based on the damage amount when drawing the damage numbers. Here's how you can modify your code to achieve this:
Modify the drawing of the damage numbers in the PostRender function:
Locate the section in your PostRender function where you draw the damage numbers. You'll want to add a condition that checks the damage amount and sets the color accordingly.
Updated PostRender function:
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
-- Determine color based on damage amount
local color
if dmgNum.amount >= 7 then
color = KColor(1, 0, 0, alpha) -- Red color for damage >= 7
else
color = KColor(1, 1, 1, alpha) -- White color for damage < 7
end
-- Draw damage number
font:DrawString(tostring(math.floor(dmgNum.amount)), pos.X, pos.Y, color, 0, false)
-- Remove after duration
if dmgNum.time >= duration then
table.remove(data.damageNumbers, i)
end
end
end
end
end
end
Explanation of the changes:
-
Determine the color based on damage amount:
-- Determine color based on damage amount
local color
if dmgNum.amount >= 7 then
color = KColor(1, 0, 0, alpha) -- Red color for damage >= 7
else
color = KColor(1, 1, 1, alpha) -- White color for damage < 7
end
This code checks if the amount of damage in dmgNum is greater than or equal to 7. If it is, it sets the color variable to red (KColor(1, 0, 0, alpha)). Otherwise, it sets it to white.
-
Use the determined color when drawing the damage number:
-- Draw damage number
font:DrawString(tostring(math.floor(dmgNum.amount)), pos.X, pos.Y, color, 0, false)
By using the color variable, the damage number will now be drawn in the appropriate color based on the damage amount.
No changes are needed in the EntityTakeDamage function since we are only altering how the damage numbers are displayed, not how they're recorded or stored.
Now, when an enemy takes damage:
- If the damage is 7 or more, the damage number will appear red.
- If the damage is less than 7, the damage number will remain white.
Full modified code snippet:
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
-- Determine color based on damage amount
local color
if dmgNum.amount >= 7 then
color = KColor(1, 0, 0, alpha) -- Red color for damage >= 7
else
color = KColor(1, 1, 1, alpha) -- White color for damage < 7
end
-- Draw damage number
font:DrawString(tostring(math.floor(dmgNum.amount)), pos.X, pos.Y, color, 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)
Remember to test your mod to ensure the changes work as expected. If you encounter any issues, double-check the code for typos and ensure that the damage amounts are being calculated correctly.