healthbar.gui_script 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. -- < 1 >
  2. local min_size = 48
  3. local max_size = 235 - min_size
  4. -- < 2 >
  5. local function set_healthbar(healthbar_node_name, health_percentage)
  6. local healthbar_node = gui.get_node(healthbar_node_name) -- < 3 >
  7. local healthbar_size = gui.get_size(healthbar_node) -- < 4 >
  8. healthbar_size.x = health_percentage * max_size + min_size -- < 5 >
  9. gui.set_size(healthbar_node, healthbar_size) -- < 6 >
  10. end
  11. function init(self)
  12. -- < 7 >
  13. set_healthbar("left_health", 1.0)
  14. set_healthbar("right_health", 1.0)
  15. set_healthbar("center_health", 1.0)
  16. end
  17. function on_message(self, message_id, message, sender)
  18. -- < 8 >
  19. if message_id == hash("update_health") then
  20. set_healthbar(message.health_name, message.health_percentage)
  21. end
  22. end
  23. --[[
  24. 1. Define minimum and maximum size of GUI healthbar (only width is changed).
  25. 2. Define a local helper function to update healthbar.
  26. 3. Get node of given name passed as "healthbar_node_name" and store it in local variable "healthbar_node".
  27. 4. Get size of this node and store it in local variable "healthbar_size".
  28. 5. Change size along X axis (width) of the node to given "health_percentage" scaled times "max_size" and added to "min_size", so that it can be no smaller than it.
  29. 6. Set the newly updated size of the node.
  30. 7. In init function, for each of three defined nodes set initial health_percentage to 1.0 (100%).
  31. 8. In on_message function, if the GUI component receives message "update_health" call helper function to update given health bar.
  32. ]]