2024-02-13 12:49:28 +00:00
|
|
|
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():
|
2024-02-13 19:38:17 +00:00
|
|
|
DeathComponent.set_physics(owner, false)
|
|
|
|
hud.visible = false
|
2024-02-13 12:49:28 +00:00
|
|
|
animation.play("slime_death")
|
|
|
|
|
|
|
|
func revive():
|
|
|
|
health.restore()
|
2024-02-13 19:38:17 +00:00
|
|
|
DeathComponent.set_physics(owner, true)
|
|
|
|
hud.visible = true
|
2024-02-13 12:49:28 +00:00
|
|
|
(owner as Node3D).transform = Transform3D.IDENTITY
|
|
|
|
owner.global_position = respawn_position
|
|
|
|
animation.play("slime_idle")
|
|
|
|
|
2024-02-13 19:38:17 +00:00
|
|
|
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)
|