class_name ComponentHealth extends Node signal died @export_node_path("CharacterBody2D") var entityPath: NodePath @onready var entity: CharacterBody2D = get_node(entityPath) @export_node_path("AnimatedSprite2D") var spritePath: NodePath @onready var sprite: AnimatedSprite2D = get_node(spritePath) @export_node_path("ComponentMovement") var component_movement_path: NodePath var component_movement: ComponentMovement @export var health: int @export var drops_items: bool = true @export_range(1, 100) var drop_chance: int = 30 var drop_items = [ preload("res://Scenes/Entities/Objects/Coin.tscn"), ] func _ready(): if component_movement_path: component_movement = get_node(component_movement_path) func take_damage(): health -= 1 if health <= 0: death() func death(): emit_signal("died") if component_movement: # stop all movement component_movement.process_mode = Node.PROCESS_MODE_DISABLED entity.set_physics_process(false) sprite.play("death") await sprite.animation_finished assert(sprite.sprite_frames.has_animation("death_post")) sprite.play("death_post") var tween: Tween = get_tree().create_tween() tween.tween_property( entity, "scale", Vector2(0, 0), 1 / sprite.sprite_frames.get_animation_speed("death_post") ) tween.tween_callback(func(): if drops_items and randi_range(1, 100) <= drop_chance: call_deferred("drop_item") entity.queue_free() ) func drop_item(): var item = drop_items.pick_random().instantiate() item.position = entity.position get_tree().current_scene.add_child(item)