43 lines
1.1 KiB
GDScript

class_name DeathComponent
extends Node
@export var health: HealthComponent
@export var hud: HUDComponent
@export var animation: AnimationPlayer
@export var drop: PackedScene
@export var respawn_time: float = 5.0
var respawn_position: Vector3
func _ready():
respawn_position = owner.global_position
health.death.connect(on_death)
func on_death():
drop_loot()
die()
await get_tree().create_timer(respawn_time).timeout
revive()
func drop_loot():
var loot := drop.instantiate() as Node3D
owner.get_parent().add_child(loot)
loot.global_position = owner.global_position + loot.position
func die():
DeathComponent.set_physics(owner, false)
hud.visible = false
animation.play("slime_death")
func revive():
health.restore()
DeathComponent.set_physics(owner, true)
hud.visible = true
(owner as Node3D).transform = Transform3D.IDENTITY
owner.global_position = respawn_position
animation.play("slime_idle")
static func set_physics(obj: Node3D, alive: bool):
obj.propagate_call("set_process", [alive])
obj.propagate_call("set_physics_process", [alive])
obj.get_node("Collision").call_deferred("set_disabled", !alive)