noita_entangled_worlds/quant.ew/files/system/nickname.lua

141 lines
3.7 KiB
Lua
Raw Normal View History

2024-08-07 17:11:55 +03:00
local ctx = dofile_once("mods/quant.ew/files/core/ctx.lua")
local net = dofile_once("mods/quant.ew/files/core/net.lua")
2024-05-27 19:53:29 +03:00
local rpc = net.new_rpc_namespace()
2024-08-07 17:11:55 +03:00
local util = dofile_once("mods/quant.ew/files/core/util.lua")
2024-05-26 02:38:23 +03:00
local nickname = {}
2024-05-26 02:38:23 +03:00
function nickname.parse( font_filename )
local id_width = {}
local file = ModTextFileGetContent(font_filename)
local quad_open = false
local space_open = false
local id = 0
local width = 0
2024-05-26 02:38:23 +03:00
for k, split in ipairs(util.string_split(file, "%s")) do
--print(string.sub(line, 2, 9))
if (split == "</QuadChar>") then
quad_open = false
id_width[id] = width
end
if (split == "</WordSpace>") then
space_open = false
end
if (space_open == true) then
id_width["space"] = tonumber(split)
end
if (quad_open == true) then
if (string.sub(split, 1, 3) == "id=") then
id = tonumber(string.sub(split, 5, -2))
--id = string.sub(v, 5, -2)
end
if (string.sub(split, 1, 6) == "width=") then
width = tonumber(string.sub(split, 8, -2))
--width = string.sub(v, 8, -2)
end
end
if (split == "<QuadChar") then
quad_open = true
end
2024-05-26 02:38:23 +03:00
if (split == "<WordSpace>") then
space_open = true
end
end
2024-05-26 02:38:23 +03:00
return id_width
end
function nickname.calculate_textwidth(text, font)
local textwidth = 0
for i = 1,string.len(text),1
do
local l = string.sub( text, i, i)
if (l == " ") then
textwidth = textwidth + font["space"]
else
2024-05-26 02:38:23 +03:00
local c_id = string.byte(l)
2024-05-27 19:53:29 +03:00
textwidth = textwidth + (font[c_id] or 1)
2024-05-26 02:38:23 +03:00
end
end
return textwidth
end
function nickname.add_label(player_entity, text, font_filename, scale, alpha)
if not EntityGetIsAlive(player_entity)
or EntityHasTag(player_entity, "polymorphed_cessation")
or EntityHasTag(player_entity, "polymorphed_player") then
return
end
2024-05-27 19:53:29 +03:00
local prev_nickname = EntityGetFirstComponentIncludingDisabled(player_entity, "SpriteComponent", "ew_nickname")
if prev_nickname ~= nil then
EntityRemoveComponent(player_entity, prev_nickname)
end
2024-05-26 02:38:23 +03:00
if (scale == nil) then
scale = 1
end
if alpha == nil then
alpha = 0.5
2024-05-26 02:38:23 +03:00
end
local font = nickname.parse(font_filename)
2024-05-26 02:38:23 +03:00
local textwidth = nickname.calculate_textwidth(text, font)
local nickname_component = EntityAddComponent2(player_entity, "SpriteComponent", {
2024-05-27 19:53:29 +03:00
_tags="ew_nickname",
2024-05-26 02:38:23 +03:00
image_file=font_filename,
is_text_sprite=true,
offset_x=textwidth*0.5,
offset_y=13 + 12/scale,
update_transform=true,
update_transform_rotation=false,
fog_of_war_hole = true,
text=text,
2024-05-27 19:53:29 +03:00
z_index=1,
alpha=alpha,
2024-05-26 02:38:23 +03:00
emissive = true,
has_special_scale=true,
special_scale_x = scale,
special_scale_y = scale,
})
return nickname_component
end
2024-05-27 19:53:29 +03:00
function nickname.on_local_player_spawn(my_player)
2024-07-18 15:11:27 +03:00
if ctx.proxy_opt.name ~= nil then
my_player.name = ctx.proxy_opt.name
2024-05-27 19:53:29 +03:00
end
end
function nickname.on_client_spawned(peer_id, player_data)
2024-05-27 22:16:50 +03:00
nickname.add_label(player_data.entity, player_data.name, "data/fonts/font_pixel_white.xml", 0.75)
2024-05-27 19:53:29 +03:00
end
function nickname.on_should_send_updates()
print("Should send nickname update")
2024-07-18 15:11:27 +03:00
if ctx.proxy_opt.name ~= nil then
print("Sending name "..ctx.proxy_opt.name)
rpc.send_name(ctx.proxy_opt.name)
2024-05-27 19:53:29 +03:00
end
end
rpc.opts_reliable()
function rpc.send_name(name)
ctx.rpc_player_data.name = name
2024-05-27 22:16:50 +03:00
nickname.add_label(ctx.rpc_player_data.entity, name, "data/fonts/font_pixel_white.xml", 0.75)
2024-05-27 19:53:29 +03:00
end
return nickname