statbars.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. -- cache setting
  2. local enable_damage = core.settings:get_bool("enable_damage")
  3. local health_bar_definition =
  4. {
  5. hud_elem_type = "statbar",
  6. position = { x=0.5, y=1 },
  7. text = "heart.png",
  8. number = 20,
  9. direction = 0,
  10. size = { x=24, y=24 },
  11. offset = { x=(-10*24)-25, y=-(48+24+16)},
  12. }
  13. local breath_bar_definition =
  14. {
  15. hud_elem_type = "statbar",
  16. position = { x=0.5, y=1 },
  17. text = "bubble.png",
  18. number = 20,
  19. direction = 0,
  20. size = { x=24, y=24 },
  21. offset = {x=25,y=-(48+24+16)},
  22. }
  23. local hud_ids = {}
  24. local function initialize_builtin_statbars(player)
  25. if not player:is_player() then
  26. return
  27. end
  28. local name = player:get_player_name()
  29. if name == "" then
  30. return
  31. end
  32. if (hud_ids[name] == nil) then
  33. hud_ids[name] = {}
  34. -- flags are not transmitted to client on connect, we need to make sure
  35. -- our current flags are transmitted by sending them actively
  36. player:hud_set_flags(player:hud_get_flags())
  37. end
  38. if player:hud_get_flags().healthbar and enable_damage then
  39. if hud_ids[name].id_healthbar == nil then
  40. health_bar_definition.number = player:get_hp()
  41. hud_ids[name].id_healthbar = player:hud_add(health_bar_definition)
  42. end
  43. else
  44. if hud_ids[name].id_healthbar ~= nil then
  45. player:hud_remove(hud_ids[name].id_healthbar)
  46. hud_ids[name].id_healthbar = nil
  47. end
  48. end
  49. if (player:get_breath() < 11) then
  50. if player:hud_get_flags().breathbar and 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)