123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- hud.registered_items = {}
- hud.damage_events = {}
- hud.breath_events = {}
- local math_floor = math.floor
- hud.all_hud_ids = {}
- hud.all_sb_bgs = {}
- local hud_id = hud.all_hud_ids
- local sb_bg = hud.all_sb_bgs
- local items = hud.registered_items
- local function throw_error(msg)
- minetest.log("error", "Better HUD[error]: " .. msg)
- end
- function hud.register(name, def)
- if not name or not def then
- throw_error("Not enough parameters given")
- return false
- end
-
- if def.hud_elem_type ~= "statbar" then
- throw_error("The given HUD element is not a statbar")
- return false
- end
- if items[name] ~= nil then
- throw_error("A statbar with that name already exists")
- return false
- end
-
-
- if def.hud_elem_type == "statbar" and def.background ~= nil then
- sb_bg[name] = table.copy(def)
- sb_bg[name].text = def.background
- sb_bg[name].number = 20
- end
-
- items[name] = def
-
- if def.events then
- for _, v in pairs(def.events) do
- if v and v.type and v.func then
- if v.type == "damage" then
- table.insert(hud.damage_events, v)
- end
- if v.type == "breath" then
- table.insert(hud.breath_events, v)
- end
- end
- end
- end
-
-
- return true
- end
- function hud.change_item(player, name, def)
- if not player or not player:is_player() or not name or not def then
- throw_error("Not enough parameters given to change HUD item")
- return false
- end
- local i_name = player:get_player_name().."_"..name
- local elem = hud_id[i_name]
- if not elem then
-
- return false
- end
-
- if def.number and elem.number then
- elem.number = math_floor((def.number / def.max) * 20)
- player:hud_change(elem.id, "number", elem.number)
- end
- if def.text and elem.text then
- player:hud_change(elem.id, "text", def.text)
- elem.text = def.text
- end
- if def.offset and elem.offset then
- player:hud_change(elem.id, "offset", def.offset)
- elem.offset = def.offset
- end
- return true
- end
- function hud.remove_item(player, name)
- if not player or not name then
- throw_error("Not enough parameters given")
- return false
- end
- local i_name = player:get_player_name() .. "_" .. name
- if hud_id[i_name] == nil then
-
- return false
- end
- player:hud_remove(hud_id[i_name].id)
- hud_id[i_name] = nil
- return true
- end
- local function add_hud_item(player, name, def)
- if not player or not name or not def then
- throw_error("not enough parameters given")
- return false
- end
-
-
-
-
- local i_name = player:get_player_name() .. "_" .. name
- hud_id[i_name] = table.copy(def)
- hud_id[i_name].id = player:hud_add(def)
- end
- minetest.register_on_joinplayer(function(player)
-
- local hud_flags = player:hud_get_flags()
- hud_flags.healthbar = false
- hud_flags.breathbar = false
- player:hud_set_flags(hud_flags)
-
- for _, item in pairs(sb_bg) do
- add_hud_item(player, _ .. "_bg", item)
- end
-
- for _, item in pairs(items) do
- add_hud_item(player, _, item)
- end
- end)
|