api.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. -- global values
  2. hud.registered_items = {}
  3. hud.damage_events = {}
  4. hud.breath_events = {}
  5. -- Localize for performance.
  6. local math_floor = math.floor
  7. -- keep id handling internal
  8. local hud_id = {} -- hud item ids
  9. local sb_bg = {} -- statbar background ids
  10. -- localize often used table
  11. local items = hud.registered_items
  12. local function throw_error(msg)
  13. minetest.log("error", "Better HUD[error]: " .. msg)
  14. end
  15. --------------------------------------------------------------------------------
  16. -- API
  17. --------------------------------------------------------------------------------
  18. function hud.register(name, def)
  19. if not name or not def then
  20. throw_error("Not enough parameters given")
  21. return false
  22. end
  23. --TODO: allow other elements
  24. if def.hud_elem_type ~= "statbar" then
  25. throw_error("The given HUD element is not a statbar")
  26. return false
  27. end
  28. if items[name] ~= nil then
  29. throw_error("A statbar with that name already exists")
  30. return false
  31. end
  32. -- Actually register
  33. -- Add background first since draworder is based on id.
  34. if def.hud_elem_type == "statbar" and def.background ~= nil then
  35. sb_bg[name] = table.copy(def)
  36. sb_bg[name].text = def.background
  37. sb_bg[name].number = 20
  38. end
  39. -- add item itself
  40. items[name] = def
  41. -- register events
  42. if def.events then
  43. for _, v in pairs(def.events) do
  44. if v and v.type and v.func then
  45. if v.type == "damage" then
  46. table.insert(hud.damage_events, v)
  47. end
  48. if v.type == "breath" then
  49. table.insert(hud.breath_events, v)
  50. end
  51. end
  52. end
  53. end
  54. -- no error so far, return sucess
  55. return true
  56. end
  57. function hud.change_item(player, name, def)
  58. if not player or not player:is_player() or not name or not def then
  59. throw_error("Not enough parameters given to change HUD item")
  60. return false
  61. end
  62. local i_name = player:get_player_name().."_"..name
  63. local elem = hud_id[i_name]
  64. if not elem then
  65. --throw_error("Given HUD element " .. dump(name) .. " does not exist")
  66. return false
  67. end
  68. -- Update supported values (currently number and text only)
  69. if def.number and elem.number then
  70. elem.number = math_floor((def.number / def.max) * 20)
  71. player:hud_change(elem.id, "number", elem.number)
  72. end
  73. if def.text and elem.text then
  74. player:hud_change(elem.id, "text", def.text)
  75. elem.text = def.text
  76. end
  77. if def.offset and elem.offset then
  78. player:hud_change(elem.id, "offset", def.offset)
  79. elem.offset = def.offset
  80. end
  81. return true
  82. end
  83. function hud.remove_item(player, name)
  84. if not player or not name then
  85. throw_error("Not enough parameters given")
  86. return false
  87. end
  88. local i_name = player:get_player_name() .. "_" .. name
  89. if hud_id[i_name] == nil then
  90. --throw_error("Given HUD element " .. dump(name) .. " does not exist")
  91. return false
  92. end
  93. player:hud_remove(hud_id[i_name].id)
  94. hud_id[i_name] = nil
  95. return true
  96. end
  97. --------------------------------------------------------------------------------
  98. -- Add registered HUD items to joining players
  99. --------------------------------------------------------------------------------
  100. -- Following code is placed here to keep HUD ids internal.
  101. local function add_hud_item(player, name, def)
  102. if not player or not name or not def then
  103. throw_error("not enough parameters given")
  104. return false
  105. end
  106. local i_name = player:get_player_name() .. "_" .. name
  107. hud_id[i_name] = def
  108. hud_id[i_name].id = player:hud_add(def)
  109. end
  110. minetest.register_on_joinplayer(function(player)
  111. -- First: hide the default statbars.
  112. local hud_flags = player:hud_get_flags()
  113. hud_flags.healthbar = false
  114. hud_flags.breathbar = false
  115. player:hud_set_flags(hud_flags)
  116. -- Now add the backgrounds for statbars.
  117. for _, item in pairs(sb_bg) do
  118. add_hud_item(player, _ .. "_bg", item)
  119. end
  120. -- And finally the actual HUD items.
  121. for _, item in pairs(items) do
  122. add_hud_item(player, _, item)
  123. end
  124. end)