1
0
Fork 0
MineClone2_For_PRs/mods/HUD/mcl_hbarmor/init.lua

129 lines
3.4 KiB
Lua
Raw Normal View History

2020-02-17 20:24:26 +01:00
local S = minetest.get_translator("mcl_hbarmor")
2017-01-16 16:09:07 +01:00
2020-02-17 20:24:26 +01:00
local mcl_hbarmor = {}
2017-01-16 16:09:07 +01:00
-- HUD statbar values
2020-02-17 20:24:26 +01:00
mcl_hbarmor.armor = {}
2017-01-16 16:09:07 +01:00
-- Stores if player's HUD bar has been initialized so far.
2020-02-17 20:24:26 +01:00
mcl_hbarmor.player_active = {}
2017-01-16 16:09:07 +01:00
-- Time difference in seconds between updates to the HUD armor bar.
-- Increase this number for slow servers.
2020-02-17 20:24:26 +01:00
mcl_hbarmor.tick = 0.1
2017-01-16 16:09:07 +01:00
-- If true, the armor bar is hidden when the player does not wear any armor
2020-02-17 20:24:26 +01:00
mcl_hbarmor.autohide = true
2017-01-16 16:09:07 +01:00
2020-02-17 20:24:26 +01:00
set = minetest.settings:get("mcl_hbarmor_tick")
2017-01-16 16:09:07 +01:00
if tonumber(set) ~= nil then
2020-02-17 20:24:26 +01:00
mcl_hbarmor.tick = tonumber(set)
2017-01-16 16:09:07 +01:00
end
local must_hide = function(playername, arm)
2020-02-18 20:10:35 +01:00
return arm == 0
2017-01-16 16:09:07 +01:00
end
local arm_printable = function(arm)
return math.ceil(math.floor(arm+0.5))
end
local function custom_hud(player)
local name = player:get_player_name()
2017-08-18 21:35:02 +02:00
if minetest.settings:get_bool("enable_damage") then
2020-02-17 20:24:26 +01:00
local ret = mcl_hbarmor.get_armor(player)
2017-01-16 16:09:07 +01:00
if ret == false then
2020-02-17 20:24:26 +01:00
minetest.log("error", "[mcl_hbarmor] Call to mcl_hbarmor.get_armor in custom_hud returned with false!")
2020-02-17 20:19:07 +01:00
return
2017-01-16 16:09:07 +01:00
end
2020-02-17 20:24:26 +01:00
local arm = tonumber(mcl_hbarmor.armor[name])
2020-02-17 20:19:07 +01:00
if not arm then
arm = 0
end
2017-01-16 16:09:07 +01:00
local hide
2020-02-17 20:24:26 +01:00
if mcl_hbarmor.autohide then
2017-01-16 16:09:07 +01:00
hide = must_hide(name, arm)
else
hide = false
end
hb.init_hudbar(player, "armor", arm_printable(arm), nil, hide)
end
end
--register and define armor HUD bar
hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon = "hbarmor_icon.png", bgicon = "hbarmor_bgicon.png", bar = "hbarmor_bar.png" }, 0, 0, 20, mcl_hbarmor.autohide)
2017-01-16 16:09:07 +01:00
2020-02-17 20:24:26 +01:00
function mcl_hbarmor.get_armor(player)
2017-01-16 16:09:07 +01:00
local name = player:get_player_name()
2021-04-14 15:46:52 +02:00
local pts = player:get_meta():get_int("mcl_armor:armor_points")
2020-02-17 20:19:07 +01:00
if not pts then
2017-01-16 16:09:07 +01:00
return false
2020-02-17 20:19:07 +01:00
else
2020-02-17 20:24:26 +01:00
mcl_hbarmor.set_armor(name, pts)
2017-01-16 16:09:07 +01:00
end
return true
end
2020-02-17 20:24:26 +01:00
function mcl_hbarmor.set_armor(player_name, pts)
mcl_hbarmor.armor[player_name] = math.max(0, math.min(20, pts))
2017-01-16 16:09:07 +01:00
end
-- update hud elemtens if value has changed
local function update_hud(player)
local name = player:get_player_name()
--armor
2020-02-17 20:24:26 +01:00
local arm = tonumber(mcl_hbarmor.armor[name])
2017-01-16 16:09:07 +01:00
if not arm then
arm = 0
2020-02-17 20:24:26 +01:00
mcl_hbarmor.armor[name] = 0
2017-01-16 16:09:07 +01:00
end
2020-02-17 20:24:26 +01:00
if mcl_hbarmor.autohide then
2017-01-16 16:09:07 +01:00
-- hide armor bar completely when there is none
if must_hide(name, arm) then
hb.hide_hudbar(player, "armor")
else
hb.change_hudbar(player, "armor", arm_printable(arm))
hb.unhide_hudbar(player, "armor")
end
else
hb.change_hudbar(player, "armor", arm_printable(arm))
end
end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
custom_hud(player)
2020-02-17 20:24:26 +01:00
mcl_hbarmor.player_active[name] = true
2017-01-16 16:09:07 +01:00
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
2020-02-17 20:24:26 +01:00
mcl_hbarmor.player_active[name] = false
2017-01-16 16:09:07 +01:00
end)
local main_timer = 0
local timer = 0
minetest.register_globalstep(function(dtime)
main_timer = main_timer + dtime
timer = timer + dtime
2020-02-17 20:24:26 +01:00
if main_timer > mcl_hbarmor.tick or timer > 4 then
2017-08-18 21:35:02 +02:00
if minetest.settings:get_bool("enable_damage") then
2020-02-17 20:24:26 +01:00
if main_timer > mcl_hbarmor.tick then main_timer = 0 end
2021-03-16 17:35:46 +01:00
for _,player in pairs(minetest.get_connected_players()) do
2017-01-16 16:09:07 +01:00
local name = player:get_player_name()
2020-02-17 20:24:26 +01:00
if mcl_hbarmor.player_active[name] == true then
local ret = mcl_hbarmor.get_armor(player)
2017-01-16 16:09:07 +01:00
if ret == false then
2020-02-17 20:24:26 +01:00
minetest.log("error", "[mcl_hbarmor] Call to mcl_hbarmor.get_armor in globalstep returned with false!")
2017-01-16 16:09:07 +01:00
end
-- update all hud elements
update_hud(player)
end
end
end
end
if timer > 4 then timer = 0 end
end)