diff --git a/hud.lua b/hud.lua index 4effa50..72f7375 100644 --- a/hud.lua +++ b/hud.lua @@ -1,37 +1,79 @@ -function ham_radio.init_hud(player) +function ham_radio.toggle_hud(player) local name = player:get_player_name() - ham_radio.playerhuds[name] = player:hud_add({ - hud_elem_type = "text", - text = "", - position = ham_radio.settings.hud_pos, - offset = { x = ham_radio.settings.hud_offset.x, y = ham_radio.settings.hud_offset.y }, - alignment = ham_radio.settings.hud_alignment, - number = 0xFFFFFF, - scale= { x = 100, y = 20 }, - }) + local item = player:get_wielded_item() + + -- remove hud if user does not wield a receiver + if item:get_name() ~= "ham_radio:receiver" then + if ham_radio.is_receiver_wielded[name] then + player:hud_remove(ham_radio.playerhuds[name].background) + player:hud_remove(ham_radio.playerhuds[name].frequency) + player:hud_remove(ham_radio.playerhuds[name].signal_meter) + player:hud_remove(ham_radio.playerhuds[name].signal_level) + ham_radio.is_receiver_wielded[name] = false + end + return false + end + + -- if hud is already enabled, pass + if ham_radio.is_receiver_wielded[name] then + return true + end + + -- create hud + ham_radio.is_receiver_wielded[name] = true + + local hud_pos = ham_radio.settings.hud_pos + + ham_radio.playerhuds[name] = { + background = player:hud_add({ + hud_elem_type = "image", + position = hud_pos, + offset = { x = -250, y = 20 }, + text = "ham_radio_hud_bg.png", + scale = { x = 2, y = 3 }, + alignment = { x = 1, y = 0 }, + }), + frequency = player:hud_add({ + hud_elem_type = "text", + text = "", + position = hud_pos, + offset = { x = 0, y = 0 }, + alignment = 0, + number = 0xFFFFFF, + scale= { x = 100, y = 20 }, + }), + signal_meter = player:hud_add({ + hud_elem_type = "image", + position = hud_pos, + offset = { x = -220, y = 30 }, + text = "ham_radio_hud_indicator_empty.png", + scale = { x = 2, y = 2 }, + alignment = { x = 1, y = 0 }, + }), + signal_level = player:hud_add({ + hud_elem_type = "image", + position = hud_pos, + offset = { x = -220, y = 30 }, + text = "ham_radio_hud_indicator_full.png", + scale = { x = 0, y = 2 }, + alignment = { x = 1, y = 0 }, + }) + } + return true end function ham_radio:update_hud_display(player) - local transmitter_signal = 0 - local name = player:get_player_name() - local item = player:get_wielded_item() - - if item:get_name() ~= "ham_radio:receiver" then - if self.playerlocators[name] then - player:hud_change(self.playerhuds[name], "text", "") - self.playerlocators[name] = false - end + if not ham_radio.toggle_hud(player) then return end - self.playerlocators[name] = true - - local meta = item:get_meta() - local frequency = meta:get_string("frequency") - minetest.chat_send_player(player:get_player_name(), "Configured freq:"..frequency) + local transmitter_signal = 0 + local name = player:get_player_name() + local meta = player:get_wielded_item():get_meta() + local frequency = meta:get_string("frequency") if frequency ~= nil and frequency ~= "" then local transmitter = self.read_transmitter(frequency) @@ -45,15 +87,17 @@ function ham_radio:update_hud_display(player) --local indicator = string.rep('|', transmitter.distance)..string.rep('|', transmitter.signal)..string.rep(':', 25-(transmitter.distance + transmitter.signal)) local indicator = string.rep('|', transmitter_signal)..string.rep(':', 20 - transmitter_signal) - local text = "[ Frequency: "..tostring(meta:get_string("frequency")).." ]"..indicator - - player:hud_change(self.playerhuds[name], "text", text) + --local text = "[ Frequency: "..tostring(meta:get_string("frequency")).." ]"..indicator + local text = "[ Frequency: "..tostring(meta:get_string("frequency")).." ]" + player:hud_change(self.playerhuds[name].frequency, "text", text) + player:hud_change(self.playerhuds[name].signal_level, "scale", { x = (transmitter_signal * 20) / 200 or 0.1, y = 2 }) end -minetest.register_on_newplayer(ham_radio.init_hud) -minetest.register_on_joinplayer(ham_radio.init_hud) +minetest.register_on_newplayer(ham_radio.toggle_hud) +minetest.register_on_joinplayer(ham_radio.toggle_hud) minetest.register_on_leaveplayer(function(player) + ham_radio.is_receiver_wielded[name] = false ham_radio.playerhuds[player:get_player_name()] = nil end) diff --git a/init.lua b/init.lua index 554eaff..611d540 100644 --- a/init.lua +++ b/init.lua @@ -5,11 +5,9 @@ ham_radio = rawget(_G, "ham_radio") or {} ham_radio = { playerhuds = {}, - playerlocators = {}, + is_receiver_wielded = {}, settings = { - hud_pos = { x = 0.45, y = 0.7 }, - hud_offset = { x = 15, y = 15 }, - hud_alignment = { x = 1, y = 0 } + hud_pos = { x = 0.5, y = 0.8 }, } } diff --git a/textures/ham_radio_hud_bg.png b/textures/ham_radio_hud_bg.png new file mode 100644 index 0000000..9ed15cc Binary files /dev/null and b/textures/ham_radio_hud_bg.png differ diff --git a/textures/ham_radio_hud_indicator_empty.png b/textures/ham_radio_hud_indicator_empty.png new file mode 100644 index 0000000..22cddb7 Binary files /dev/null and b/textures/ham_radio_hud_indicator_empty.png differ diff --git a/textures/ham_radio_hud_indicator_full.png b/textures/ham_radio_hud_indicator_full.png new file mode 100644 index 0000000..8a05e88 Binary files /dev/null and b/textures/ham_radio_hud_indicator_full.png differ diff --git a/textures/indicator.xcf b/textures/indicator.xcf new file mode 100644 index 0000000..31a83b1 Binary files /dev/null and b/textures/indicator.xcf differ