Unified and refactored FOV API code

This commit is contained in:
the-real-herowl 2023-12-13 00:29:22 +01:00 committed by the-real-herowl
parent e312955a14
commit 8a5058e032
1 changed files with 34 additions and 37 deletions

View File

@ -15,11 +15,17 @@ mcl_fovapi = {}
mcl_fovapi.registered_modifiers = {} mcl_fovapi.registered_modifiers = {}
mcl_fovapi.applied_modifiers = {} mcl_fovapi.applied_modifiers = {}
minetest.register_on_joinplayer(function(player)
local player_name = player:get_player_name()
-- initialization
mcl_fovapi.applied_modifiers[player_name] = {}
end)
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name() local player_name = player:get_player_name()
-- handle clean up -- handle clean up
mcl_fovapi.applied_modifiers[name] = nil mcl_fovapi.applied_modifiers[player_name] = nil
end) end)
function mcl_fovapi.register_modifier(def) function mcl_fovapi.register_modifier(def)
@ -66,47 +72,38 @@ function mcl_fovapi.register_modifier(def)
end end
minetest.register_on_respawnplayer(function(player) minetest.register_on_respawnplayer(function(player)
mcl_fovapi.remove_all_modifiers(player:get_player_name()) mcl_fovapi.remove_all_modifiers(player)
end) end)
function mcl_fovapi.apply_modifier(player, modifier_name) function mcl_fovapi.apply_modifier(player, modifier_name)
if player == nil then if not player or not modifier_name then
return
end
if modifier_name == nil then
return return
end end
if mcl_fovapi.registered_modifiers[modifier_name] == nil then if mcl_fovapi.registered_modifiers[modifier_name] == nil then
return return
end end
if mcl_fovapi.applied_modifiers and mcl_fovapi.applied_modifiers[player] and mcl_fovapi.applied_modifiers[player][modifier_name] then local player_name = player:get_player_name()
if mcl_fovapi.applied_modifiers[player][modifier_name] and mcl_fovapi.applied_modifiers[player][modifier_name] == true then if mcl_fovapi.applied_modifiers and mcl_fovapi.applied_modifiers[player_name] and mcl_fovapi.applied_modifiers[player_name][modifier_name] then
return return
end
end end
if mcl_fovapi.applied_modifiers[player] == nil then for k, _ in pairs(mcl_fovapi.applied_modifiers[player_name]) do
mcl_fovapi.applied_modifiers[player] = {}
end
for k, _ in pairs(mcl_fovapi.applied_modifiers[player]) do
if mcl_fovapi.registered_modifiers[k].exclusive == true then return end if mcl_fovapi.registered_modifiers[k].exclusive == true then return end
end end
local modifier = mcl_fovapi.registered_modifiers[modifier_name] local modifier = mcl_fovapi.registered_modifiers[modifier_name]
if modifier.on_start ~= nil then if modifier.on_start then
modifier.on_start(player) modifier.on_start(player)
end end
mcl_fovapi.applied_modifiers[player][modifier_name] = true -- set the applied to be true. mcl_fovapi.applied_modifiers[player_name][modifier_name] = true -- set the applied to be true.
if DEBUG then if DEBUG then
minetest.log("FOV::Player Applied Modifiers :" .. dump(mcl_fovapi.applied_modifiers[player])) minetest.log("FOV::Player Applied Modifiers :" .. dump(mcl_fovapi.applied_modifiers[player_name]))
end end
local pname = player:get_player_name()
if DEBUG then if DEBUG then
minetest.log("FOV::Modifier applied to player:" .. pname .. " modifier: " .. modifier_name) minetest.log("FOV::Modifier applied to player:" .. player_name .. " modifier: " .. modifier_name)
end end
-- modifier apply code. -- modifier apply code.
@ -138,26 +135,27 @@ function mcl_fovapi.apply_modifier(player, modifier_name)
end end
function mcl_fovapi.remove_modifier(player, modifier_name) function mcl_fovapi.remove_modifier(player, modifier_name)
if player == nil then if not player or not modifier_name then
return return
end end
if mcl_fovapi.applied_modifiers[player][modifier_name] == nil then local player_name = player:get_player_name()
return if not mcl_fovapi.applied_modifiers[player_name]
or not mcl_fovapi.applied_modifiers[player_name][modifier_name] then
return
end end
if DEBUG then if DEBUG then
local name = player:get_player_name() minetest.log("FOV::Player: " .. player_name .. " modifier: " .. modifier_name .. "removed.")
minetest.log("FOV::Player: " .. name .. " modifier: " .. modifier_name .. "removed.")
end end
mcl_fovapi.applied_modifiers[player][modifier_name] = nil mcl_fovapi.applied_modifiers[player_name][modifier_name] = nil
local modifier = mcl_fovapi.registered_modifiers[modifier_name] 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.
local applied = {} local applied = {}
for k, _ in pairs(mcl_fovapi.applied_modifiers[player]) do for k, _ in pairs(mcl_fovapi.applied_modifiers[player_name]) do
applied[k] = mcl_fovapi.registered_modifiers[k] applied[k] = mcl_fovapi.registered_modifiers[k]
end end
@ -196,30 +194,29 @@ function mcl_fovapi.remove_modifier(player, modifier_name)
player:set_fov(fov_factor, not non_multiplier_added, modifier.reset_time) player:set_fov(fov_factor, not non_multiplier_added, modifier.reset_time)
end end
if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then if mcl_fovapi.registered_modifiers[modifier_name].on_end then
mcl_fovapi.registered_modifiers[modifier_name].on_end(player) mcl_fovapi.registered_modifiers[modifier_name].on_end(player)
end end
end end
function mcl_fovapi.remove_all_modifiers(player) function mcl_fovapi.remove_all_modifiers(player)
if player == nil then if not player then
return return
end end
local player_name = player:get_player_name()
if DEBUG then if DEBUG then
local name = player:get_player_name() minetest.log("FOV::Player: " .. player_name .. " modifiers have been reset.")
minetest.log("FOV::Player: " .. name .. " modifiers have been reset.")
end end
for x in mcl_fovapi.applied_modifiers[player] do for name, x in pairs(mcl_fovapi.applied_modifiers[player_name]) do
x = nil x = nil
if mcl_fovapi.registered_modifiers[name].on_end then
mcl_fovapi.registered_modifiers[name].on_end(player)
end
end end
player:set_fov(0, false, 0) player:set_fov(0, false, 0)
if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then
mcl_fovapi.registered_modifiers[modifier_name].on_end(player)
end
end end
--[[ --[[