minetest_hbarmor/init.lua

101 lines
2.5 KiB
Lua
Raw Normal View History

2013-06-28 22:58:06 +02:00
hud = {}
-- HUD statbar values
hud.armor = {}
2015-02-24 05:30:08 +01:00
-- Stores if player's HUD bar has been initialized so far.
hud.player_active = {}
-- HUD item ids
local armor_hud = {}
2013-06-28 22:58:06 +02:00
HUD_TICK = 0.1
--load custom settings
2015-02-24 01:55:07 +01:00
local set = io.open(minetest.get_modpath("hbarmor").."/hud.conf", "r")
2013-07-20 22:07:24 +02:00
if set then
2015-02-24 01:55:07 +01:00
dofile(minetest.get_modpath("hbarmor").."/hud.conf")
2013-07-20 22:07:24 +02:00
set:close()
end
2013-06-28 22:58:06 +02:00
2015-02-24 04:49:32 +01:00
local must_hide = function(playername, arm)
return ((not armor.def[playername].count or armor.def[playername].count == 0) and arm == 0)
end
local arm_printable = function(arm)
return math.ceil(math.floor(arm+0.5))
end
local function custom_hud(player)
2015-02-24 02:54:55 +01:00
local name = player:get_player_name()
2013-07-20 14:02:30 +02:00
2015-02-24 02:54:55 +01:00
if minetest.setting_getbool("enable_damage") then
2015-02-24 04:49:32 +01:00
local arm = tonumber(hud.armor[name])
if not arm then arm = 0 end
local hide = must_hide(name, arm)
hb.init_hudbar(player, "armor", arm_printable(arm), nil, nil, hide)
2015-02-24 02:54:55 +01:00
end
2013-06-28 22:58:06 +02:00
end
2015-02-24 02:02:45 +01:00
--register and define armor HUD bar
2015-02-24 02:52:26 +01:00
hb.register_hudbar("armor", 0xFFFFFF, "Armor", { icon = "hbarmor_icon.png", bar = "hbarmor_bar.png" }, 0, 100, true, "%s: %d%%")
2015-02-24 02:02:45 +01:00
--needs to be defined for older version of 3darmor
function hud.set_armor()
end
2015-02-24 01:55:07 +01:00
dofile(minetest.get_modpath("hbarmor").."/armor.lua")
2015-02-24 04:49:32 +01:00
-- update hud elemtens if value has changed
2013-06-28 22:58:06 +02:00
local function update_hud(player)
local name = player:get_player_name()
--armor
local arm = tonumber(hud.armor[name])
2015-02-24 05:30:08 +01:00
if not arm then
arm = 0
hud.armor[name] = 0
end
2015-02-24 04:51:48 +01:00
-- hide armor bar completely when there is none
if must_hide(name, arm) then
hb.hide_hudbar(player, "armor")
else
2015-02-24 05:30:08 +01:00
hb.change_hudbar(player, "armor", arm_printable(arm))
2015-02-24 04:51:48 +01:00
hb.unhide_hudbar(player, "armor")
end
2013-06-28 22:58:06 +02:00
end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
hud.armor[name] = 0
2015-02-24 01:55:07 +01:00
custom_hud(player)
2015-02-24 05:30:08 +01:00
hud.player_active[name] = true
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
hud.player_active[name] = false
2013-06-28 22:58:06 +02:00
end)
2014-04-14 06:51:28 +02:00
local main_timer = 0
2013-06-28 22:58:06 +02:00
local timer = 0
2015-02-24 04:49:32 +01:00
minetest.register_globalstep(function(dtime)
2015-02-24 02:54:55 +01:00
main_timer = main_timer + dtime
timer = timer + dtime
2015-02-24 04:49:32 +01:00
if main_timer > HUD_TICK or timer > 4 then
2015-02-24 05:37:11 +01:00
if minetest.setting_getbool("enable_damage") then
if main_timer > HUD_TICK then main_timer = 0 end
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
2015-02-24 05:30:08 +01:00
if hud.player_active[name] == true then
hud.get_armor(player)
2015-02-24 04:49:32 +01:00
2015-02-24 05:30:08 +01:00
-- update all hud elements
update_hud(player)
end
2013-06-28 22:58:06 +02:00
end
2014-04-14 06:51:28 +02:00
end
2015-02-24 04:49:32 +01:00
end
if timer > 4 then timer = 0 end
2013-06-28 22:58:06 +02:00
end)