game: animate hp bars

This commit is contained in:
2025-08-20 06:26:22 +02:00
parent 6b70c0562f
commit dec0901b3d
3 changed files with 44 additions and 8 deletions

View File

@@ -47,9 +47,9 @@ func update_debug_stats() -> void:
for stat in debug_stats: for stat in debug_stats:
# Create margin container # Create margin container
var mc: MarginContainer = MarginContainer.new() var mc: MarginContainer = MarginContainer.new()
mc.add_theme_constant_override("margin_top", 10) mc.add_theme_constant_override("margin_top", 2)
mc.add_theme_constant_override("margin_left", 20) mc.add_theme_constant_override("margin_left", 20)
mc.add_theme_constant_override("margin_bottom", 10) mc.add_theme_constant_override("margin_bottom", 2)
mc.add_theme_constant_override("margin_right", 20) mc.add_theme_constant_override("margin_right", 20)
stats_container_vbox.add_child(mc) stats_container_vbox.add_child(mc)

View File

@@ -4,9 +4,42 @@ extends Control
@onready var hp_bar: ProgressBar = $CenterContainer/VBoxContainer/ProgressBar @onready var hp_bar: ProgressBar = $CenterContainer/VBoxContainer/ProgressBar
@onready var elapsed_label: Label = $CenterContainer/VBoxContainer/ElapsedLabel @onready var elapsed_label: Label = $CenterContainer/VBoxContainer/ElapsedLabel
const ANIM_SPEED = 4.0
const TRESHOLD = 1
func set_hp(value: float): var wanted_hp_value: float
hp_bar.value = value 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): func set_elapsed_time(value: float):

View File

@@ -15,7 +15,7 @@ var god_mode: bool = false
func _ready() -> void: func _ready() -> void:
camera.position = global_position camera.position = global_position
main_ui.player_ui.set_hp(100) main_ui.player_ui.update_hp()
GlobalConst.sig_debug_god_mode.connect(toggle_god_mode) GlobalConst.sig_debug_god_mode.connect(toggle_god_mode)
@@ -42,13 +42,16 @@ func take_damage(value: float) -> void:
player_stats.current_health = clampf( player_stats.current_health = clampf(
player_stats.current_health, -1.0, player_stats.get_final("max_health", modifiers) player_stats.current_health, -1.0, player_stats.get_final("max_health", modifiers)
) )
var current_hp = player_stats.get_final("current_health", modifiers)
var max_hp = player_stats.get_final("max_health", modifiers)
GlobalConst.sig_debug_stats_set.emit("current_health", "%f" % current_hp)
GlobalConst.sig_debug_stats_set.emit("max_hp", "%f" % max_hp)
var dm = preload("res://scenes/damage_numbers.tscn").instantiate() var dm = preload("res://scenes/damage_numbers.tscn").instantiate()
dm.damage_taken = value dm.damage_taken = value
dm.player_damage = false dm.player_damage = false
add_child(dm) add_child(dm)
main_ui.player_ui.set_hp( main_ui.player_ui.update_hp()
(player_stats.current_health / player_stats.get_final("max_health", modifiers)) * 100
)
if player_stats.current_health <= 0: if player_stats.current_health <= 0:
die() die()