Skip to content
Snippets Groups Projects

early platforming level

Merged Jack Kolm requested to merge early-platforming-level into main
Files
14
+ 111
6
@@ -4,9 +4,114 @@ const heart_full = preload("res://assets/hearts/hud_heart_full.png")
const heart_empty = preload("res://assets/hearts/hud_heart_empty.png")
#var heart_half = preload("res://assets/hearts/hud_heart_half.png")
func update_health(current_health : int) -> void:
for i in get_child_count():
if current_health > i:
get_child(i).texture = heart_full
else:
get_child(i).texture = heart_empty
const FADE_DURATION := 0.5 # Fade time in seconds
const FADE_FULL_OPACITY := 1.0
const FADE_LOW_OPACITY := 0.3
const SHOW_DURATION := 2.0 # Time to keep the UI visible after health change
const MAX_HEALTH := 5 # Ensure this matches the Player's MAX_HEALTH
var always_visible_due_to_low_health := false
var fade_tween: Tween = null
var scale_tween: Tween = null
var flash_tween: Tween = null
var original_position := position
var is_flashing := false
var fade_timer := Timer.new()
func _ready() -> void:
modulate.a = 0.0
add_child(fade_timer)
fade_timer.one_shot = true
func update_health(current_health: int) -> void:
for i in range(get_child_count()):
var child = get_child(i)
if child is TextureRect:
child.texture = heart_full if current_health > i else heart_empty
if current_health == MAX_HEALTH:
modulate = Color(1, 1, 1, 1)
fade_out()
elif current_health == 1:
show_ui(true)
start_flashing()
start_scaling_warning()
else:
modulate = Color(1, 1, 1, 1)
show_ui(false)
stop_flashing()
stop_scaling_warning()
pulse_ui()
func fade_out() -> void:
if fade_tween:
fade_tween.stop()
fade_tween = get_tree().create_tween()
fade_tween.tween_property(self, "modulate:a", 0.0, FADE_DURATION)
always_visible_due_to_low_health = false
func show_ui(permanent: bool = false) -> void:
if fade_tween:
fade_tween.stop()
modulate.a = FADE_FULL_OPACITY
if permanent:
always_visible_due_to_low_health = true
else:
always_visible_due_to_low_health = false
fade_timer.start(SHOW_DURATION)
fade_timer.timeout.connect(fade_to_low_opacity)
func fade_to_low_opacity() -> void:
if not always_visible_due_to_low_health:
if fade_tween:
fade_tween.stop()
fade_tween = get_tree().create_tween()
fade_tween.tween_property(self, "modulate:a", FADE_LOW_OPACITY, FADE_DURATION)
func pulse_ui() -> void:
if scale_tween:
scale_tween.stop()
scale_tween = get_tree().create_tween()
scale_tween.tween_property(self, "scale", Vector2(1.2, 1.2), 0.1)
scale_tween.tween_property(self, "scale", Vector2(1, 1), 0.1)
func start_flashing() -> void:
if is_flashing:
return
is_flashing = true
continue_flashing()
func continue_flashing() -> void:
if not is_flashing:
return
if flash_tween:
flash_tween.stop()
flash_tween = get_tree().create_tween()
flash_tween.tween_property(self, "modulate", Color(1, 0.2, 0.2), 0.1)
flash_tween.tween_property(self, "modulate", Color(1, 1, 1), 0.1)
flash_tween.finished.connect(continue_flashing)
func stop_flashing() -> void:
is_flashing = false
if flash_tween:
flash_tween.stop()
func start_scaling_warning() -> void:
if scale_tween:
scale_tween.stop()
continue_scaling_warning()
func continue_scaling_warning() -> void:
if scale_tween:
scale_tween.stop()
scale_tween = get_tree().create_tween()
scale_tween.tween_property(self, "scale", Vector2(1.2, 1.2), 0.5)
scale_tween.tween_property(self, "scale", Vector2(1.0, 1.0), 0.5)
scale_tween.finished.connect(continue_scaling_warning)
func stop_scaling_warning() -> void:
if scale_tween:
scale_tween.stop()
Loading