Change global variable names
[minetest_hbarmor.git] / init.lua
blob976196857c4eec88e53d13b3b0cc808a2fde5465
1 hbarmor = {}
3 -- HUD statbar values
4 hbarmor.armor = {}
6 -- Stores if player's HUD bar has been initialized so far.
7 hbarmor.player_active = {}
9 -- HUD item ids
10 local armor_hud = {}
12 hbarmor.tick = 0.1
14 --load custom settings
15 local set = io.open(minetest.get_modpath("hbarmor").."/hbarmor.conf", "r")
16 if set then
17 dofile(minetest.get_modpath("hbarmor").."/hbarmor.conf")
18 set:close()
19 end
21 local must_hide = function(playername, arm)
22 return ((not armor.def[playername].count or armor.def[playername].count == 0) and arm == 0)
23 end
25 local arm_printable = function(arm)
26 return math.ceil(math.floor(arm+0.5))
27 end
29 local function custom_hud(player)
30 local name = player:get_player_name()
32 if minetest.setting_getbool("enable_damage") then
33 local arm = tonumber(hbarmor.armor[name])
34 if not arm then arm = 0 end
35 local hide = must_hide(name, arm)
36 hb.init_hudbar(player, "armor", arm_printable(arm), nil, hide)
37 end
38 end
40 --register and define armor HUD bar
41 hb.register_hudbar("armor", 0xFFFFFF, "Armor", { icon = "hbarmor_icon.png", bar = "hbarmor_bar.png" }, 0, 100, true, "%s: %d%%")
43 dofile(minetest.get_modpath("hbarmor").."/armor.lua")
46 -- update hud elemtens if value has changed
47 local function update_hud(player)
48 local name = player:get_player_name()
49 --armor
50 local arm = tonumber(hbarmor.armor[name])
51 if not arm then
52 arm = 0
53 hbarmor.armor[name] = 0
54 end
55 -- hide armor bar completely when there is none
56 if must_hide(name, arm) then
57 hb.hide_hudbar(player, "armor")
58 else
59 hb.change_hudbar(player, "armor", arm_printable(arm))
60 hb.unhide_hudbar(player, "armor")
61 end
62 end
64 minetest.register_on_joinplayer(function(player)
65 local name = player:get_player_name()
66 hbarmor.armor[name] = 0
67 custom_hud(player)
68 hbarmor.player_active[name] = true
69 end)
71 minetest.register_on_leaveplayer(function(player)
72 local name = player:get_player_name()
73 hbarmor.player_active[name] = false
74 end)
76 local main_timer = 0
77 local timer = 0
78 minetest.register_globalstep(function(dtime)
79 main_timer = main_timer + dtime
80 timer = timer + dtime
81 if main_timer > hbarmor.tick or timer > 4 then
82 if minetest.setting_getbool("enable_damage") then
83 if main_timer > hbarmor.tick then main_timer = 0 end
84 for _,player in ipairs(minetest.get_connected_players()) do
85 local name = player:get_player_name()
86 if hbarmor.player_active[name] == true then
87 hbarmor.get_armor(player)
89 -- update all hud elements
90 update_hud(player)
91 end
92 end
93 end
94 end
95 if timer > 4 then timer = 0 end
96 end)