hud.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. local saved_huds = {}
  2. local ght = tonumber(minetest.settings:get("goblins_hud_timer")) or 0
  3. local function clear_hud(params)
  4. local player = minetest.get_player_by_name(params[1])
  5. if player then
  6. local meta = player:get_meta()
  7. --print ("testing"..dump(params))
  8. if os.time() >= (meta:get_int("hud_time_start") + ght) then
  9. player:hud_remove(params[2])
  10. meta:set_int("hud_cleared",1)
  11. --print ("removing"..dump(params))
  12. end
  13. end
  14. end
  15. function goblins.update_hud(player)
  16. if ght > 0 and player then
  17. --for _,player in ipairs(minetest.get_connected_players()) do
  18. local player_name = player:get_player_name()
  19. --print(player_name)
  20. local meta = player:get_meta()
  21. local goblin_current = meta:get_string("goblin_current")
  22. local territory_current = meta:get_string("territory_current")
  23. local known_territories = minetest.deserialize(meta:get_string("territory_list"))
  24. local territory = "unknown"
  25. if known_territories then
  26. for k,v in pairs(known_territories) do
  27. if territory_current == v then
  28. territory = v
  29. end
  30. end
  31. end
  32. local text_goblin_current = goblin_current
  33. local text_territory_current = "of " ..territory
  34. local territory_score_table = minetest.deserialize(meta:get_string(territory_current))
  35. local text_territory_scores = {}
  36. if not territory_score_table then return end
  37. for k,v in pairs(territory_score_table) do
  38. text_territory_scores[k] = k..": "..v
  39. end
  40. local ids = saved_huds[player_name]
  41. if not ids then
  42. ids = {}
  43. ids.territory_scores = {}
  44. saved_huds[player_name] = ids
  45. -- create HUD elements and set ids into `ids`
  46. --[[
  47. player:hud_add({
  48. hud_elem_type = "text",
  49. position = {x = 1, y = 0.5},
  50. offset = {x = -120, y = -25},
  51. text = "Goblin HUD",
  52. alignment = -0,
  53. scale = { x = 100, y = 30},
  54. number = 0xFFFFFF,
  55. })
  56. --]]
  57. ids.goblin_current = player:hud_add({
  58. hud_elem_type = "text",
  59. position = {x = 1, y = 0.5},
  60. offset = {x = -120, y = -25},
  61. text = text_goblin_current,
  62. alignment = -1,
  63. scale = { x = 50, y = 10},
  64. number = 0xFFFFFF,
  65. })
  66. local params = {player_name,ids.goblin_current}
  67. minetest.after((ght+5),clear_hud, params)
  68. ids.territory_current = player:hud_add({
  69. hud_elem_type = "text",
  70. position = {x = 1, y = 0.5},
  71. offset = {x = -120, y = 0},
  72. text = text_territory_current,
  73. alignment = -1,
  74. scale = { x = 50, y = 10},
  75. number = 0xFFFFFF,
  76. })
  77. local params = {player_name,ids.territory_current}
  78. minetest.after((ght+5),clear_hud, params)
  79. local tst_yo = 0
  80. for k,v in pairs(territory_score_table) do
  81. tst_yo = tst_yo + 20
  82. ids.territory_scores[k] = player:hud_add({
  83. hud_elem_type = "text",
  84. position = {x = 1, y = 0.5},
  85. offset = {x = -120, y = tst_yo},
  86. text = text_territory_scores[k],
  87. alignment = -1,
  88. scale = { x = 50, y = 10},
  89. number = 0xFFFFFF,
  90. })
  91. local params = {player_name,ids.territory_scores[k]}
  92. minetest.after(ght+5,clear_hud, params)
  93. end
  94. --minetest.after(ght,clear_hud, params)
  95. else
  96. if meta:get_int("hud_cleared") == 1 then
  97. --hud_params_title()
  98. ids.goblin_current = player:hud_add({
  99. hud_elem_type = "text",
  100. position = {x = 1, y = 0.5},
  101. offset = {x = -120, y = -25},
  102. text = text_goblin_current,
  103. alignment = -1,
  104. scale = { x = 50, y = 10},
  105. number = 0xFFFFFF,
  106. })
  107. local params = {player_name,ids.goblin_current}
  108. minetest.after(ght,clear_hud, params)
  109. ids.territory_current = player:hud_add({
  110. hud_elem_type = "text",
  111. position = {x = 1, y = 0.5},
  112. offset = {x = -120, y = 0},
  113. text = text_territory_current,
  114. alignment = -1,
  115. scale = { x = 50, y = 10},
  116. number = 0xFFFFFF,
  117. })
  118. local params = {player_name,ids.territory_current}
  119. minetest.after(ght,clear_hud, params)
  120. local tst_yo = 0
  121. for k,v in pairs(territory_score_table) do
  122. tst_yo = tst_yo + 20
  123. ids.territory_scores[k] = player:hud_add({
  124. hud_elem_type = "text",
  125. position = {x = 1, y = 0.5},
  126. offset = {x = -120, y = tst_yo},
  127. text = text_territory_scores[k],
  128. alignment = -1,
  129. scale = { x = 50, y = 10},
  130. number = 0xFFFFFF,
  131. })
  132. local params = {player_name,ids.territory_scores[k]}
  133. minetest.after(ght,clear_hud, params)
  134. end
  135. else
  136. local params = {player_name,ids.goblin_current}
  137. player:hud_change(ids["goblin_current"], "text", text_goblin_current)
  138. minetest.after(ght,clear_hud, params)
  139. local params = {player_name,ids.territory_current}
  140. player:hud_change(ids["territory_current"], "text", text_territory_current)
  141. minetest.after(ght,clear_hud, params)
  142. for k,v in pairs(territory_score_table) do
  143. local params = {player_name,ids.territory_scores[k]}
  144. minetest.after(ght,clear_hud, params)
  145. player:hud_change(ids.territory_scores[k],"text", text_territory_scores[k])
  146. end
  147. --player:hud_change(ids["bar_foreground"],
  148. --"scale", { x = percent, y = 1 })
  149. end
  150. end
  151. meta:set_int("hud_cleared",0)
  152. meta:set_int("hud_time_start",os.time())
  153. --minetest.after(ght, print,"ONE" )
  154. -- minetest.after(ght-1, print,"TWO" )
  155. -- minetest.after(ght-2, print,"THREE")
  156. end
  157. end
  158. minetest.register_on_joinplayer(goblins.update_hud)
  159. minetest.register_on_leaveplayer(function(player)
  160. saved_huds[player:get_player_name()] = nil
  161. end)