1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/functions/boss_unit.lua
2019-09-08 07:21:19 +02:00

51 lines
1.8 KiB
Lua

--adds a health bar and health increase to a unit
local function create_healthbar(entity, size)
return rendering.draw_sprite({
sprite="virtual-signal/signal-white",
tint={0, 200, 0},
x_scale=size * 15, y_scale=size, render_layer="light-effect",
target=entity, target_offset={0, -2.5}, surface=entity.surface,
})
end
local function set_healthbar(boss_unit)
local m = boss_unit.health / boss_unit.max_health
local x_scale = rendering.get_y_scale(boss_unit.healthbar_id) * 15
rendering.set_x_scale(boss_unit.healthbar_id, x_scale * m)
rendering.set_color(boss_unit.healthbar_id, {math.floor(255 - 255 * m), math.floor(200 * m), 0})
end
function add_boss_unit(entity, health_factor, size)
if not entity then return end
if not entity.unit_number then return end
if not health_factor then return end
local health = math.floor(entity.prototype.max_health * health_factor)
if health == 0 then return end
local s = 0.5
if size then s = size end
global.boss_units[entity.unit_number] = {entity = entity, max_health = health, health = health, healthbar_id = create_healthbar(entity, s)}
end
local function on_entity_damaged(event)
local entity = event.entity
if not entity.valid then return end
if entity.type ~= "unit" then return end
if not global.boss_units[entity.unit_number] then return end
entity.health = entity.health + event.final_damage_amount
global.boss_units[entity.unit_number].health = global.boss_units[entity.unit_number].health - event.final_damage_amount
if global.boss_units[entity.unit_number].health <= 0 then
global.boss_units[entity.unit_number] = nil
entity.die()
else
set_healthbar(global.boss_units[entity.unit_number])
end
end
local function on_init()
global.boss_units = {}
end
local event = require 'utils.event'
event.on_init(on_init)
event.add(defines.events.on_entity_damaged, on_entity_damaged)