Improved respawn

This commit is contained in:
Eduard Urbach 2024-02-13 13:49:28 +01:00
parent bbef2a0a4a
commit 4c1d8ab877
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
7 changed files with 75 additions and 25 deletions

View File

@ -0,0 +1,43 @@
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

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://bfvudkup0xsq4"]
[ext_resource type="Script" path="res://enemy/death/DeathComponent.gd" id="1_5qhry"]
[node name="Death" type="Node"]
script = ExtResource("1_5qhry")

View File

@ -1,15 +0,0 @@
class_name LootComponent
extends Node
@export var health: HealthComponent
@export var drop: PackedScene
func _ready():
health.death.connect(on_death)
func on_death():
prints(owner.name, "death")
var loot := drop.instantiate() as Node3D
owner.get_parent().add_child(loot)
loot.global_position = owner.global_position + loot.position
owner.queue_free()

View File

@ -1,6 +0,0 @@
[gd_scene load_steps=2 format=3 uid="uid://bfvudkup0xsq4"]
[ext_resource type="Script" path="res://enemy/loot/LootComponent.gd" id="1_5qhry"]
[node name="Loot" type="Node"]
script = ExtResource("1_5qhry")

View File

@ -1,11 +1,11 @@
[gd_scene load_steps=13 format=3 uid="uid://cb2t7bvvf3gwh"]
[gd_scene load_steps=14 format=3 uid="uid://cb2t7bvvf3gwh"]
[ext_resource type="PackedScene" uid="uid://b358op5h1y83m" path="res://assets/slime/Slime.blend" id="1_1h1hj"]
[ext_resource type="Script" path="res://enemy/Enemy.gd" id="1_r5888"]
[ext_resource type="PackedScene" uid="uid://2bbycjulf00g" path="res://player/health/HealthComponent.tscn" id="2_fsqxc"]
[ext_resource type="PackedScene" uid="uid://x102pryt2s5a" path="res://player/movement/MovementComponent.tscn" id="3_2phqx"]
[ext_resource type="PackedScene" uid="uid://6jpnl6c4fdvo" path="res://player/hud/HUDComponent.tscn" id="3_4jtio"]
[ext_resource type="PackedScene" uid="uid://bfvudkup0xsq4" path="res://enemy/loot/LootComponent.tscn" id="6_68kgw"]
[ext_resource type="PackedScene" uid="uid://bfvudkup0xsq4" path="res://enemy/death/DeathComponent.tscn" id="6_68kgw"]
[ext_resource type="PackedScene" uid="uid://dl4vcp04t8iyr" path="res://item/soul/Soul.tscn" id="7_i0rtb"]
[ext_resource type="Script" path="res://enemy/EnemyController.gd" id="8_b7r6l"]
@ -13,6 +13,16 @@
height = 0.5
radius = 0.35
[sub_resource type="Animation" id="Animation_vjs0t"]
resource_name = "slime_death"
tracks/0/type = "scale_3d"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("..")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = PackedFloat32Array(0, 1, 1, 1, 1, 1, 1, 1.1, 0.7, 1.1)
[sub_resource type="Animation" id="Animation_qls31"]
resource_name = "slime_fall"
tracks/0/type = "scale_3d"
@ -37,6 +47,7 @@ tracks/0/keys = PackedFloat32Array(0, 1, 1, 1, 1, 0.5, 1, 0.9, 1.1, 0.9, 1, 1, 1
[sub_resource type="AnimationLibrary" id="AnimationLibrary_1bw8m"]
_data = {
"slime_death": SubResource("Animation_vjs0t"),
"slime_fall": SubResource("Animation_qls31"),
"slime_idle": SubResource("Animation_mdtm7")
}
@ -69,8 +80,10 @@ max_value = 100.0
[node name="Movement" parent="." instance=ExtResource("3_2phqx")]
move_speed = 0.5
[node name="Loot" parent="." node_paths=PackedStringArray("health") instance=ExtResource("6_68kgw")]
[node name="Death" parent="." node_paths=PackedStringArray("health", "hud", "animation") instance=ExtResource("6_68kgw")]
health = NodePath("../Health")
hud = NodePath("../HUD")
animation = NodePath("../Animation")
drop = ExtResource("7_i0rtb")
[node name="EnemyController" type="Node" parent="."]

View File

@ -8,14 +8,23 @@ const DEATH_THRESHOLD = 0.01
@export var max_value: float
var value: float
var is_dead: bool
func _ready():
restore()
func restore():
is_dead = false
value = max_value
value_changed.emit()
func take_damage(attack: DamageInstance):
if is_dead:
return
value = clampf(value - attack.damage, 0, max_value)
value_changed.emit()
if value < DEATH_THRESHOLD:
is_dead = true
death.emit()

View File

@ -4,4 +4,4 @@ func on_body_entered(body: Node3D):
var health := body.get_node_or_null("Health") as HealthComponent
if health:
health.take_damage(DamageInstance.new(10))
health.take_damage(DamageInstance.new(50))