32 lines
980 B
GDScript
32 lines
980 B
GDScript
extends Node
|
|
class_name HealthComponent
|
|
|
|
@export var max_health: float = 100.0
|
|
var current_health: float = 0.0
|
|
|
|
# Сигналы, чтобы другие скрипты знали, когда объект ранен или умер
|
|
signal health_changed(new_health: float)
|
|
signal died
|
|
|
|
func _ready() -> void:
|
|
current_health = max_health
|
|
|
|
func take_damage(amount: float) -> void:
|
|
if current_health <= 0:
|
|
return
|
|
|
|
current_health -= amount
|
|
health_changed.emit(current_health)
|
|
print("[Здоровье] ", get_parent().name, " получил урон: ", amount, ". Осталось: ", current_health)
|
|
|
|
if current_health <= 0:
|
|
current_health = 0
|
|
die()
|
|
|
|
func die() -> void:
|
|
died.emit()
|
|
print("[Здоровье] ", get_parent().name, " погиб!")
|
|
# Для врага удаляем его со сцены, для игрока тут можно вызвать экран смерти
|
|
if get_parent() and get_parent().name != "Player":
|
|
get_parent().queue_free()
|