diff --git a/init.lua b/init.lua index 80f20b5..833a0c0 100644 --- a/init.lua +++ b/init.lua @@ -12,7 +12,10 @@ end -- as it's called on every step local vector_distance = vector.distance local min = math.min -local max_breath = minetest.PLAYER_MAX_BREATH_DEFAULT or 11 +local max = { + breath = 11, + hp = 20, +} local mt_5 = minetest.features.object_independent_selectionbox @@ -29,6 +32,14 @@ local function add_gauge(player) end end +-- credit: https://github.com/minetest/minetest/blob/6de8d77e17017cd5cc7b065d42566b6b1cd076cc/builtin/game/statbars.lua#L30-L37 +local function scaleToDefault(player, field) + -- Scale "hp" or "breath" to supported amount + local current = player["get_" .. field](player) + local max_display = math.max(player:get_properties()[field .. "_max"], current) + return current / max_display * max[field] +end + minetest.register_entity("gauges:hp_bar", { visual = "sprite", visual_size = {x=1, y=1/16, z=1}, @@ -50,12 +61,8 @@ minetest.register_entity("gauges:hp_bar", { return end - -- hp calc credit: https://github.com/minetest/minetest/blob/6de8d77e17017cd5cc7b065d42566b6b1cd076cc/builtin/game/statbars.lua#L30-L37 - local current = player:get_hp() - local max_display = math.max(20, math.max(player:get_properties().hp_max, current)) - local hp = current / max_display * 20 - - local breath = min(player:get_breath(), max_breath) + local hp = scaleToDefault(player, "hp") + local breath = scaleToDefault(player, "breath") if self.hp ~= hp or self.breath ~= breath then local health_t = "health_"..hp..".png"