healthbar.script 1.2 KB

12345678910111213141516171819202122232425
  1. function init(self)
  2. -- < 1 >
  3. self.player_one_health = 1.0
  4. self.player_two_health = 1.0
  5. self.game_boss_health = 1.0
  6. -- < 2 >
  7. timer.delay(1, true, function()
  8. -- < 3 >
  9. self.player_one_health = math.max(self.player_one_health - 0.1, 0)
  10. self.player_two_health = math.max(self.player_two_health - 0.1, 0)
  11. self.game_boss_health = math.max(self.game_boss_health - 0.1, 0)
  12. -- < 4 >
  13. msg.post("hud", "update_health", { health_name = "left_health", health_percentage = self.player_one_health })
  14. msg.post("hud", "update_health", { health_name = "right_health", health_percentage = self.player_two_health })
  15. msg.post("hud", "update_health", { health_name = "center_health", health_percentage = self.game_boss_health })
  16. end)
  17. end
  18. --[[
  19. 1. Set initial health percentage (1.0 = 100%, 0.0 = 0%).
  20. 2. Start a timer that will call every 1 second (first argument) repeateadly (second argument being true) a callback function (3rd argument)
  21. 3. Reduce each health percentage by 0.1 (10%), but no less than 0 (using math.max to select `0`, if `self.player_one_health - 0.1` is less than `0`).
  22. 4. Send messages to hud (gui component) to "updated_health" with health name and percentage to be set in GUI script.
  23. ]]