init.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. -- gauges: Adds health/breath bars above players
  2. --
  3. -- Copyright © 2014-2020 4aiman, Hugo Locurcio and contributors - MIT License
  4. -- See `LICENSE.md` included in the source distribution for details.
  5. if minetest.settings:get_bool("health_bars") == false or
  6. not minetest.settings:get_bool("enable_damage")
  7. then return end
  8. -- Localize the vector distance function for better performance, as it's called
  9. -- on every step
  10. local vector_distance = vector.distance
  11. minetest.register_entity("gauges:hp_bar", {
  12. visual = "sprite",
  13. visual_size = {x=1, y=1/16, z=1},
  14. -- The texture is changed later in the code
  15. textures = {"blank.png"},
  16. collisionbox = {0},
  17. physical = false,
  18. on_step = function(self)
  19. local player = self.wielder
  20. local gauge = self.object
  21. if not player or
  22. not minetest.is_player(player) or
  23. vector_distance(player:get_pos(), gauge:get_pos()) > 3
  24. then
  25. gauge:remove()
  26. return
  27. end
  28. local hp = player:get_hp() <= 20 and player:get_hp() or 20
  29. local breath = player:get_breath() <= 10 and player:get_breath() or 11
  30. if self.hp ~= hp or self.breath ~= breath then
  31. local hp_tex = hp < 20 and "health_"..tostring(hp)..".png" or "blank.png"
  32. local breath_tex = breath < 10 and "breath_"..tostring(breath)..".png" or "blank.png"
  33. gauge:set_properties({
  34. textures = {
  35. hp_tex .. "^" .. breath_tex
  36. }
  37. })
  38. self.hp = hp
  39. self.breath = breath
  40. end
  41. end
  42. })
  43. local function add_gauge(player)
  44. if player and minetest.is_player(player) then
  45. local entity = minetest.add_entity(player:get_pos(), "gauges:hp_bar")
  46. -- check for minetest_game 0.4.*
  47. local height = minetest.get_modpath("player_api") and 19 or 9
  48. entity:set_attach(player, "", {x=0, y=height, z=0}, {x=0, y=0, z=0})
  49. entity:get_luaentity().wielder = player
  50. end
  51. end
  52. minetest.register_on_joinplayer(function(player)
  53. minetest.after(1, add_gauge, player)
  54. end)