extends Node2D

# Speed of the bullet
@export var speed: float

var shot_by: CharacterBody2D
var damaged_opponent: CharacterBody2D

# Direction is calculated based on the projectile's rotation
var direction: Vector2 = Vector2()

func _ready():
	# Set the direction based on the projectile's current rotation
	direction = Vector2(cos(rotation), sin(rotation)).normalized()
	destroy_after_seconds(3)

func _physics_process(delta):
	# Move the bullet forward
	position += direction * speed * delta

func destroy_after_seconds(seconds):
	await get_tree().create_timer(seconds).timeout
	queue_free()

func _on_area_2d_area_entered(area: Area2D) -> void:
	if area.name == "Head_Collision":
		var parent = area.get_parent()
		damaged_opponent = parent
		parent.take_damage()
		shot_by.deal_damage()
		queue_free()
