Made fovapi registration more robust

This commit is contained in:
the-real-herowl 2023-12-09 23:22:36 +01:00 committed by the-real-herowl
parent 580a1caa38
commit a650f8b368

View File

@ -35,28 +35,42 @@ minetest.register_on_leaveplayer(function(player)
mcl_fovapi.applied_modifiers[name] = nil mcl_fovapi.applied_modifiers[name] = nil
end) end)
function mcl_fovapi.register_modifier(name, fov_factor, time, is_multiplier, exclusive, on_start, on_end) function mcl_fovapi.register_modifier(def)
if is_multiplier ~= true and is_multiplier ~= false then if type(def.name) ~= "string" then
is_multiplier = true error("Modifier name must be a string")
end end
if exclusive ~= true and exclusive ~= false then if type(def.fov_factor) ~= "number" then
exclusive = false error("FOV factor must be a number")
end end
local def = { if type(def.time) ~= "number" then
modifer_name = name, error("Transition time must be a number")
fov_factor = fov_factor, end
time = time,
is_multiplier = is_multiplier, if def.on_start ~= nil and type(def.on_start) ~= "function" then
exclusive = exclusive, error("Callback on_start must be a function")
on_start = on_start, end
on_end = on_end, if def.on_end ~= nil and type(def.on_end) ~= "function" then
} error("Callback on_end must be a function")
end
local mdef = {}
mdef.fov_factor = def.fov_factor
mdef.time = def.time
if def.is_multiplier == false then mdef.is_multiplier = false
else mdef.is_multiplier = true end
if def.exclusive == true then mdef.exclusive = true
else mdef.exclusive = false end
mdef.on_start = def.on_start
mdef.on_end = def.on_end
if DEBUG then if DEBUG then
minetest.log("FOV::Modifier Definition Registered:\n" .. dump(def)) minetest.log("FOV::Modifier Definition Registered:\n" .. dump(def))
end end
mcl_fovapi.registered_modifiers[name] = def mcl_fovapi.registered_modifiers[def.name] = mdef
end end
@ -130,6 +144,7 @@ function mcl_fovapi.remove_modifier(player, modifier_name)
end end
mcl_fovapi.applied_modifiers[player][modifier_name] = nil mcl_fovapi.applied_modifiers[player][modifier_name] = nil
local modifier = mcl_fovapi.registered_modifiers[modifier_name]
-- check for other fov modifiers, and set them up, or reset to default. -- check for other fov modifiers, and set them up, or reset to default.
@ -139,6 +154,7 @@ function mcl_fovapi.remove_modifier(player, modifier_name)
end end
if #applied == 0 then if #applied == 0 then
player:set_fov(0, false, modifier.time)
return return
end end
local exc = false local exc = false
@ -154,10 +170,11 @@ function mcl_fovapi.remove_modifier(player, modifier_name)
player:set_fov(exc.fov_factor, exc.is_multiplier, 0) -- we want this to be immediate. player:set_fov(exc.fov_factor, exc.is_multiplier, 0) -- we want this to be immediate.
else else
-- handle normal fov modifiers. -- handle normal fov modifiers.
player:set_fov(0, false, 0) -- we want this to be immediate. local fov_factor = 1
for x in applied do for x in applied do
player:set_fov(x.fov_factor, true, 0) fov_factor = fov_factor * x.fov_factor
end end
player:set_fov(fov_factor, true, modifier.time)
end end
if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then