From 4d00a8cf1d801e0467be74827d93efdc973892d4 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Mon, 7 Sep 2015 10:31:09 +0100 Subject: [PATCH] Create ctf_colors, clean up hud --- ctf/depends.txt | 3 +- ctf/gui.lua | 45 -------------------- ctf/hud.lua | 96 ++++++++---------------------------------- ctf/init.lua | 16 ------- ctf_colors/depends.txt | 1 + ctf_colors/init.lua | 84 ++++++++++++++++++++++++++++++++++++ ctf_flag/depends.txt | 1 + hudkit/init.lua | 61 +++++++++++++++++++++++++++ 8 files changed, 166 insertions(+), 141 deletions(-) create mode 100644 ctf_colors/depends.txt create mode 100644 ctf_colors/init.lua create mode 100644 hudkit/init.lua diff --git a/ctf/depends.txt b/ctf/depends.txt index fab6a5c..5588f85 100644 --- a/ctf/depends.txt +++ b/ctf/depends.txt @@ -1 +1,2 @@ -chatplus? \ No newline at end of file +chatplus? +hudkit diff --git a/ctf/gui.lua b/ctf/gui.lua index c1b5da3..79d1424 100644 --- a/ctf/gui.lua +++ b/ctf/gui.lua @@ -199,29 +199,6 @@ ctf.gui.register_tab("diplo", "Diplomacy", function(name, team) ) end) --- Team interface -ctf.gui.register_tab("settings", "Settings", function(name, team) - local color = "" - - if ctf.team(team).data.color then - color = ctf.team(team).data.color - end - - local result = "field[3,2;4,1;color;Team Color;" .. color .. "]" .. - "button[4,6;2,1;save;Save]" - - - if not ctf.can_mod(name,team) then - result = "label[0.5,1;You do not own this team!" - end - - minetest.show_formspec(name, "ctf:settings", - "size[10,7]" .. - ctf.gui.get_tabs(name, team) .. - result - ) -end) - local function formspec_is_ctf_tab(fsname) for name, tab in pairs(ctf.gui.tabs) do if fsname == "ctf:" .. name then @@ -261,28 +238,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) ctf.gui.show(name, "news") return true end - - -- Settings page - if fields.save and formname == "ctf:settings" then - ctf.gui.show(name, "settings") - - if ctf.flag_colors[fields.color] then - team.data.color = fields.color - ctf.needs_save = true - else - local colors = "" - for color, code in pairs(ctf.flag_colors) do - if colors ~= "" then - colors = colors .. ", " - end - colors = colors .. color - end - minetest.chat_send_player(name, "Color " .. fields.color .. - " does not exist! Available: " .. colors) - end - - return true - end end) minetest.register_on_player_receive_fields(function(player, formname, fields) diff --git a/ctf/hud.lua b/ctf/hud.lua index debb445..cf41efe 100644 --- a/ctf/hud.lua +++ b/ctf/hud.lua @@ -1,66 +1,8 @@ -local function hudkit() - return { - players = {}, - - add = function(self, player, id, def) - local name = player:get_player_name() - local elements = self.players[name] - - if not elements then - self.players[name] = {} - elements = self.players[name] - end - - elements[id] = player:hud_add(def) - return true - end, - - exists = function(self, player, id) - if not player then - return false - end - - local name = player:get_player_name() - local elements = self.players[name] - - if not elements or not elements[id] then - return false - end - return true - end, - - change = function(self, player, id, stat, value) - if not player then - return false - end - - local name = player:get_player_name() - local elements = self.players[name] - - if not elements or not elements[id] then - return false - end - - player:hud_change(elements[id], stat, value) - return true - end, - - remove = function(self, player, id) - local name = player:get_player_name() - local elements = self.players[name] - - if not elements or not elements[id] then - return false - end - - player:hud_remove(elements[id]) - elements[id] = nil - return true - end - } -end - ctf.hud = hudkit() +ctf.hud.parts = {} +function ctf.hud.register_part(func) + table.insert(ctf.hud.parts, func) +end minetest.register_on_leaveplayer(function(player) ctf.hud.players[player:get_player_name()] = nil @@ -79,29 +21,27 @@ function ctf.hud.update(player) end -- Team Identifier - local color = ctf.flag_colors[ctf.team(tplayer.team).data.color] - if not color then - color = "0x000000" + for i = 1, #ctf.hud.parts do + ctf.hud.parts[i](player, name, tplayer) end - player:set_nametag_attributes({ color = "0xFF" .. string.sub(color, 3) }) +end + +ctf.hud.register_part(function(player, name, tplayer) if not ctf.hud:exists(player, "ctf:hud_team") then - return ctf.hud:add(player, "ctf:hud_team", { + ctf.hud:add(player, "ctf:hud_team", { hud_elem_type = "text", position = {x = 1, y = 0}, scale = {x = 100, y = 100}, text = tplayer.team, - number = color, + number = "0x000000", offset = {x=-100, y = 20} }) else ctf.hud:change(player, "ctf:hud_team", "text", tplayer.team) - ctf.hud:change(player, "ctf:hud_team", "number", color) end -end +end) -local count = 0 function ctf.hud.updateAll() - count = 0 if not ctf.setting("hud") then return end @@ -112,10 +52,8 @@ function ctf.hud.updateAll() end end -minetest.register_globalstep(function(delta) - count = count + delta - - if count > 10 then - ctf.hud.updateAll() - end -end) +local function tick() + ctf.hud.updateAll() + minetest.after(10, tick) +end +minetest.after(1, tick) diff --git a/ctf/init.lua b/ctf/init.lua index f98bba7..1239818 100644 --- a/ctf/init.lua +++ b/ctf/init.lua @@ -21,22 +21,6 @@ minetest.register_privilege("ctf_admin", { description = "Can create teams, manage players, assign team owners.", }) --- Colors -ctf.flag_colors = { - red = "0xFF4444", - cyan = "0x00FFFF", - blue = "0x4466FF", - purple = "0x800080", - yellow = "0xFFFF00", - green = "0x00FF00", - pink = "0xFF00FF", - silver = "0xC0C0C0", - gray = "0x808080", - black = "0x000000", - orange = "0xFFA500", - gold = "0x808000" -} - -- Modules dofile(minetest.get_modpath("ctf") .. "/core.lua") dofile(minetest.get_modpath("ctf") .. "/teams.lua") diff --git a/ctf_colors/depends.txt b/ctf_colors/depends.txt new file mode 100644 index 0000000..0dcc91d --- /dev/null +++ b/ctf_colors/depends.txt @@ -0,0 +1 @@ +ctf diff --git a/ctf_colors/init.lua b/ctf_colors/init.lua new file mode 100644 index 0000000..42f8792 --- /dev/null +++ b/ctf_colors/init.lua @@ -0,0 +1,84 @@ +ctf.flag_colors = { + red = "0xFF4444", + cyan = "0x00FFFF", + blue = "0x4466FF", + purple = "0x800080", + yellow = "0xFFFF00", + green = "0x00FF00", + pink = "0xFF00FF", + silver = "0xC0C0C0", + gray = "0x808080", + black = "0x000000", + orange = "0xFFA500", + gold = "0x808000" +} + +ctf.hud.register_part(function(player, name, tplayer) + local color = ctf.flag_colors[ctf.team(tplayer.team).data.color] + if not color then + color = "0x000000" + end + player:set_nametag_attributes({ color = "0xFF" .. string.sub(color, 3) }) + + if not ctf.hud:exists(player, "ctf:hud_team") then + ctf.hud:add(player, "ctf:hud_team", { + hud_elem_type = "text", + position = {x = 1, y = 0}, + scale = {x = 100, y = 100}, + text = tplayer.team, + number = color, + offset = {x=-100, y = 20} + }) + else + ctf.hud:change(player, "ctf:hud_team", "number", color) + end +end) + +ctf.gui.register_tab("settings", "Settings", function(name, team) + local color = "" + if ctf.team(team).data.color then + color = ctf.team(team).data.color + end + + local result = "field[3,2;4,1;color;Team Color;" .. color .. "]" .. + "button[4,6;2,1;save;Save]" + + + if not ctf.can_mod(name,team) then + result = "label[0.5,1;You do not own this team!" + end + + minetest.show_formspec(name, "ctf:settings", + "size[10,7]" .. + ctf.gui.get_tabs(name, team) .. + result + ) +end) + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname ~= "ctf:settings" then + return false + end + + -- Settings page + if fields.save then + ctf.gui.show(name, "settings") + + if ctf.flag_colors[fields.color] then + team.data.color = fields.color + ctf.needs_save = true + else + local colors = "" + for color, code in pairs(ctf.flag_colors) do + if colors ~= "" then + colors = colors .. ", " + end + colors = colors .. color + end + minetest.chat_send_player(name, "Color " .. fields.color .. + " does not exist! Available: " .. colors) + end + + return true + end +end) diff --git a/ctf_flag/depends.txt b/ctf_flag/depends.txt index 0dcc91d..4c5cce5 100644 --- a/ctf_flag/depends.txt +++ b/ctf_flag/depends.txt @@ -1 +1,2 @@ ctf +ctf_colors diff --git a/hudkit/init.lua b/hudkit/init.lua new file mode 100644 index 0000000..3d016c9 --- /dev/null +++ b/hudkit/init.lua @@ -0,0 +1,61 @@ +function hudkit() + return { + players = {}, + + add = function(self, player, id, def) + local name = player:get_player_name() + local elements = self.players[name] + + if not elements then + self.players[name] = {} + elements = self.players[name] + end + + elements[id] = player:hud_add(def) + return true + end, + + exists = function(self, player, id) + if not player then + return false + end + + local name = player:get_player_name() + local elements = self.players[name] + + if not elements or not elements[id] then + return false + end + return true + end, + + change = function(self, player, id, stat, value) + if not player then + return false + end + + local name = player:get_player_name() + local elements = self.players[name] + + if not elements or not elements[id] then + return false + end + + player:hud_change(elements[id], stat, value) + return true + end, + + remove = function(self, player, id) + local name = player:get_player_name() + local elements = self.players[name] + + if not elements or not elements[id] then + return false + end + + player:hud_remove(elements[id]) + elements[id] = nil + return true + end + } +end