Update init.lua

This commit is contained in:
LoneWolfHT 2021-10-12 15:51:41 -07:00 committed by GitHub
parent bb270cd035
commit fe16736038
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 7 deletions

View File

@ -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"