i3/src/hud.lua

148 lines
3.6 KiB
Lua
Raw Normal View History

2023-04-02 01:23:11 +02:00
IMPORT("max", "ceil", "remove", "str_to_pos")
IMPORT("get_connected_players", "add_hud_waypoint")
2021-11-26 03:32:04 +01:00
local function init_hud(player)
local name = player:get_player_name()
local data = i3.data[name]
2022-09-14 15:30:31 +02:00
local wdesc_y = -90
if core.global_exists"hb" then
wdesc_y -= ceil(hb.hudbars_count / 2) * 5
elseif not i3.settings.damage_enabled then
wdesc_y += 15
end
2021-11-26 03:32:04 +01:00
data.hud = {
notifs = {},
wielditem = player:hud_add {
hud_elem_type = "text",
position = {x = 0.5, y = 1},
2022-09-14 15:30:31 +02:00
offset = {x = 0, y = wdesc_y},
alignment = {x = 0, y = -1},
number = 0xffffff,
text = "",
z_index = 0xDEAD,
style = 1,
}
2021-11-26 03:32:04 +01:00
}
end
2023-04-02 00:45:39 +02:00
local function get_progress(offset, max_val)
local progress = offset * (1 / (max_val - 5))
2023-04-01 22:45:18 +02:00
return 1 - (progress ^ 4)
end
local function show_hud(player, data, notif, idx, dt)
local hud_info_bg = player:hud_get(notif.elems.bg)
2023-04-01 22:45:18 +02:00
local offset = hud_info_bg.offset
2023-04-01 18:13:21 +02:00
2023-04-01 22:45:18 +02:00
if offset.y < notif.max.y then
notif.show = false
2023-04-02 00:45:39 +02:00
notif.hud_timer += dt
2021-11-26 03:32:04 +01:00
end
player:hud_change(notif.elems.text, "text", notif.hud_msg)
2021-11-26 03:32:04 +01:00
if notif.hud_img then
player:hud_change(notif.elems.img, "text", notif.hud_img)
2021-11-26 03:32:04 +01:00
end
if notif.show then
2023-04-01 22:45:18 +02:00
local speed = i3.settings.hud_speed * (100 * get_progress(offset.y, notif.max.y)) * dt
for _, def in pairs(notif.elems) do
local hud_info = player:hud_get(def)
2021-11-26 03:32:04 +01:00
player:hud_change(def, "offset", {
x = hud_info.offset.x,
2023-04-02 01:23:11 +02:00
y = hud_info.offset.y - (speed * max(1, (#data.hud.notifs - idx + 1) / 1.45))
})
2021-11-26 03:32:04 +01:00
end
2023-04-01 22:45:18 +02:00
elseif notif.show == false and notif.hud_timer >= i3.settings.hud_timer_max then
local speed = (i3.settings.hud_speed * 2.6) * (100 * get_progress(offset.x, notif.max.x)) * dt
for _, def in pairs(notif.elems) do
local hud_info = player:hud_get(def)
player:hud_change(def, "offset", {
2023-04-02 01:23:11 +02:00
x = hud_info.offset.x - speed,
2023-04-01 22:45:18 +02:00
y = hud_info.offset.y
})
2023-04-02 01:23:11 +02:00
if hud_info.offset.x < notif.max.x then
player:hud_remove(def)
remove(data.hud.notifs, idx)
2021-11-26 03:32:04 +01:00
end
end
end
end
core.register_globalstep(function(dt)
local players = get_connected_players()
players[0] = #players
for i = 1, players[0] do
local player = players[i]
local name = player:get_player_name()
local data = i3.data[name]
if not data then return end
for idx, notif in ipairs(data.hud.notifs) do
if notif.show ~= nil then
show_hud(player, data, notif, idx, dt)
end
2023-03-05 13:57:24 +01:00
end
2023-01-29 21:44:53 +01:00
local has_text = player:hud_get(data.hud.wielditem).text ~= ""
if not data.wielditem_hud then
2023-01-29 21:44:53 +01:00
if has_text then
player:hud_change(data.hud.wielditem, "text", "")
end
2022-08-29 11:28:46 +02:00
return
end
data.timer = (data.timer or 0) + dt
2022-08-28 14:45:37 +02:00
local wieldidx = player:get_wield_index()
2022-08-28 14:45:37 +02:00
if wieldidx == data.old_wieldidx then
2023-01-29 21:44:53 +01:00
if data.timer >= i3.settings.wielditem_fade_after and has_text then
2022-08-29 11:28:46 +02:00
player:hud_change(data.hud.wielditem, "text", "")
end
return
end
2022-08-29 11:28:46 +02:00
data.timer = 0
2022-08-28 14:45:37 +02:00
data.old_wieldidx = wieldidx
2022-08-28 14:45:37 +02:00
local wielditem = player:get_wielded_item()
local meta = wielditem:get_meta()
2022-08-28 14:45:37 +02:00
local meta_desc = meta:get_string"short_description"
meta_desc = meta_desc:gsub("\27", "")
meta_desc = core.strip_colors(meta_desc)
local desc = meta_desc ~= "" and meta_desc or wielditem:get_short_description()
player:hud_change(data.hud.wielditem, "text", desc:trim())
end
end)
2021-11-26 04:58:57 +01:00
local function init_waypoints(player)
local name = player:get_player_name()
local data = i3.data[name]
data.waypoints = data.waypoints or {}
for _, v in ipairs(data.waypoints) do
if not v.hide then
2023-01-07 12:28:07 +01:00
local id = add_hud_waypoint(player, v.name, str_to_pos(v.pos), v.color, v.image)
2021-11-26 04:58:57 +01:00
v.id = id
end
end
end
return function(player)
init_hud(player)
init_waypoints(player)
end