63 lines
1.7 KiB
GDScript
63 lines
1.7 KiB
GDScript
extends Node2D
|
|
|
|
## Floating damage number that appears above entities when they take damage
|
|
|
|
var damage_amount: int = 0
|
|
var damage_color: Color = Color.RED
|
|
var lifetime: float = 2.0
|
|
var elapsed: float = 0.0
|
|
|
|
@onready var label: Label = null
|
|
|
|
func _ready():
|
|
# Create label
|
|
label = Label.new()
|
|
label.name = "Label"
|
|
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
|
|
add_child(label)
|
|
|
|
# Set label properties
|
|
label.add_theme_font_size_override("font_size", 18)
|
|
label.add_theme_color_override("font_color", damage_color)
|
|
label.add_theme_color_override("font_shadow_color", Color.BLACK)
|
|
label.add_theme_color_override("font_outline_color", Color.BLACK)
|
|
label.add_theme_constant_override("shadow_offset_x", 2)
|
|
label.add_theme_constant_override("shadow_offset_y", 2)
|
|
label.add_theme_constant_override("outline_size", 4)
|
|
label.text = str(damage_amount)
|
|
|
|
# Start above the entity
|
|
position.y -= 20
|
|
|
|
func _process(delta):
|
|
elapsed += delta
|
|
|
|
# Float upward
|
|
position.y -= 30 * delta
|
|
|
|
# Fade out over lifetime
|
|
var alpha = 1.0 - (elapsed / lifetime)
|
|
if label:
|
|
var color_with_alpha = damage_color
|
|
color_with_alpha.a = alpha
|
|
label.add_theme_color_override("font_color", color_with_alpha)
|
|
|
|
var shadow_color = Color.BLACK
|
|
shadow_color.a = alpha
|
|
label.add_theme_color_override("font_shadow_color", shadow_color)
|
|
label.add_theme_color_override("font_outline_color", shadow_color)
|
|
|
|
# Remove when lifetime expires
|
|
if elapsed >= lifetime:
|
|
queue_free()
|
|
|
|
func initialize(dmg: int, color: Color = Color.RED):
|
|
damage_amount = dmg
|
|
damage_color = color
|
|
|
|
# Update the label if it exists
|
|
if label:
|
|
label.text = str(damage_amount)
|
|
label.add_theme_color_override("font_color", damage_color)
|