From d2e91486ca981a862affb73724b26611ad178615 Mon Sep 17 00:00:00 2001 From: Zughy <4279489-marco_a@users.noreply.gitlab.com> Date: Thu, 14 May 2020 21:16:11 +0200 Subject: [PATCH] basic HUD for announcements implemented + practical implementation for queues --- api.lua | 5 +- hud.lua | 121 ++++++++++++++++++++++++++++++++++ init.lua | 1 + player_manager.lua | 1 + signs.lua | 64 ++++++++++++++++-- textures/arenalib_hud_bg.png | Bin 0 -> 99 bytes textures/arenalib_hud_bg2.png | Bin 0 -> 99 bytes 7 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 hud.lua create mode 100644 textures/arenalib_hud_bg.png create mode 100644 textures/arenalib_hud_bg2.png diff --git a/api.lua b/api.lua index 836d408..f0d40f3 100644 --- a/api.lua +++ b/api.lua @@ -762,10 +762,13 @@ function arena_lib.remove_player_from_arena(p_name, is_eliminated) -- se l'arena era in coda e ora ci son troppi pochi giocatori, annullo la coda if arena.in_queue then local timer = minetest.get_node_timer(arena.sign) + local players_in_arena = arena_lib.get_arena_players_count(arena) - if arena_lib.get_arena_players_count(arena) < arena.min_players then + if players_in_arena < arena.min_players then timer:stop() arena.in_queue = false + arena_lib.HUD_send_msg_all("hotbar", arena, arena.name .. " | " .. players_in_arena .. "/" .. arena.max_players .. + " | in attesa di più giocatori... (" .. arena.min_players - players_in_arena .. ")") arena_lib.send_message_players_in_arena(arena, mod_ref.prefix .. S("The queue has been cancelled due to not enough players")) end diff --git a/hud.lua b/hud.lua new file mode 100644 index 0000000..079a0b3 --- /dev/null +++ b/hud.lua @@ -0,0 +1,121 @@ +local player_huds = {} -- KEY: p_name, INDEX: {HUD_BG_ID, HUD_TXT_ID} + + + +function arena_lib.HUD_add(player) + + local HUD_BROADCAST_IMG = player:hud_add({ + hud_elem_type = "image", + position = { x = 0.5, y = 0.2}, + text = "", + scale = { x = 25, y = 2}, + number = 0xFFFFFF, + }) + + local HUD_BROADCAST_TXT = player:hud_add({ + hud_elem_type = "text", + position = { x = 0.5, y = 0.2}, + text = "", + scale = { x = 1, y = 1}, + number = 0xFFFFFF, + }) + + local HUD_HOTBAR_IMG = player:hud_add({ + hud_elem_type = "image", + position = { x = 0.5, y = 0.89}, + text = "", + scale = { x = 25, y = 1.5}, + number = 0xFFFFFF, + }) + + local HUD_HOTBAR_TXT = player:hud_add({ + hud_elem_type = "text", + position = { x = 0.5, y = 0.89}, + text = "", + scale = { x = 1, y = 1}, + number = 0xFFFFFF, + }) + + player_huds[player:get_player_name()] = {HUD_BROADCAST_IMG, HUD_BROADCAST_TXT, HUD_HOTBAR_IMG, HUD_HOTBAR_TXT} +end + + + +function arena_lib.HUD_send_msg(HUD_type, p_name, new_msg) + + local player = minetest.get_player_by_name(p_name) + local p_HUD = player_huds[p_name] + + if HUD_type == "broadcast" then + player:hud_change(p_HUD[1], "text", "arenalib_hud_bg.png") + player:hud_change(p_HUD[2], "text", new_msg) + elseif HUD_type == "hotbar" then + player:hud_change(p_HUD[3], "text", "arenalib_hud_bg2.png") + player:hud_change(p_HUD[4], "text", new_msg) + end + +end + + + +function arena_lib.HUD_send_msg_all(HUD_type, arena, new_msg) + for pl_name, _ in pairs(arena.players) do + + local pl = minetest.get_player_by_name(pl_name) + local pl_HUD = player_huds[pl_name] + + if HUD_type == "broadcast" then + pl:hud_change(pl_HUD[1], "text", "arenalib_hud_bg.png") + pl:hud_change(pl_HUD[2], "text", new_msg) + elseif HUD_type == "hotbar" then + pl:hud_change(pl_HUD[3], "text", "arenalib_hud_bg2.png") + pl:hud_change(pl_HUD[4], "text", new_msg) + end + end +end + + + +function arena_lib.HUD_hide(HUD_type, player_or_arena) + + if type(player_or_arena) == "string" then + + local player = minetest.get_player_by_name(player_or_arena) + local p_HUD = player_huds[player_or_arena] + + if HUD_type == "broadcast" then + player:hud_change(p_HUD[1], "text", "") + player:hud_change(p_HUD[2], "text", "") + elseif HUD_type == "hotbar" then + player:hud_change(p_HUD[3], "text", "") + player:hud_change(p_HUD[4], "text", "") + elseif HUD_type == "all" then + player:hud_change(p_HUD[1], "text", "") + player:hud_change(p_HUD[2], "text", "") + player:hud_change(p_HUD[3], "text", "") + player:hud_change(p_HUD[4], "text", "") + end + + elseif type(player_or_arena) == "table" then + + for pl_name, _ in pairs(player_or_arena.players) do + + local pl = minetest.get_player_by_name(pl_name) + local pl_HUD = player_huds[pl_name] + + if HUD_type == "broadcast" then + pl:hud_change(pl_HUD[1], "text", "") + pl:hud_change(pl_HUD[2], "text", "") + elseif HUD_type == "hotbar" then + pl:hud_change(pl_HUD[3], "text", "") + pl:hud_change(pl_HUD[4], "text", "") + elseif HUD_type == "all" then + pl:hud_change(pl_HUD[1], "text", "") + pl:hud_change(pl_HUD[2], "text", "") + pl:hud_change(pl_HUD[3], "text", "") + pl:hud_change(pl_HUD[4], "text", "") + end + end + end + +end diff --git a/init.lua b/init.lua index 8a84d07..3cf8677 100644 --- a/init.lua +++ b/init.lua @@ -1,6 +1,7 @@ dofile(minetest.get_modpath("arena_lib") .. "/api.lua") dofile(minetest.get_modpath("arena_lib") .. "/callbacks.lua") dofile(minetest.get_modpath("arena_lib") .. "/debug_utilities.lua") +dofile(minetest.get_modpath("arena_lib") .. "/hud.lua") dofile(minetest.get_modpath("arena_lib") .. "/items.lua") dofile(minetest.get_modpath("arena_lib") .. "/player_manager.lua") dofile(minetest.get_modpath("arena_lib") .. "/signs.lua") diff --git a/player_manager.lua b/player_manager.lua index fea5c30..4f2187b 100644 --- a/player_manager.lua +++ b/player_manager.lua @@ -1,6 +1,7 @@ minetest.register_on_joinplayer(function(player) player:set_pos(arena_lib.get_hub_spawn_point()) + arena_lib.HUD_add(player) end) diff --git a/signs.lua b/signs.lua index 0f0a401..c79ecba 100644 --- a/signs.lua +++ b/signs.lua @@ -4,6 +4,8 @@ local S = minetest.get_translator("arena_lib") local function in_game_txt(arena) end +local function HUD_countdown(arena, seconds) end +local function arena_display_format(arena, players_in_arena, msg) end @@ -52,14 +54,23 @@ minetest.override_item("default:sign_wall", { sign_arena.players[p_name] = nil arena_lib.update_sign(pos, sign_arena) arena_lib.remove_from_queue(p_name) + arena_lib.HUD_hide("all", p_name) minetest.chat_send_player(p_name, mod_ref.prefix .. S("You have left the queue")) arena_lib.send_message_players_in_arena(sign_arena, mod_ref.prefix .. S("@1 has left the queue", p_name)) + local players_in_arena = arena_lib.get_arena_players_count(sign_arena) + -- ...e la annullo se non ci sono più abbastanza persone - if arena_lib.get_arena_players_count(sign_arena) < sign_arena.min_players and sign_arena.in_queue then + if players_in_arena < sign_arena.min_players and sign_arena.in_queue then minetest.get_node_timer(pos):stop() + arena_lib.HUD_hide("broadcast", sign_arena) + arena_lib.HUD_send_msg_all("hotbar", sign_arena, arena_display_format(sign_arena, players_in_arena, "in attesa di più giocatori... ") .. + "(" .. sign_arena.min_players - players_in_arena .. ")") arena_lib.send_message_players_in_arena(sign_arena, mod_ref.prefix .. S("The queue has been cancelled due to not enough players")) sign_arena.in_queue = false + else + -- (il numero giocatori è cambiato, per questo aggiorno) + arena_lib.HUD_send_msg_all("hotbar", sign_arena, arena_display_format(sign_arena, players_in_arena,"Preparati!")) end return @@ -74,9 +85,14 @@ minetest.override_item("default:sign_wall", { arena_lib.update_sign(old_arena.sign, old_arena) arena_lib.send_message_players_in_arena(old_arena, old_mod_ref.prefix .. S("@1 has left the queue", p_name)) + local players_in_arena = arena_lib.get_arena_players_count(old_arena) + -- annullando la coda della precedente se non ci sono più abbastanza giocatori - if arena_lib.get_arena_players_count(old_arena) < old_arena.min_players and old_arena.in_queue then + if players_in_arena < old_arena.min_players and old_arena.in_queue then minetest.get_node_timer(old_arena.sign):stop() + arena_lib.HUD_hide("broadcast", old_arena) + arena_lib.HUD_send_msg_all("hotbar", old_arena, arena_display_format(old_arena, players_in_arena, "in attesa di più giocatori... ") .. + "(" .. old_arena.min_players - players_in_arena .. ")") arena_lib.send_message_players_in_arena(old_arena, old_mod_ref.prefix .. S("The queue has been cancelled due to not enough players")) old_arena.in_queue = false end @@ -107,14 +123,23 @@ minetest.override_item("default:sign_wall", { local timer = minetest.get_node_timer(pos) -- se ci sono abbastanza giocatori, parte il timer di attesa - if arena_lib.get_arena_players_count(sign_arena) == sign_arena.min_players and not sign_arena.in_queue and not sign_arena.in_game then - arena_lib.send_message_players_in_arena(sign_arena, mod_ref.prefix .. S("The game begins in @1 seconds!", mod_ref.queue_waiting_time)) - sign_arena.in_queue = true - timer:start(mod_ref.queue_waiting_time) + if not sign_arena.in_queue and not sign_arena.in_game then + + local players_in_arena = arena_lib.get_arena_players_count(sign_arena) + + if players_in_arena == sign_arena.min_players then + sign_arena.in_queue = true + timer:start(mod_ref.queue_waiting_time) + HUD_countdown(sign_arena, players_in_arena, mod_ref.queue_waiting_time) + elseif players_in_arena < sign_arena.min_players then + arena_lib.HUD_send_msg("hotbar", p_name, arena_display_format(sign_arena, players_in_arena, " in attesa di più giocatori... ") .. + "(" .. sign_arena.min_players - players_in_arena .. ")") + end end + -- se raggiungo i giocatori massimi e la partita non è iniziata, parte subito - if arena_lib.get_arena_players_count(sign_arena) == sign_arena.max_players and sign_arena.in_queue then + if players_in_arena == sign_arena.max_players and sign_arena.in_queue then timer:stop() timer:start(0.01) end @@ -134,6 +159,7 @@ minetest.override_item("default:sign_wall", { sign_arena.in_game = true arena_lib.update_sign(pos, sign_arena) + arena_lib.HUD_hide("all", sign_arena) arena_lib.load_arena(mod, arena_ID) return false @@ -187,6 +213,30 @@ end ---------------FUNZIONI LOCALI---------------- ---------------------------------------------- +function HUD_countdown(arena, players_in_arena, seconds) + if not arena.in_queue or seconds == 0 then return end + + -- dai 3 secondi in giù il messaggio è stampato su broadcast + if seconds <= 3 then + arena_lib.HUD_send_msg_all("broadcast", arena, S("The game begins in @1 seconds!", seconds)) + arena_lib.HUD_send_msg_all("hotbar", arena, arena_display_format(arena, players_in_arena, "Preparati!")) + else + arena_lib.HUD_send_msg_all("hotbar", arena, arena_display_format(arena, players_in_arena, "" .. seconds .. " secondi all'inzio")) + end + + minetest.after(1, function() + HUD_countdown(arena, players_in_arena, seconds-1) + end) +end + + +-- es. Foresta | 3/4 | Il match inizierà a breve +function arena_display_format(arena, players_in_arena, msg) + return arena.name .. " | " .. players_in_arena .. "/" .. arena.max_players .. " | " .. msg +end + + + function in_game_txt(arena) local txt diff --git a/textures/arenalib_hud_bg.png b/textures/arenalib_hud_bg.png new file mode 100644 index 0000000000000000000000000000000000000000..b4a692419dc5a2bc825488c76d844501d08e1164 GIT binary patch literal 99 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|WIbIRLo9le uO)gwqoxXs6P^JTGI+ZBxvX