Skip to content

Commit

Permalink
Show teammate nametags (If per-player entities are supported)
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWolfHT committed Nov 15, 2023
1 parent 3b07d85 commit 605d9a1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
44 changes: 44 additions & 0 deletions mods/ctf/ctf_modebase/features.lua
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,36 @@ local function can_punchplayer(player, hitter)
return true
end

local function update_playertag(player, t, nametag, team_nametag)
local inverse = {}
local allowed_players = table.copy(ctf_teams.online_players[t].players)
allowed_players[player:get_player_name()] = nil

for k, v in ipairs(minetest.get_connected_players()) do
local n = v:get_player_name()
if not allowed_players[n] then
inverse[n] = true
end
end

team_nametag.object:set_observers(allowed_players)
team_nametag.object:set_properties({nametag_color = ctf_teams.team[t].color})
nametag.object:set_observers(inverse)
end

local function update_playertags()
for _, p in pairs(minetest.get_connected_players()) do
local t = ctf_teams.get(p)
local playertag = playertag.get(p)
local team_nametag = playertag.nametag_entity
local nametag = playertag.entity

if t and nametag and team_nametag then
update_playertag(p, t, nametag, team_nametag)
end
end
end

local item_levels = {
"wood",
"stone",
Expand Down Expand Up @@ -486,6 +516,11 @@ return {
ctf_modebase.flag_huds.untrack_capturer(pname)

playertag.set(player, playertag.TYPE_ENTITY)

if player.set_observers then
update_playertags()
end

drop_flag(pteam)
end,
on_flag_capture = function(player, teamnames)
Expand All @@ -494,6 +529,11 @@ return {
local tcolor = ctf_teams.team[pteam].color

playertag.set(player, playertag.TYPE_ENTITY)

if player.set_observers then
update_playertags()
end

celebrate_team(pteam)

local text = " has captured the flag"
Expand Down Expand Up @@ -581,6 +621,10 @@ return {

playertag.set(player, playertag.TYPE_ENTITY)

if player.set_observers then
update_playertags()
end

tp_player_near_flag(player)
end,
on_leaveplayer = function(player)
Expand Down
2 changes: 1 addition & 1 deletion mods/ctf/ctf_modebase/mod.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name = ctf_modebase
depends = ctf_api, ctf_core, ctf_teams, ctf_gui, ctf_map, ctf_healing, crafting, mhud, default, binoculars, ctf_player, player_api
depends = ctf_api, ctf_core, ctf_teams, ctf_gui, ctf_map, ctf_healing, crafting, mhud, default, binoculars, ctf_player, player_api, playertag
49 changes: 42 additions & 7 deletions mods/other/playertag/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ playertag = {
TYPE_ENTITY = TYPE_ENTITY,
}

local function add_entity_tag(player)
local function add_entity_tag(player, old_observers)
-- Hide fixed nametag
player:set_nametag_attributes({
color = {a = 0, r = 0, g = 0, b = 0}
})

local ent = minetest.add_entity(player:get_pos(), "playertag:tag")
local ent2 = false

if ent.set_observers then
ent2 = minetest.add_entity(player:get_pos(), "playertag:tag")
ent2:set_observers(old_observers or {})
ent2:set_properties({nametag = player:get_player_name()})
end

-- Build name from font texture
local texture = "npcf_tag_bg.png"
Expand All @@ -37,38 +44,65 @@ local function add_entity_tag(player)
-- Attach to player
ent:set_attach(player, "", ATTACH_POSITION, {x=0, y=0, z=0})

if ent2 then
ent2:set_attach(player, "", ATTACH_POSITION, {x=0, y=0, z=0})
end

-- Store
players[player:get_player_name()].entity = ent
players[player:get_player_name()].entity = ent:get_luaentity()
players[player:get_player_name()].nametag_entity = ent2 and ent2:get_luaentity()
end

local function remove_entity_tag(player)
local tag = players[player:get_player_name()]
if tag and tag.entity then
tag.entity:remove()
tag.entity.object:remove()

if tag.nametag_entity then
tag.nametag_entity.object:remove()
end
end
end

local function update(player, settings)
local pname = player:get_player_name()
local old_observers = {}

if player.get_observers and players[pname] and players[pname].nametag_entity then
old_observers = players[pname].nametag_entity.object:get_observers()
end

if settings.nametag_entity_observers then
old_observers = table.copy(settings.nametag_entity_observers)
settings.nametag_entity_observers = nil
end

remove_entity_tag(player)
players[player:get_player_name()] = settings
players[pname] = settings

if settings.type == TYPE_BUILTIN then
player:set_nametag_attributes({
color = settings.color or {a=255, r=255, g=255, b=255},
bgcolor = {a=0, r=0, g=0, b=0},
})
elseif settings.type == TYPE_ENTITY then
add_entity_tag(player)
add_entity_tag(player, old_observers)
end
end

function playertag.set(player, type, color)
function playertag.set(player, type, color, extra)
local oldset = players[player:get_player_name()]
if not oldset then return end

if oldset.type ~= type or oldset.color ~= color then
update(player, {type = type, color = color})
extra = extra or {}
extra.type = type
extra.color = color

update(player, extra)
end

return players[player:get_player_name()]
end

function playertag.get(player)
Expand All @@ -83,6 +117,7 @@ minetest.register_entity("playertag:tag", {
visual = "sprite",
visual_size = {x=2.16, y=0.18, z=2.16}, --{x=1.44, y=0.12, z=1.44},
textures = {"blank.png"},
collisionbox = { 0, -0.3, 0, 0, -0.3, 0 },
physical = false,
makes_footstep_sound = false,
backface_culling = false,
Expand Down

0 comments on commit 605d9a1

Please sign in to comment.