class_name Hitbox extends Area2D
signal hit_entity(entity : Entity);
signal hit_obstacle;


var from_player = true
var enabled = true
@export var deal_damage = true
@export var dmg = 1

func _ready() -> void:
	collision_mask = 1 | 2 | 4
	body_entered.connect(_on_body_entered)
	area_entered.connect(_on_area_entered)

func _on_area_entered(area : Area2D):
	if area is Hitbox:
		if area.from_player != from_player:
			hit_obstacle.emit()

func _on_body_entered(body: Node) -> void:
	if not enabled: return
	if body is Enemy and from_player:
		if deal_damage:
			body.damage(dmg)
			body.knockback()
		
		hit_entity.emit(body)

	if body is Player and not from_player:
		if deal_damage:
			body.damage(dmg)
		
		hit_entity.emit(body)
	
	if body is StaticBody2D:
		hit_obstacle.emit()
