extends Node2D

onready var viewport1 = $SplitScreen/ViewportContainer1/Viewport1
onready var viewport2 = $SplitScreen/ViewportContainer2/Viewport2
onready var viewport_both = $ViewportContainerBoth/ViewportBoth
onready var camera1 = $SplitScreen/ViewportContainer1/Viewport1/Camera1
onready var camera2 = $SplitScreen/ViewportContainer2/Viewport2/Camera2
onready var viewport_container_both = $ViewportContainerBoth
onready var cameraBoth = $ViewportContainerBoth/ViewportBoth/CameraBoth
onready var split_line = $SplitLine
onready var press_enter = $PressEnter

onready var frozen_view = $FrozenView
onready var upper_view_sprite = $FrozenView/UpperViewSprite
onready var upper_view_sprite_bg = $FrozenView/UpperViewSpriteBg
onready var lower_view_sprite = $FrozenView/LowerViewSprite
onready var lower_view_sprite_bg = $FrozenView/LowerViewSpriteBg
onready var frozen_colliders = $FrozenView/FrozenColliders

# onready var world = $SplitScreen/ViewportContainer1/Viewport1/World
var world = null

func _ready():
	var level = ProjectSettings.get("levels/next_level")
	world = load("res://Level" + str(level) + ".tscn").instance()
	viewport1.add_child(world)

	viewport_container_both.visible = false
	viewport2.world_2d = viewport1.world_2d
	viewport_both.world_2d = viewport1.world_2d
	var player1 = world.get_node("Player1")
	var player2 = world.get_node("Player2")
	camera1.target = player1
	camera2.target = player2
	cameraBoth.target = player2

	upper_view_sprite.set_region(true)
	upper_view_sprite.set_region_rect(Rect2(0, 0, 600, 300))
	
	player1.camera = camera1
	player1.other_player = player2
	player1.viewport_container_both = viewport_container_both
	player1.frozen_view = frozen_view
	player1.frozen_view_sprite = upper_view_sprite
	player1.frozen_view_sprite_bg = upper_view_sprite_bg
	player1.frozen_colliders = frozen_colliders
	player1.split_line = split_line
	press_enter.visible = false
	player1.press_enter = press_enter
	
	player2.camera = camera2
	player2.other_player = player1
	player2.viewport_container_both = viewport_container_both
	player2.frozen_view = frozen_view
	player2.frozen_view_sprite = lower_view_sprite
	player2.frozen_view_sprite_bg = lower_view_sprite_bg
	#player2.lower_viewport = viewport2
	player2.frozen_colliders = frozen_colliders
	
	#set_camera_limits()

# func set_camera_limits():
# 	var tile_map = world.get_node("TileMap")
# 	var tile_map_rect = tile_map.get_used_rect()
# 	var tile_map_cell_size = tile_map.cell_size
# 	for cam in [camera1, camera2, cameraBoth]:
# 		cam.limit_left = tile_map_rect.position.x * tile_map_cell_size.x
# 		cam.limit_right = tile_map_rect.end.x * tile_map_cell_size.x
# 		cam.limit_top = tile_map_rect.position.y * tile_map_cell_size.y
# 		cam.limit_bottom = tile_map_rect.end.y * tile_map_cell_size.y
