SchildDerStaerke/scripts/hurtbox.gd

38 lines
957 B
GDScript

class_name HurtBox
extends Area3D
const COLLISION_LAYER: int = 0
const COLLISION_MASK: int = 2
const DEFAULT_COOLDOWN_S: float = 1.0
var can_take_damage: bool = true
@export var damage_cooldown_s: float = DEFAULT_COOLDOWN_S
func _ready() -> void:
collision_layer = COLLISION_LAYER
collision_mask = COLLISION_MASK
area_entered.connect(_on_area_entered)
monitoring = true
func _on_area_entered(other_area: Area3D) -> void:
if not can_take_damage:
return
if not (other_area is HitBox):
push_warning("Unerwarteter Typ: %s" % [other_area])
return
var hitbox := other_area as HitBox
if hitbox.owner == owner:
# Eigenen Treffer ignorieren
return
if owner != null and owner.has_method("take_damage"):
owner.call("take_damage", hitbox.get_owner_damage())
start_damage_cooldown()
func start_damage_cooldown() -> void:
can_take_damage = false
await get_tree().create_timer(damage_cooldown_s).timeout
can_take_damage = true