Files
DungeonsOfKharadum/src/scripts/damage_number.gd

51 lines
2.0 KiB
GDScript

extends Label
@export var label: String = "1"
@export var color := Color.RED # Red color for damage numbers
@export var direction := Vector2.ZERO # Default direction (will be random if not set)
var fade_delay := 0.6 # When to start fading (display duration)
var move_duration := 1.0 # Total animation duration (includes fade)
var fade_duration := 0.4 # Time to fade out (after fade_delay)
var rise_distance: float = 20.0 # Distance to move upward
var stretch_amount := Vector2(1, 1.4) # How much to stretch the sprite
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
call_deferred("_initialize_damage_number")
pass # Replace with function body.
func _initialize_damage_number() -> void:
# Set color (red by default) and text
self.modulate = color
self.text = label
# If direction is not set, use a random upward direction with slight variation
if direction == Vector2.ZERO:
var random_angle = randf_range(-PI/6, PI/6) # ±30 degrees from straight up
direction = Vector2(sin(random_angle), -cos(random_angle)) # Mostly upward with slight variation
# Calculate target position (move upward with slight horizontal variation)
var move_target = global_position + (direction.normalized() * rise_distance)
# Total animation duration = display (0.6s) + fade (0.4s) = 1.0s
var total_duration = fade_delay + fade_duration # 0.6 + 0.4 = 1.0s
# Create tween for movement (entire duration, continues during fade)
var move_tween = create_tween()
move_tween.set_trans(Tween.TRANS_CUBIC)
move_tween.set_ease(Tween.EASE_OUT)
move_tween.tween_property(self, "global_position", move_target, total_duration)
# Wait for display duration (0.6s), then start fading
await get_tree().create_timer(fade_delay).timeout
# Fade out over fade_duration (0.4s) while still moving
var fade_tween = create_tween()
fade_tween.tween_property(self, "modulate:a", 0.0, fade_duration)
# Wait for fade to complete, then remove
await fade_tween.finished
queue_free()
pass