44 lines
1.0 KiB
GDScript
44 lines
1.0 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():
|
|
set_alive(false)
|
|
animation.play("slime_death")
|
|
|
|
func revive():
|
|
health.restore()
|
|
set_alive(true)
|
|
(owner as Node3D).transform = Transform3D.IDENTITY
|
|
owner.global_position = respawn_position
|
|
animation.play("slime_idle")
|
|
|
|
|
|
func set_alive(alive: bool):
|
|
owner.propagate_call("set_process", [alive])
|
|
owner.propagate_call("set_physics_process", [alive])
|
|
owner.get_node("Collision").call_deferred("set_disabled", !alive)
|
|
hud.visible = alive
|