diff --git a/ctf_colors/hud.lua b/ctf_colors/hud.lua index c459206..c1004f9 100644 --- a/ctf_colors/hud.lua +++ b/ctf_colors/hud.lua @@ -1,22 +1,41 @@ -ctf.hud.register_part(function(player, name, tplayer) - local text_color = ctf.team(tplayer.team).data.color - local color = ctf.flag_colors[text_color] - if not color then - color = "0x000000" +function ctf_colors.get_color(name, tplayer) + local tcolor_text = ctf.team(tplayer.team).data.color + local tcolor_hex = ctf.flag_colors[tcolor_text] + if not tcolor_hex then + tcolor_hex = "0x000000" end + return tcolor_text, tcolor_hex +end + +function ctf_colors.get_nametag_color(name, tplayer, tcolor_text, tcolor_hex) + if ctf.setting("colors.nametag.tcolor") then + return "0xFF" .. string.sub(tcolor_hex, 3) + else + return "0xFFFFFFFF" + end +end + +function ctf_colors.update(player, name, tplayer) + if not player then + player = minetest.get_player_by_name(name) + end + + local tcolor_text, tcolor_hex = ctf_colors.get_color(name, tplayer) + if ctf.setting("colors.nametag") then - player:set_nametag_attributes({ color = "0xFF" .. string.sub(color, 3) }) + player:set_nametag_attributes({ + color = ctf_colors.get_nametag_color(name, tplayer, tcolor_text, tcolor_hex) }) end - if ctf.setting("colors.skins") and text_color and color then + if ctf.setting("colors.skins") and tcolor_text and tcolor_hex then if minetest.global_exists("armor") then -- TODO: how should support for skin mods be done? - armor.textures[name].skin = "ctf_colors_skin_" .. text_color .. ".png" + armor.textures[name].skin = "ctf_colors_skin_" .. tcolor_text .. ".png" armor:update_player_visuals(player) else player:set_properties({ - textures = {"ctf_colors_skin_" .. text_color .. ".png"} + textures = {"ctf_colors_skin_" .. tcolor_text .. ".png"} }) end end @@ -27,15 +46,18 @@ ctf.hud.register_part(function(player, name, tplayer) position = {x = 1, y = 0}, scale = {x = 100, y = 100}, text = "Team " .. tplayer.team, - number = color, + number = tcolor_hex, offset = {x = -20, y = 20}, alignment = {x = -1, y = 0} }) else ctf.hud:change(player, "ctf:hud_team", "text", "Team " .. tplayer.team) - ctf.hud:change(player, "ctf:hud_team", "number", color) + ctf.hud:change(player, "ctf:hud_team", "number", tcolor_hex) end -end) +end + + +ctf.hud.register_part(ctf_colors.update) --[[if minetest.global_exists("armor") and armor.get_player_skin then print("3d_armor detected!") diff --git a/ctf_colors/init.lua b/ctf_colors/init.lua index c25af37..1ec4ee2 100644 --- a/ctf_colors/init.lua +++ b/ctf_colors/init.lua @@ -1,4 +1,6 @@ -ctf.flag_colors = { +-- Supported colors +ctf_colors = {} +ctf_colors.colors = { red = "0xFF4444", cyan = "0x00FFFF", blue = "0x4466FF", @@ -12,11 +14,13 @@ ctf.flag_colors = { orange = "0xFFA500", gold = "0x808000" } +ctf.flag_colors = ctf_colors.colors ctf.register_on_init(function() ctf.log("colors", "Initialising...") ctf._set("colors.skins", false) ctf._set("colors.nametag", true) + ctf._set("colors.nametag.tcolor", false) end) dofile(minetest.get_modpath("ctf_colors") .. "/hud.lua") diff --git a/ctf_flag/api.lua b/ctf_flag/api.lua index c3483e6..6650dba 100644 --- a/ctf_flag/api.lua +++ b/ctf_flag/api.lua @@ -50,6 +50,15 @@ function ctf_flag.collect_claimed() return claimed end +function ctf_flag.get_claimed_by_player(name) + local claimed = ctf_flag.collect_claimed() + for _, flag in pairs(claimed) do + if flag.claimed.player == name then + return name + end + end +end + function ctf_flag.player_drop_flag(name) if not name then return diff --git a/ctf_flag/init.lua b/ctf_flag/init.lua index ea5243f..f27abb9 100644 --- a/ctf_flag/init.lua +++ b/ctf_flag/init.lua @@ -114,6 +114,24 @@ ctf_flag.register_on_pick_up(function(attname, flag) end end) +-- Change nametag color +local oldntc = ctf_colors.get_nametag_color +function ctf_colors.get_nametag_color(name, tplayer, tcolor_text, tcolor_hex) + if ctf_flag.get_claimed_by_player(name) then + return "0xFFFF0000" + else + return oldntc(name, tplayer, tcolor_text, tcolor_hex) + end +end + +ctf_flag.register_on_pick_up(function(attname, flag) + ctf_colors.update(nil, attname, ctf.player(attname)) +end) + +ctf_flag.register_on_drop(function(attname, flag) + ctf_colors.update(nil, attname, ctf.player(attname)) +end) + -- Drop after time local pickup_times = {} ctf_flag.register_on_pick_up(function(attname, flag)