statbars.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. local health_bar_definition =
  2. {
  3. hud_elem_type = "statbar",
  4. position = { x=0.5, y=1 },
  5. text = "heart.png",
  6. number = 20,
  7. direction = 0,
  8. size = { x=24, y=24 },
  9. offset = { x=(-10*24)-25, y=-(48+24+16)},
  10. }
  11. local breath_bar_definition =
  12. {
  13. hud_elem_type = "statbar",
  14. position = { x=0.5, y=1 },
  15. text = "bubble.png",
  16. number = 20,
  17. direction = 0,
  18. size = { x=24, y=24 },
  19. offset = {x=25,y=-(48+24+16)},
  20. }
  21. local hud_ids = {}
  22. local function initialize_builtin_statbars(player)
  23. if not player:is_player() then
  24. return
  25. end
  26. local name = player:get_player_name()
  27. if name == "" then
  28. return
  29. end
  30. if (hud_ids[name] == nil) then
  31. hud_ids[name] = {}
  32. -- flags are not transmitted to client on connect, we need to make sure
  33. -- our current flags are transmitted by sending them actively
  34. player:hud_set_flags(player:hud_get_flags())
  35. end
  36. if player:hud_get_flags().healthbar and
  37. core.is_yes(core.setting_get("enable_damage")) then
  38. if hud_ids[name].id_healthbar == nil then
  39. health_bar_definition.number = player:get_hp()
  40. hud_ids[name].id_healthbar = player:hud_add(health_bar_definition)
  41. end
  42. else
  43. if hud_ids[name].id_healthbar ~= nil then
  44. player:hud_remove(hud_ids[name].id_healthbar)
  45. hud_ids[name].id_healthbar = nil
  46. end
  47. end
  48. if (player:get_breath() < 11) then
  49. if player:hud_get_flags().breathbar and
  50. core.is_yes(core.setting_get("enable_damage")) then
  51. if hud_ids[name].id_breathbar == nil then
  52. hud_ids[name].id_breathbar = player:hud_add(breath_bar_definition)
  53. end
  54. else
  55. if hud_ids[name].id_breathbar ~= nil then
  56. player:hud_remove(hud_ids[name].id_breathbar)
  57. hud_ids[name].id_breathbar = nil
  58. end
  59. end
  60. elseif hud_ids[name].id_breathbar ~= nil then
  61. player:hud_remove(hud_ids[name].id_breathbar)
  62. hud_ids[name].id_breathbar = nil
  63. end
  64. end
  65. local function cleanup_builtin_statbars(player)
  66. if not player:is_player() then
  67. return
  68. end
  69. local name = player:get_player_name()
  70. if name == "" then
  71. return
  72. end
  73. hud_ids[name] = nil
  74. end
  75. local function player_event_handler(player,eventname)
  76. assert(player:is_player())
  77. local name = player:get_player_name()
  78. if name == "" then
  79. return
  80. end
  81. if eventname == "health_changed" then
  82. initialize_builtin_statbars(player)
  83. if hud_ids[name].id_healthbar ~= nil then
  84. player:hud_change(hud_ids[name].id_healthbar,"number",player:get_hp())
  85. return true
  86. end
  87. end
  88. if eventname == "breath_changed" then
  89. initialize_builtin_statbars(player)
  90. if hud_ids[name].id_breathbar ~= nil then
  91. player:hud_change(hud_ids[name].id_breathbar,"number",player:get_breath()*2)
  92. return true
  93. end
  94. end
  95. if eventname == "hud_changed" then
  96. initialize_builtin_statbars(player)
  97. return true
  98. end
  99. return false
  100. end
  101. function core.hud_replace_builtin(name, definition)
  102. if definition == nil or
  103. type(definition) ~= "table" or
  104. definition.hud_elem_type ~= "statbar" then
  105. return false
  106. end
  107. if name == "health" then
  108. health_bar_definition = definition
  109. for name,ids in pairs(hud_ids) do
  110. local player = core.get_player_by_name(name)
  111. if player and hud_ids[name].id_healthbar then
  112. player:hud_remove(hud_ids[name].id_healthbar)
  113. initialize_builtin_statbars(player)
  114. end
  115. end
  116. return true
  117. end
  118. if name == "breath" then
  119. breath_bar_definition = definition
  120. for name,ids in pairs(hud_ids) do
  121. local player = core.get_player_by_name(name)
  122. if player and hud_ids[name].id_breathbar then
  123. player:hud_remove(hud_ids[name].id_breathbar)
  124. initialize_builtin_statbars(player)
  125. end
  126. end
  127. return true
  128. end
  129. return false
  130. end
  131. core.register_on_joinplayer(initialize_builtin_statbars)
  132. core.register_on_leaveplayer(cleanup_builtin_statbars)
  133. core.register_playerevent(player_event_handler)