extends Node

@onready var cam:Camera2D = $Global

var from:Camera2D
var tween:Tween

func _ready() -> void:
	from = $Cam1
	from.make_current()

var id:int = 0

signal change_respawn
func transition_camera(to:Camera2D, player_pos:Vector2):
	id += 1
	var transition_id = id
	change_respawn.emit(player_pos)
	cam.zoom = from.zoom
	cam.offset = from.offset
	cam.light_mask = from.light_mask
	
	cam.global_transform = from.global_transform
	cam.make_current()
	
	
	tween = create_tween()
	tween.tween_property(cam, "global_transform", to.global_transform, 1.0).set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.tween_property(cam, "zoom", to.zoom, 1.0).set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.tween_property(cam, "offset", to.offset, 1.0).set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.play()
	from = to
	
	await tween.finished
	if transition_id == id:
		id = 0
		from.make_current()
