mirror of
https://github.com/TeTpaAka/quests.git
synced 2024-12-22 16:50:39 +01:00
autohide the hud when there are no active quests
This commit is contained in:
parent
55948b6b97
commit
9feac43e64
12
core.lua
12
core.lua
@ -110,9 +110,15 @@ function quests.accept_quest(playername, questname)
|
|||||||
quests.successfull_quests[playername][questname] = {count = 1}
|
quests.successfull_quests[playername][questname] = {count = 1}
|
||||||
end
|
end
|
||||||
quests.active_quests[playername][questname].finished = true
|
quests.active_quests[playername][questname].finished = true
|
||||||
|
for _,quest in ipairs(quests.hud[playername].list) do
|
||||||
|
if (quest.name == questname) then
|
||||||
|
local player = minetest.get_player_by_name(playername)
|
||||||
|
player:hud_change(quest.id, "number", 0x00AD00)
|
||||||
|
end
|
||||||
|
end
|
||||||
minetest.after(3, function(playername, questname)
|
minetest.after(3, function(playername, questname)
|
||||||
quests.active_quests[playername][questname] = nil
|
quests.active_quests[playername][questname] = nil
|
||||||
minetest.after(1,quests.update_hud,playername)
|
quests.update_hud(playername)
|
||||||
end, playername, questname)
|
end, playername, questname)
|
||||||
return true -- the quest is finished, the mod can give a reward
|
return true -- the quest is finished, the mod can give a reward
|
||||||
end
|
end
|
||||||
@ -141,7 +147,7 @@ function quests.abort_quest(playername, questname)
|
|||||||
end
|
end
|
||||||
|
|
||||||
quests.active_quests[playername][questname].finished = true
|
quests.active_quests[playername][questname].finished = true
|
||||||
for _,quest in ipairs(quests.hud[playername]) do
|
for _,quest in ipairs(quests.hud[playername].list) do
|
||||||
if (quest.name == questname) then
|
if (quest.name == questname) then
|
||||||
local player = minetest.get_player_by_name(playername)
|
local player = minetest.get_player_by_name(playername)
|
||||||
player:hud_change(quest.id, "number", 0xAD0000)
|
player:hud_change(quest.id, "number", 0xAD0000)
|
||||||
@ -149,7 +155,7 @@ function quests.abort_quest(playername, questname)
|
|||||||
end
|
end
|
||||||
minetest.after(3, function(playername, questname)
|
minetest.after(3, function(playername, questname)
|
||||||
quests.active_quests[playername][questname] = nil
|
quests.active_quests[playername][questname] = nil
|
||||||
minetest.after(1,quests.update_hud,playername)
|
quests.update_hud(playername)
|
||||||
end, playername, questname)
|
end, playername, questname)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,13 +65,19 @@ function quests.create_config(playername, integrated)
|
|||||||
formspec = formspec .. "size[7,3]"
|
formspec = formspec .. "size[7,3]"
|
||||||
end
|
end
|
||||||
formspec = formspec .. "checkbox[.25,.25;quests_config_enable;" .. S("Enable HUD") .. ";"
|
formspec = formspec .. "checkbox[.25,.25;quests_config_enable;" .. S("Enable HUD") .. ";"
|
||||||
if(quests.hud[playername] ~= nil) then
|
if(quests.hud[playername] ~= nil and quests.hud[playername].list ~= nil) then
|
||||||
|
formspec = formspec .. "true"
|
||||||
|
else
|
||||||
|
formspec = formspec .. "false"
|
||||||
|
end
|
||||||
|
formspec = formspec .. "]checkbox[.25,.75;quests_config_autohide;" .. S("Autohide HUD") .. ";"
|
||||||
|
if(quests.hud[playername] ~= nil and quests.hud[playername].autohide) then
|
||||||
formspec = formspec .. "true"
|
formspec = formspec .. "true"
|
||||||
else
|
else
|
||||||
formspec = formspec .. "false"
|
formspec = formspec .. "false"
|
||||||
end
|
end
|
||||||
formspec = formspec .. "]"..
|
formspec = formspec .. "]"..
|
||||||
"button[.25,1.25;3,.7;quests_config_return;" .. S("Return") .. "]"
|
"button[.25,1.75;3,.7;quests_config_return;" .. S("Return") .. "]"
|
||||||
return formspec
|
return formspec
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -173,11 +179,30 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||||||
|
|
||||||
-- config
|
-- config
|
||||||
if (fields["quests_config_enable"]) then
|
if (fields["quests_config_enable"]) then
|
||||||
|
quests.hud[playername].autohide = false
|
||||||
if (fields["quests_config_enable"] == "true") then
|
if (fields["quests_config_enable"] == "true") then
|
||||||
quests.show_hud(playername)
|
quests.show_hud(playername)
|
||||||
else
|
else
|
||||||
quests.hide_hud(playername)
|
quests.hide_hud(playername)
|
||||||
end
|
end
|
||||||
|
if (formname == "quests:config") then
|
||||||
|
minetest.show_formspec(playername, "quests:config", quests.create_config(playername))
|
||||||
|
else
|
||||||
|
unified_inventory.set_inventory_formspec(player, "quests_config")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if (fields["quests_config_autohide"]) then
|
||||||
|
if (fields["quests_config_autohide"] == "true") then
|
||||||
|
quests.hud[playername].autohide = true
|
||||||
|
quests.update_hud(playername)
|
||||||
|
else
|
||||||
|
quests.hud[playername].autohide = false
|
||||||
|
end
|
||||||
|
if (formname == "quests:config") then
|
||||||
|
minetest.show_formspec(playername, "quests:config", quests.create_config(playername))
|
||||||
|
else
|
||||||
|
unified_inventory.set_inventory_formspec(player, "quests_config")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if (fields["quests_config_return"]) then
|
if (fields["quests_config_return"]) then
|
||||||
if (formname == "quests:config") then
|
if (formname == "quests:config") then
|
||||||
|
83
hud.lua
83
hud.lua
@ -15,8 +15,11 @@ local hud_config = { position = {x = 1, y = 0.2},
|
|||||||
|
|
||||||
-- call this function to enable the HUD for the player that shows his quests
|
-- call this function to enable the HUD for the player that shows his quests
|
||||||
-- the HUD can only show up to show_max quests
|
-- the HUD can only show up to show_max quests
|
||||||
function quests.show_hud(playername)
|
function quests.show_hud(playername, autohide)
|
||||||
if (quests.hud[playername] ~= nil) then
|
if (quests.hud[playername] == nil) then
|
||||||
|
quests.hud[playername] = { autohide = autohide}
|
||||||
|
end
|
||||||
|
if (quests.hud[playername].list ~= nil) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local hud = {
|
local hud = {
|
||||||
@ -31,20 +34,20 @@ function quests.show_hud(playername)
|
|||||||
|
|
||||||
local player = minetest.get_player_by_name(playername)
|
local player = minetest.get_player_by_name(playername)
|
||||||
if (player == nil) then
|
if (player == nil) then
|
||||||
return
|
return false
|
||||||
end
|
end
|
||||||
quests.hud[playername] = {}
|
quests.hud[playername].list = {}
|
||||||
table.insert(quests.hud[playername], { value=0, id=player:hud_add(hud) })
|
table.insert(quests.hud[playername].list, { value=0, id=player:hud_add(hud) })
|
||||||
minetest.after(0, quests.update_hud, playername)
|
minetest.after(0, quests.update_hud, playername)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- call this method to hide the hud
|
-- call this method to hide the hud
|
||||||
function quests.hide_hud(playername)
|
function quests.hide_hud(playername)
|
||||||
local player = minetest.get_player_by_name(playername)
|
local player = minetest.get_player_by_name(playername)
|
||||||
if (player == nil) then
|
if (player == nil or quests.hud[playername] == nil or quests.hud[playername].list == nil) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
for _,quest in pairs(quests.hud[playername]) do
|
for _,quest in pairs(quests.hud[playername].list) do
|
||||||
player:hud_remove(quest.id)
|
player:hud_remove(quest.id)
|
||||||
if (quest.id_background ~= nil) then
|
if (quest.id_background ~= nil) then
|
||||||
player:hud_remove(quest.id_background)
|
player:hud_remove(quest.id_background)
|
||||||
@ -53,7 +56,7 @@ function quests.hide_hud(playername)
|
|||||||
player:hud_remove(quest.id_bar)
|
player:hud_remove(quest.id_bar)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
quests.hud[playername] = nil
|
quests.hud[playername].list = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -71,6 +74,12 @@ function quests.update_hud(playername)
|
|||||||
if (quests.hud[playername] == nil or quests.active_quests[playername] == nil) then
|
if (quests.hud[playername] == nil or quests.active_quests[playername] == nil) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
if (quests.hud[playername].list == nil) then
|
||||||
|
if (quests.hud[playername].autohide and next(quests.active_quests[playername]) ~= nil) then
|
||||||
|
quests.show_hud(playername)
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
local player = minetest.get_player_by_name(playername)
|
local player = minetest.get_player_by_name(playername)
|
||||||
if (player == nil) then
|
if (player == nil) then
|
||||||
return
|
return
|
||||||
@ -81,14 +90,11 @@ function quests.update_hud(playername)
|
|||||||
local change = false
|
local change = false
|
||||||
local visible = {}
|
local visible = {}
|
||||||
local remove = {}
|
local remove = {}
|
||||||
for j,hud_element in ipairs(quests.hud[playername]) do
|
for j,hud_element in ipairs(quests.hud[playername].list) do
|
||||||
if (hud_element.name ~= nil) then
|
if (hud_element.name ~= nil) then
|
||||||
if (quests.active_quests[playername][hud_element.name] ~= nil) then
|
if (quests.active_quests[playername][hud_element.name] ~= nil) then
|
||||||
if (hud_element.value ~= quests.active_quests[playername][hud_element.name].value) then
|
if (hud_element.value ~= quests.active_quests[playername][hud_element.name].value) then
|
||||||
hud_element.value = quests.active_quests[playername][hud_element.name].value
|
hud_element.value = quests.active_quests[playername][hud_element.name].value
|
||||||
if (hud_element.value == quests.registered_quests[hud_element.name].max) then
|
|
||||||
player:hud_change(hud_element.id, "number", 0x00BB00)
|
|
||||||
end
|
|
||||||
player:hud_change(hud_element.id, "text", get_quest_hud_string(hud_element.name, quests.active_quests[playername][hud_element.name]))
|
player:hud_change(hud_element.id, "text", get_quest_hud_string(hud_element.name, quests.active_quests[playername][hud_element.name]))
|
||||||
if (hud_element.id_bar ~= nil) then
|
if (hud_element.id_bar ~= nil) then
|
||||||
player:hud_change(hud_element.id_bar, "number", math.floor(40 * hud_element.value / quests.registered_quests[hud_element.name].max))
|
player:hud_change(hud_element.id_bar, "number", math.floor(40 * hud_element.value / quests.registered_quests[hud_element.name].max))
|
||||||
@ -121,7 +127,7 @@ function quests.update_hud(playername)
|
|||||||
--remove ended quests
|
--remove ended quests
|
||||||
if (remove[1] ~= nil) then
|
if (remove[1] ~= nil) then
|
||||||
for _,j in ipairs(remove) do
|
for _,j in ipairs(remove) do
|
||||||
table.remove(quests.hud[playername], j)
|
table.remove(quests.hud[playername].list, j)
|
||||||
i = i - 1
|
i = i - 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -157,7 +163,7 @@ function quests.update_hud(playername)
|
|||||||
text = "quests_questbar.png" })
|
text = "quests_questbar.png" })
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(quests.hud[playername], { name = questname,
|
table.insert(quests.hud[playername].list, { name = questname,
|
||||||
id = id,
|
id = id,
|
||||||
id_background = id_background,
|
id_background = id_background,
|
||||||
id_bar = id_bar,
|
id_bar = id_bar,
|
||||||
@ -168,19 +174,48 @@ function quests.update_hud(playername)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if (quests.hud[playername].autohide) then
|
||||||
|
if (next(quests.active_quests[playername]) == nil) then
|
||||||
|
player:hud_change(quests.hud[playername].list[1].id, "text", S("No more Quests"))
|
||||||
|
minetest.after(3, function(playername)
|
||||||
|
if (next(quests.active_quests[playername]) ~= nil) then
|
||||||
|
player:hud_change(quests.hud[playername].list[1].id, "text", S("Quests:"))
|
||||||
|
else
|
||||||
|
quests.hide_hud(playername)
|
||||||
|
end
|
||||||
|
end, playername)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- show the HUDs
|
-- show the HUDs
|
||||||
for playername,id in pairs(quests.hud) do
|
--for playername,id in pairs(quests.hud) do
|
||||||
if (id ~= nil) then
|
-- if (id ~= nil) then
|
||||||
quests.hud[playername] = nil
|
-- quests.hud[playername] = nil
|
||||||
minetest.after(10, function(playername)
|
-- minetest.after(10, function(playername)
|
||||||
quests.show_hud(playername)
|
-- quests.show_hud(playername)
|
||||||
quests.update_hud(playername)
|
-- quests.update_hud(playername)
|
||||||
end, playername)
|
-- end, playername)
|
||||||
|
-- end
|
||||||
|
--end
|
||||||
|
|
||||||
|
minetest.register_on_joinplayer(function(player)
|
||||||
|
local playername = player:get_player_name()
|
||||||
|
if (quests.hud[playername] ~= nil) then
|
||||||
|
if (not(quests.hud[playername].first)) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local list = quests.hud[playername].list
|
||||||
|
print(dump(list))
|
||||||
|
local autohide = quests.hud[playername].autohide
|
||||||
|
quests.hud[playername] = { autohide = autohide }
|
||||||
|
if (list ~= nil) then
|
||||||
|
minetest.after(1, function(playername)
|
||||||
|
quests.show_hud(playername)
|
||||||
|
end, playername)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
3
init.lua
3
init.lua
@ -11,6 +11,9 @@ quests.active_quests = quests.active_quests or {}
|
|||||||
quests.successfull_quests = quests.successfull_quests or {}
|
quests.successfull_quests = quests.successfull_quests or {}
|
||||||
quests.failed_quests = quests.failed_quests or {}
|
quests.failed_quests = quests.failed_quests or {}
|
||||||
quests.hud = quests.hud or {}
|
quests.hud = quests.hud or {}
|
||||||
|
for idx,_ in pairs(quests.hud) do
|
||||||
|
quests.hud[idx].first = true
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
quests.formspec_lists = {}
|
quests.formspec_lists = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user