SchildDerStaerke/scripts/hurtbox.gd

39 lines
894 B
GDScript

class_name HurtBox extends Area3D
var can_take_damage: bool = true
var cooldown_time: float = 1.0 # Cooldown duration in seconds
func _init() -> void:
collision_layer = 0
collision_mask = 2
func _ready() -> void:
connect("area_entered", Callable(self, "_on_area_entered"))
monitoring = true
func _on_area_entered(hitbox) -> void:
if not can_take_damage:
return
if not hitbox is HitBox:
print("Unexpected type:", hitbox)
return
if hitbox.owner == owner:
print("Hitbox owner is the same as hurtbox owner, ignoring hit.")
return
if hitbox == null or not hitbox is HitBox:
print("Unexpected or null hitbox:", hitbox)
return
if owner.has_method("take_damage"):
owner.call("take_damage", hitbox.get_damage())
start_cooldown()
func start_cooldown() -> void:
can_take_damage = false
await get_tree().create_timer(cooldown_time).timeout
can_take_damage = true