statbars.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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+10)},
  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+10)},
  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. end
  33. if player:hud_get_flags().healthbar and
  34. core.is_yes(core.setting_get("enable_damage")) then
  35. if hud_ids[name].id_healthbar == nil then
  36. health_bar_definition.number = player:get_hp()
  37. hud_ids[name].id_healthbar = player:hud_add(health_bar_definition)
  38. end
  39. else
  40. if hud_ids[name].id_healthbar ~= nil then
  41. player:hud_remove(hud_ids[name].id_healthbar)
  42. hud_ids[name].id_healthbar = nil
  43. end
  44. end
  45. if (player:get_breath() < 11) then
  46. if player:hud_get_flags().breathbar and
  47. core.is_yes(core.setting_get("enable_damage")) then
  48. if hud_ids[name].id_breathbar == nil then
  49. hud_ids[name].id_breathbar = player:hud_add(breath_bar_definition)
  50. end
  51. else
  52. if hud_ids[name].id_breathbar ~= nil then
  53. player:hud_remove(hud_ids[name].id_breathbar)
  54. hud_ids[name].id_breathbar = nil
  55. end
  56. end
  57. elseif hud_ids[name].id_breathbar ~= nil then
  58. player:hud_remove(hud_ids[name].id_breathbar)
  59. hud_ids[name].id_breathbar = nil
  60. end
  61. end
  62. local function cleanup_builtin_statbars(player)
  63. if not player:is_player() then
  64. return
  65. end
  66. local name = player:get_player_name()
  67. if name == "" then
  68. return
  69. end
  70. hud_ids[name] = nil
  71. end
  72. local function player_event_handler(player,eventname)
  73. assert(player:is_player())
  74. local name = player:get_player_name()
  75. if name == "" then
  76. return
  77. end
  78. if eventname == "health_changed" then
  79. initialize_builtin_statbars(player)
  80. if hud_ids[name].id_healthbar ~= nil then
  81. player:hud_change(hud_ids[name].id_healthbar,"number",player:get_hp())
  82. return true
  83. end
  84. end
  85. if eventname == "breath_changed" then
  86. initialize_builtin_statbars(player)
  87. if hud_ids[name].id_breathbar ~= nil then
  88. player:hud_change(hud_ids[name].id_breathbar,"number",player:get_breath()*2)
  89. return true
  90. end
  91. end
  92. if eventname == "hud_changed" then
  93. initialize_builtin_statbars(player)
  94. return true
  95. end
  96. return false
  97. end
  98. function core.hud_replace_builtin(name, definition)
  99. if definition == nil or
  100. type(definition) ~= "table" or
  101. definition.hud_elem_type ~= "statbar" then
  102. return false
  103. end
  104. if name == "health" then
  105. health_bar_definition = definition
  106. for name,ids in pairs(hud_ids) do
  107. local player = core.get_player_by_name(name)
  108. if player and hud_ids[name].id_healthbar then
  109. player:hud_remove(hud_ids[name].id_healthbar)
  110. initialize_builtin_statbars(player)
  111. end
  112. end
  113. return true
  114. end
  115. if name == "breath" then
  116. breath_bar_definition = definition
  117. for name,ids in pairs(hud_ids) do
  118. local player = core.get_player_by_name(name)
  119. if player and hud_ids[name].id_breathbar then
  120. player:hud_remove(hud_ids[name].id_breathbar)
  121. initialize_builtin_statbars(player)
  122. end
  123. end
  124. return true
  125. end
  126. return false
  127. end
  128. core.register_on_joinplayer(initialize_builtin_statbars)
  129. core.register_on_leaveplayer(cleanup_builtin_statbars)
  130. core.register_playerevent(player_event_handler)