43 lines
1.1 KiB
GDScript3
Raw Normal View History

2024-02-13 12:49:28 +00:00
class_name DeathComponent
2024-02-27 20:05:55 +00:00
extends CharacterComponent
2024-02-13 12:49:28 +00:00
@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():
2024-02-27 20:05:55 +00:00
respawn_position = character.global_position
2024-02-13 12:49:28 +00:00
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
2024-02-27 20:05:55 +00:00
character.get_parent().add_child(loot)
loot.global_position = character.global_position + loot.position
2024-02-13 12:49:28 +00:00
func die():
2024-02-27 20:05:55 +00:00
DeathComponent.set_physics(character, false)
2024-02-13 19:38:17 +00:00
hud.visible = false
2024-02-13 12:49:28 +00:00
animation.play("slime_death")
func revive():
health.restore()
2024-02-27 20:05:55 +00:00
DeathComponent.set_physics(character, true)
2024-02-13 19:38:17 +00:00
hud.visible = true
2024-02-27 20:05:55 +00:00
character.transform = Transform3D.IDENTITY
character.global_position = respawn_position
2024-02-13 12:49:28 +00:00
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)