hud.lua 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. --[[
  2. HUD definitions for Thirsty
  3. Optionally from one of the supported mods
  4. Any hud needs to define the following functions:
  5. - thirsty.hud_init(player)
  6. Initialize the HUD for a new player.
  7. - thirsty.hud_update(player, value)
  8. Display the new value "value" for the given player. "value" is
  9. a floating point number, not necessarily bounded. You can use the
  10. "thirsty.hud_clamp(value)" function to get an integer between 0
  11. and 20.
  12. ]]
  13. local PPA = thirsty.persistent_player_attributes
  14. function thirsty.hud_clamp(value)
  15. if value < 0 then
  16. return 0
  17. elseif value > 20 then
  18. return 20
  19. else
  20. return math.ceil(value)
  21. end
  22. end
  23. if minetest.get_modpath("hudbars") then
  24. hb.register_hudbar('thirst', 0xffffff, "Hydration", {
  25. bar = 'thirsty_hudbars_bar.png',
  26. icon = 'thirsty_cup_100_16.png'
  27. }, 20, 20, false)
  28. function thirsty.hud_init(player)
  29. hb.init_hudbar(player, 'thirst',
  30. thirsty.hud_clamp(PPA.get_value(player, 'thirsty_hydro')),
  31. 20, false)
  32. end
  33. function thirsty.hud_update(player, value)
  34. hb.change_hudbar(player, 'thirst', thirsty.hud_clamp(value), 20)
  35. end
  36. elseif minetest.get_modpath("hud") then
  37. -- default positions follow [hud] defaults
  38. local position = HUD_THIRST_POS or { x=0.5, y=1 }
  39. local offset = HUD_THIRST_OFFSET or { x=15, y=-133} -- above AIR
  40. hud.register('thirst', {
  41. hud_elem_type = "statbar",
  42. position = position,
  43. text = "thirsty_cup_100_24.png",
  44. background = "thirsty_cup_0_24.png",
  45. number = 20,
  46. max = 20,
  47. size = HUD_SB_SIZE, -- by default { x=24, y=24 },
  48. offset = offset,
  49. })
  50. function thirsty.hud_init(player)
  51. -- automatic by [hud]
  52. end
  53. function thirsty.hud_update(player, value)
  54. hud.change_item(player, 'thirst', {
  55. number = thirsty.hud_clamp(value)
  56. })
  57. end
  58. else
  59. -- 'builtin' hud
  60. function thirsty.hud_init(player)
  61. -- above breath bar, for now
  62. local name = player:get_player_name()
  63. thirsty.players[name].hud_id = player:hud_add({
  64. hud_elem_type = "statbar",
  65. position = { x=0.5, y=1 },
  66. text = "thirsty_cup_100_24.png",
  67. number = thirsty.hud_clamp(PPA.get_value(player, 'thirsty_hydro')),
  68. direction = 0,
  69. size = { x=24, y=24 },
  70. offset = { x=25, y=-(48+24+16+32)},
  71. })
  72. end
  73. function thirsty.hud_update(player, value)
  74. local name = player:get_player_name()
  75. local hud_id = thirsty.players[name].hud_id
  76. player:hud_change(hud_id, 'number', thirsty.hud_clamp(value))
  77. end
  78. end