class_name Bullet extends CharacterBody2D

@onready var damageArea = $DamageArea
@onready var rangeTimer = $rangeTimer

var speed = 10 * 10000

var damage: int = 1
var penetration: int = 1
var penetratedCount: int = 0
var range: float = 1.0

var fromTimeGhost = false

func _ready() -> void:
	rangeTimer.start(range)

func spawn(_positon: Vector2, _rotation: float, _damage: int, _range: float = 8.0, _penetration: int = 1, _speed: int = 10) -> void:
	global_position = _positon
	rotation = _rotation
	damage = _damage
	penetration = _penetration
	range = _range
	speed = _speed * 10000


func _physics_process(delta: float) -> void:
	velocity = Vector2.UP.rotated(rotation) * speed * delta
	move_and_slide()

func _on_damage_area_area_entered(area: Area2D) -> void:
	if fromTimeGhost:
		#print(area.get_parent())
		#print(Globals.getPlayer())
		if area.get_parent() == Globals.getPlayer():
			return
	damageArea.damageHurtArea(area, damage)
	penetratedCount += 1
	checkPenetration()

func checkPenetration():
	if penetration <= penetratedCount:
		queue_free()

func _on_range_timer_timeout() -> void:
	queue_free()
