statbars.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 = core.PLAYER_MAX_HP_DEFAULT,
  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 = core.PLAYER_MAX_BREATH_DEFAULT,
  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 scaleToDefault(player, field)
  25. -- Scale "hp" or "breath" to the default dimensions
  26. local current = player["get_" .. field](player)
  27. local nominal = core["PLAYER_MAX_".. field:upper() .. "_DEFAULT"]
  28. local max_display = math.max(nominal,
  29. math.max(player:get_properties()[field .. "_max"], current))
  30. return current / max_display * nominal
  31. end
  32. local function initialize_builtin_statbars(player)
  33. if not player:is_player() then
  34. return
  35. end
  36. local name = player:get_player_name()
  37. if name == "" then
  38. return
  39. end
  40. if not hud_ids[name] then
  41. hud_ids[name] = {}
  42. -- flags are not transmitted to client on connect, we need to make sure
  43. -- our current flags are transmitted by sending them actively
  44. player:hud_set_flags(player:hud_get_flags())
  45. end
  46. local hud = hud_ids[name]
  47. if player:hud_get_flags().healthbar and enable_damage then
  48. if hud.id_healthbar == nil then
  49. local hud_def = table.copy(health_bar_definition)
  50. hud_def.number = scaleToDefault(player, "hp")
  51. hud.id_healthbar = player:hud_add(hud_def)
  52. end
  53. elseif hud.id_healthbar ~= nil then
  54. player:hud_remove(hud.id_healthbar)
  55. hud.id_healthbar = nil
  56. end
  57. local breath_max = player:get_properties().breath_max
  58. if player:hud_get_flags().breathbar and enable_damage and
  59. player:get_breath() < breath_max then
  60. if hud.id_breathbar == nil then
  61. local hud_def = table.copy(breath_bar_definition)
  62. hud_def.number = 2 * scaleToDefault(player, "breath")
  63. hud.id_breathbar = player:hud_add(hud_def)
  64. end
  65. elseif hud.id_breathbar ~= nil then
  66. player:hud_remove(hud.id_breathbar)
  67. hud.id_breathbar = nil
  68. end
  69. end
  70. local function cleanup_builtin_statbars(player)
  71. if not player:is_player() then
  72. return
  73. end
  74. local name = player:get_player_name()
  75. if name == "" then
  76. return
  77. end
  78. hud_ids[name] = nil
  79. end
  80. local function player_event_handler(player,eventname)
  81. assert(player:is_player())
  82. local name = player:get_player_name()
  83. if name == "" then
  84. return
  85. end
  86. if eventname == "health_changed" then
  87. initialize_builtin_statbars(player)
  88. if hud_ids[name].id_healthbar ~= nil then
  89. player:hud_change(hud_ids[name].id_healthbar,
  90. "number", scaleToDefault(player, "hp"))
  91. return true
  92. end
  93. end
  94. if eventname == "breath_changed" then
  95. initialize_builtin_statbars(player)
  96. if hud_ids[name].id_breathbar ~= nil then
  97. player:hud_change(hud_ids[name].id_breathbar,
  98. "number", 2 * scaleToDefault(player, "breath"))
  99. return true
  100. end
  101. end
  102. if eventname == "hud_changed" then
  103. initialize_builtin_statbars(player)
  104. return true
  105. end
  106. return false
  107. end
  108. function core.hud_replace_builtin(name, definition)
  109. if definition == nil or
  110. type(definition) ~= "table" or
  111. definition.hud_elem_type ~= "statbar" then
  112. return false
  113. end
  114. if name == "health" then
  115. health_bar_definition = definition
  116. for name,ids in pairs(hud_ids) do
  117. local player = core.get_player_by_name(name)
  118. if player and hud_ids[name].id_healthbar then
  119. player:hud_remove(hud_ids[name].id_healthbar)
  120. initialize_builtin_statbars(player)
  121. end
  122. end
  123. return true
  124. end
  125. if name == "breath" then
  126. breath_bar_definition = definition
  127. for name,ids in pairs(hud_ids) do
  128. local player = core.get_player_by_name(name)
  129. if player and hud_ids[name].id_breathbar then
  130. player:hud_remove(hud_ids[name].id_breathbar)
  131. initialize_builtin_statbars(player)
  132. end
  133. end
  134. return true
  135. end
  136. return false
  137. end
  138. core.register_on_joinplayer(initialize_builtin_statbars)
  139. core.register_on_leaveplayer(cleanup_builtin_statbars)
  140. core.register_playerevent(player_event_handler)