55 lines
1.6 KiB
GDScript
55 lines
1.6 KiB
GDScript
class_name PlayerUI
|
|
extends Control
|
|
|
|
@onready var hp_bar: ProgressBar = $CenterContainer/VBoxContainer/ProgressBar
|
|
@onready var elapsed_label: Label = $CenterContainer/VBoxContainer/ElapsedLabel
|
|
|
|
const ANIM_SPEED = 4.0
|
|
const TRESHOLD = 1
|
|
|
|
var wanted_hp_value: float
|
|
var wanted_hp_max: float
|
|
|
|
var player: Player
|
|
|
|
|
|
func _ready() -> void:
|
|
player = get_tree().get_first_node_in_group("player")
|
|
if player:
|
|
hp_bar.value = player.player_stats.current_health
|
|
hp_bar.max_value = player.player_stats.max_health
|
|
|
|
|
|
func update_hp():
|
|
if not player:
|
|
push_error("cant update hp, no player found")
|
|
var current_hp = player.player_stats.get_final("current_health", player.modifiers)
|
|
var max_hp = player.player_stats.get_final("max_health", player.modifiers)
|
|
wanted_hp_value = current_hp
|
|
wanted_hp_max = max_hp
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
if wanted_hp_max != hp_bar.max_value:
|
|
print_debug("wanted: %f-%f" % [wanted_hp_max, hp_bar.max_value])
|
|
hp_bar.max_value = lerpf(hp_bar.max_value, wanted_hp_max, ANIM_SPEED * delta)
|
|
if abs(hp_bar.max_value - wanted_hp_max) < TRESHOLD:
|
|
hp_bar.max_value = wanted_hp_max
|
|
if wanted_hp_value != hp_bar.value:
|
|
print_debug("current: %f-%f" % [wanted_hp_value, hp_bar.value])
|
|
hp_bar.value = lerpf(hp_bar.value, wanted_hp_value, ANIM_SPEED * delta)
|
|
if abs(hp_bar.value - wanted_hp_value) < TRESHOLD:
|
|
hp_bar.value = wanted_hp_value
|
|
|
|
|
|
func set_elapsed_time(value: float):
|
|
elapsed_label.text = format_time(value)
|
|
|
|
|
|
func format_time(seconds: float) -> String:
|
|
var total_ms = int(seconds * 1000.0)
|
|
var s = (total_ms / 1000) % 60
|
|
var m = total_ms / 60000
|
|
|
|
return "%02d:%02d" % [m, s]
|