Do not set fire to objects that are already burning.

Changes to mcl_burning.set_on_fire():
* Add logic that only updates the burn time, but skips adding fire
  entities to objects that are already burning.
* Condense code a little, remove single use variables.
* Add a comment to a questionable piece of code.
* Add comments to the function.
This commit is contained in:
kabou 2022-03-02 16:10:55 +01:00
parent b17776699e
commit e80006f4ea
1 changed files with 39 additions and 28 deletions

View File

@ -58,13 +58,24 @@ function mcl_burning.update_hud(player)
end end
end end
-- Sets and object state as burning and adds a fire animation to the object.
--
-- Parameters:
-- obj - may be a player or a lua_entity;
-- burn_time - sets the object's burn duration;
--
-- If obj is a player, adds a fire animation to the HUD, if obj is a
-- lua_entity, adds an animated fire entity to obj.
-- The effective burn duration is modified by obj's armor protection.
-- If obj was already burning, its burn duration is updated if the current
-- duration is less than burn_time.
-- If obj is dead, fireproof or a creative player, this function does nothing.
--
function mcl_burning.set_on_fire(obj, burn_time) function mcl_burning.set_on_fire(obj, burn_time)
if obj:get_hp() < 0 then if obj:get_hp() < 0 then
return return
end end
local storage = mcl_burning.get_storage(obj)
local luaentity = obj:get_luaentity() local luaentity = obj:get_luaentity()
if luaentity and luaentity.fire_resistant then if luaentity and luaentity.fire_resistant then
return return
@ -85,42 +96,42 @@ function mcl_burning.set_on_fire(obj, burn_time)
end end
end end
end end
if max_fire_prot_lvl > 0 then if max_fire_prot_lvl > 0 then
burn_time = burn_time - math.floor(burn_time * max_fire_prot_lvl * 0.15) burn_time = burn_time - math.floor(burn_time * max_fire_prot_lvl * 0.15)
end end
end end
if not storage.burn_time or burn_time >= storage.burn_time then local storage = mcl_burning.get_storage(obj)
if obj:is_player() then if storage.burn_time then
mcl_burning.update_hud(obj) if burn_time > storage.burn_time then
storage.burn_time = burn_time
end end
storage.burn_time = burn_time return
storage.fire_damage_timer = 0 end
storage.burn_time = burn_time
storage.fire_damage_timer = 0
local fire_entity = minetest.add_entity(obj:get_pos(), "mcl_burning:fire") local minp, maxp = mcl_burning.get_collisionbox(obj, false, storage)
local minp, maxp = mcl_burning.get_collisionbox(obj, false, storage) local size = vector.subtract(maxp, minp)
local obj_size = obj:get_properties().visual_size size = vector.multiply(size, vector.new(1.1, 1.2, 1.1))
size = vector.divide(size, obj:get_properties().visual_size)
local vertical_grow_factor = 1.2 local fire_entity = minetest.add_entity(obj:get_pos(), "mcl_burning:fire")
local horizontal_grow_factor = 1.1 fire_entity:set_properties({visual_size = size})
local grow_vector = vector.new(horizontal_grow_factor, vertical_grow_factor, horizontal_grow_factor) fire_entity:set_attach(obj, "", vector.new(0, size.y * 5, 0), vector.new(0, 0, 0))
local size = vector.subtract(maxp, minp) if obj:is_player() then
size = vector.multiply(size, grow_vector) mcl_burning.update_hud(obj)
size = vector.divide(size, obj_size) end
local offset = vector.new(0, size.y * 10 / 2, 0)
fire_entity:set_properties({visual_size = size}) -- FIXME: does this code make sense? It removes attached fire luaentities from
fire_entity:set_attach(obj, "", offset, {x = 0, y = 0, z = 0}) -- another object that happen to be at the same position.
local fire_luaentity = fire_entity:get_luaentity() local fire_luaentity = fire_entity:get_luaentity()
for _, other in pairs(minetest.get_objects_inside_radius(fire_entity:get_pos(), 0)) do
for _, other in pairs(minetest.get_objects_inside_radius(fire_entity:get_pos(), 0)) do local other_luaentity = other:get_luaentity()
local other_luaentity = other:get_luaentity() if other_luaentity and other_luaentity.name == "mcl_burning:fire" and other_luaentity ~= fire_luaentity then
if other_luaentity and other_luaentity.name == "mcl_burning:fire" and other_luaentity ~= fire_luaentity then other:remove()
other:remove() break
break
end
end end
end end
end end