extends AnimatedSprite2D

var direction: Vector2
var speed: float
var power: int

@onready var collider = $Collision
@onready var collision = $Collision/CollisionShape2D
@onready var timer = $Timer

signal collided_with_area(other: Area2D, power: int, direction: Vector2)

func _ready():
	flip_h = sign(direction.x) < 0
	flip_v = sign(direction.y) > 0
	if abs(direction.dot(Vector2.RIGHT)) == 0.0:
		# Vertical
		self.play(&"vertical")
	else:
		self.play(&"horizontal")
		
func _physics_process(delta: float):
	position += direction * speed * delta

func _on_collision_area_entered(area):
	_boom()
	collided_with_area.emit(area, power, direction)

func _on_timeout():
	queue_free()

func _boom():
	timer.start(0.3)
	play(&"hit")
	speed = 0.0
	z_index = 1
	collision.set_deferred("disabled", true)

func _on_collision_body_entered(body):
	_boom()
