Display armor as percentage
[minetest_hbarmor.git] / init.lua
blob0f3c12421277d806f530c9bb10f272a02ec2147b
1 hud = {}
3 -- HUD statbar values
4 hud.armor = {}
5 hud.armor_out = {}
7 -- HUD item ids
8 local armor_hud = {}
9 local armor_hud_bg = {}
11 HUD_TICK = 0.1
13 --load custom settings
14 local set = io.open(minetest.get_modpath("hbarmor").."/hud.conf", "r")
15 if set then
16 dofile(minetest.get_modpath("hbarmor").."/hud.conf")
17 set:close()
18 end
20 local function custom_hud(player)
21 local name = player:get_player_name()
23 if minetest.setting_getbool("enable_damage") then
24 hb.init_hudbar(player, "armor")
25 end
26 end
28 --register and define armor HUD bar
29 hb.register_hudbar("armor", 0xFFFFFF, "Armor", { icon = "hbarmor_icon.png", bar = "hbarmor_bar.png" }, 0, 100, true, "%s: %d%%")
31 --needs to be defined for older version of 3darmor
32 function hud.set_armor()
33 end
36 dofile(minetest.get_modpath("hbarmor").."/armor.lua")
38 -- update hud elemtens if value has changed
39 local function update_hud(player)
40 local name = player:get_player_name()
41 --armor
42 local arm_out = tonumber(hud.armor_out[name])
43 if not arm_out then arm_out = 0 end
44 local arm = tonumber(hud.armor[name])
45 if not arm then arm = 0 end
46 if arm_out ~= arm then
47 hud.armor_out[name] = arm
48 local arm_displayed = math.ceil(math.floor(arm+0.5))
49 hb.change_hudbar(player, "armor", arm_displayed)
50 -- hide armor bar completely when there is none
51 if (not armor.def[name].count or armor.def[name].count == 0) and arm == 0 then
52 hb.hide_hudbar(player, "armor")
53 else
54 hb.unhide_hudbar(player, "armor")
55 end
56 end
57 end
59 minetest.register_on_joinplayer(function(player)
60 local name = player:get_player_name()
61 hud.armor[name] = 0
62 hud.armor_out[name] = 0
63 custom_hud(player)
64 end)
66 minetest.register_on_respawnplayer(function(player)
67 end)
69 local main_timer = 0
70 local timer = 0
71 minetest.after(2.5, function()
72 minetest.register_globalstep(function(dtime)
73 main_timer = main_timer + dtime
74 timer = timer + dtime
75 if main_timer > HUD_TICK or timer > 4 then
76 if main_timer > HUD_TICK then main_timer = 0 end
77 for _,player in ipairs(minetest.get_connected_players()) do
78 local name = player:get_player_name()
80 -- only proceed if damage is enabled
81 if minetest.setting_getbool("enable_damage") then
82 hud.get_armor(player)
84 -- update all hud elements
85 update_hud(player)
87 end
88 end
90 end
91 if timer > 4 then timer = 0 end
92 end)
93 end)