MacIcyEngine

GDScript ui_healthbar

Oct 31st, 2024
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends ProgressBar
  2.  
  3. @export var start_hp: int = 10
  4. @export var invulnerable_duration: float = 1.0
  5. @export var full_hp_color: Color
  6. @export var empty_hp_color: Color
  7.  
  8. var stylebox: StyleBoxFlat
  9. var invulnerable_timer: float = 0
  10.  
  11. func _init() -> void:
  12.     stylebox = get_theme_stylebox("fill") as StyleBoxFlat
  13.  
  14. func _enter_tree() -> void:
  15.     EventHandler.on_player_take_damage.connect(take_damage)
  16.  
  17. func _exit_tree() -> void:
  18.     EventHandler.on_player_take_damage.disconnect(take_damage)
  19.  
  20. func _ready() -> void:
  21.     max_value = start_hp
  22.     value = start_hp
  23.     update_hp_bar()
  24.  
  25. func _process(delta: float) -> void:
  26.     if invulnerable_timer > 0:
  27.         invulnerable_timer -= delta
  28.  
  29. func take_damage(damage: int) -> void:
  30.     if invulnerable_timer <= 0:
  31.         value -= damage
  32.         update_hp_bar()
  33.  
  34.         invulnerable_timer = invulnerable_duration
  35.  
  36.         if value <= 0:
  37.             EventHandler.on_player_death
  38.  
  39. func update_hp_bar() -> void:
  40.     if value:
  41.         stylebox.bg_color = lerp(empty_hp_color, full_hp_color, value / max_value)
  42.  
Advertisement
Add Comment
Please, Sign In to add comment