ham_radio/hud.lua

129 lines
3.7 KiB
Lua
Raw Normal View History

2019-12-06 15:25:03 +01:00
2019-12-07 10:42:08 +01:00
function ham_radio.toggle_hud(player)
2019-12-06 15:25:03 +01:00
local name = player:get_player_name()
2019-12-07 10:42:08 +01:00
local item = player:get_wielded_item()
2019-12-08 18:07:18 +01:00
-- remove hud and RDS if user does not wield a receiver
2019-12-08 13:46:12 +01:00
if item:get_name() ~= "ham_radio:handheld_receiver" then
2019-12-07 10:42:08 +01:00
if ham_radio.is_receiver_wielded[name] then
2019-12-08 15:58:45 +01:00
for hud_id, hud_handler in pairs(ham_radio.playerhuds[name]) do
player:hud_remove(hud_handler)
end
ham_radio.playerhuds[name] = nil
2019-12-07 21:54:19 +01:00
ham_radio.is_receiver_wielded[name] = false
2019-12-08 18:07:18 +01:00
ham_radio.player_rds[name] = nil
2019-12-07 10:42:08 +01:00
end
return false
end
-- if hud is already enabled, pass
if ham_radio.is_receiver_wielded[name] then
return true
2019-12-07 10:42:08 +01:00
end
-- create hud
ham_radio.is_receiver_wielded[name] = true
local hud_pos = ham_radio.settings.hud_pos
2019-12-08 20:43:32 +01:00
local hud_color = ham_radio.settings.hud_color
2019-12-07 10:42:08 +01:00
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 = 2 },
2019-12-07 10:42:08 +01:00
alignment = { x = 1, y = 0 },
}),
frequency = player:hud_add({
hud_elem_type = "text",
text = "",
position = hud_pos,
2019-12-08 17:42:40 +01:00
offset = { x = -220, y = 5 },
alignment = { x = 1, y = 0},
2019-12-08 20:43:32 +01:00
number = hud_color.inactive,
2019-12-07 10:42:08 +01:00
scale= { x = 100, y = 20 },
}),
2019-12-08 18:07:18 +01:00
rds = player:hud_add({
2019-12-08 17:42:40 +01:00
hud_elem_type = "text",
text = "",
position = hud_pos,
offset = { x = 220, y = 5 },
alignment = { x = -1, y = 0},
2019-12-08 20:43:32 +01:00
number = hud_color.inactive,
2019-12-08 17:42:40 +01:00
scale= { x = 100, y = 20 },
}),
signal_meter = player:hud_add({
2019-12-07 10:42:08 +01:00
hud_elem_type = "image",
position = hud_pos,
offset = { x = -220, y = 35 },
2019-12-07 10:42:08 +01:00
text = "ham_radio_hud_indicator_empty.png",
scale = { x = 2, y = 1 },
2019-12-07 10:42:08 +01:00
alignment = { x = 1, y = 0 },
}),
2019-12-08 17:42:40 +01:00
signal_level = player:hud_add({
hud_elem_type = "image",
2019-12-07 10:42:08 +01:00
position = hud_pos,
offset = { x = -220, y = 35 },
2019-12-07 10:42:08 +01:00
text = "ham_radio_hud_indicator_full.png",
scale = { x = 0, y = 1 },
2019-12-07 10:42:08 +01:00
alignment = { x = 1, y = 0 },
})
}
return true
2019-12-06 15:25:03 +01:00
end
function ham_radio:update_hud_display(player)
2019-12-07 10:42:08 +01:00
if not ham_radio.toggle_hud(player) then
2019-12-06 15:25:03 +01:00
return
end
local signal_power = 0
2019-12-07 10:42:08 +01:00
local name = player:get_player_name()
local meta = player:get_wielded_item():get_meta()
local frequency = meta:get_string("frequency")
2019-12-07 19:40:42 +01:00
2019-12-06 15:25:03 +01:00
if frequency ~= nil and frequency ~= "" then
2019-12-07 19:40:42 +01:00
local transmitters = self.find_transmitters(frequency)
for position, transmitter in pairs(transmitters) do
local transmitter_signal = self:locate_transmitter(player, minetest.string_to_pos(position))
if transmitter_signal > signal_power then
-- use max power from transmitters nearby
signal_power = transmitter_signal
end
2019-12-06 15:25:03 +01:00
end
end
2019-12-08 17:42:40 +01:00
2019-12-08 20:43:32 +01:00
local hud_color = ham_radio.settings.hud_color
-- update frequency hud
local frequency_text = "FQ ---"
local frequency_color = hud_color.inactive
if frequency ~= "" then
frequency_text = "FQ "..frequency
frequency_color = hud_color.active
2019-12-08 17:42:40 +01:00
end
2019-12-08 20:43:32 +01:00
player:hud_change(self.playerhuds[name].frequency, "text", frequency_text)
player:hud_change(self.playerhuds[name].frequency, "number", frequency_color)
2019-12-08 17:42:40 +01:00
2019-12-08 20:43:32 +01:00
-- update RDS hud
local rds_text = "RDS off"
local rds_color = hud_color.inactive
2019-12-08 18:07:18 +01:00
if meta:get_string("rds_disabled") == "" then
2019-12-08 20:43:32 +01:00
rds_text = "RDS ON"
rds_color = hud_color.active
2019-12-08 17:42:40 +01:00
end
2019-12-08 20:43:32 +01:00
player:hud_change(self.playerhuds[name].rds, "text", rds_text)
player:hud_change(self.playerhuds[name].rds, "number", rds_color)
2019-12-08 17:42:40 +01:00
2019-12-08 20:43:32 +01:00
-- update signal level hud
player:hud_change(
self.playerhuds[name].signal_level,
"scale",
{ x = signal_power/50 or 0.1, y = 1 } -- x scale should be 0-2
)
2019-12-06 15:25:03 +01:00
end