diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-04-27 20:06:49 +0200 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-04-27 20:06:49 +0200 |
commit | 81cc6f1d22677dd4a60aa9377c91b291bf10e523 (patch) | |
tree | 9c22712a9746ed0598a44be6e88727d021436860 /Scenes/Entities/Enemies/Components/Health.gd | |
parent | c8a316e4b8987dad963656a34665fa0e23dedcb1 (diff) |
update
Diffstat (limited to 'Scenes/Entities/Enemies/Components/Health.gd')
-rw-r--r-- | Scenes/Entities/Enemies/Components/Health.gd | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Scenes/Entities/Enemies/Components/Health.gd b/Scenes/Entities/Enemies/Components/Health.gd new file mode 100644 index 0000000..f5e231e --- /dev/null +++ b/Scenes/Entities/Enemies/Components/Health.gd @@ -0,0 +1,39 @@ +extends Node + +class_name Component_Health + + +@export_node_path("CharacterBody2D") var entityPath: NodePath +@onready var entity: CharacterBody2D = get_node(entityPath) + +@export_node_path("AnimatedSprite2D") var spritePath: NodePath +@onready var sprite: AnimatedSprite2D = get_node(spritePath) + +@export var health: int + + +func take_damage(): + health -= 1 + + if health <= 0: + death() + + +func death(): + entity.set_physics_process(false) + + sprite.play("death") + await sprite.animation_finished + + sprite.play("death_post") + + var tween: Tween = get_tree().create_tween() + tween.tween_property( + entity, + "scale", + Vector2(0, 0), + 1 / sprite.sprite_frames.get_animation_speed("death_post") + ) + tween.tween_callback(func(): + entity.queue_free() + ) |