extends Node3D

var map: Node3D
var starting_map = "levelground0000.tscn"

func init():
	MetSys.set_save_data()
	goto_map(MetSys.get_full_room_path(starting_map))
	MetSys.room_changed.connect(on_room_changed, CONNECT_DEFERRED)

func goto_map(map_path: String):
	var prev_map_position := Vector2i.MAX
	if map:
		# If some map is already loaded (which is true anytime other than the beginning), remember its position and free it.
		prev_map_position = MetSys.get_current_room_instance().get_base_coords()
		map.queue_free()
		map = null
	
	# Load the new map scene.
	map = load(map_path).instantiate()
	add_child(map)
	MetSys.current_layer = MetSys.get_current_room_instance().get_layer()

func on_room_changed(target_map: String):
	if target_map.is_absolute_path():
		goto_map(target_map)
	else:
		goto_map(MetSys.get_full_room_path(target_map))
