extends Node2D

@export var speed: float = 400.0
@export var max_height: float = 100.0
@export var payload : PackedScene
var target_position: Vector2

@onready var sprite = $Bomb

var start_position: Vector2
var distance_total: float
var distance_covered: float = 0.0

var from_player = true

func set_from_player(value):
	from_player = value

func _ready():
	start_position = global_position
	distance_total = 150

func _process(delta):
	if distance_covered < distance_total:
		distance_covered += speed * delta
		var t = clamp(distance_covered / distance_total, 0.0, 1.0)
		
		global_position = start_position.lerp(target_position, t)
		
		var arc_y = 4 * max_height * t * (1 - t)
		
		sprite.position.y = -arc_y
	else:
		global_position = target_position
		sprite.position.y = 0
		set_process(false) 
		on_arrival()

func on_arrival():
	EventBus.screenshake.emit(1)
	var ins = payload.instantiate()
	ins.set_from_player(from_player)
	ins.global_position = global_position
	get_parent().add_child(ins)
	queue_free()
