class_name DoorZone
extends Area2D

@export_category("Outgoing")
@export_file("*.tscn") var TargetRoomPath: String
@export var TargetDoor: String
@export var PreserveX: bool = false
@export var PreserveY: bool = false
@export var PreserveVelocity: bool = false

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	pass


func _on_body_entered(body:  Node2D) -> void:
	if body is Player:
		if self.name == global.player_target:
			return
		var offset := Vector2(0., 0.);
		if PreserveX:
			offset.x = body.position.x - self.position.x
		if PreserveY:
			offset.y = body.position.y - self.position.y
		global.player_offset = offset
		global.player_velocity = Vector2(0., 0.)
		if PreserveVelocity:
			global.player_velocity = body.velocity
		global.player_target = TargetDoor
		global.goto_scene(load(TargetRoomPath))


func _on_body_exited(_body:  Node2D) -> void:
	global.player_target = ""
