extends Area2D

export var limit_left = false
export var limit_right = false
export var limit_top = false
export var limit_bottom = false

export var boundary_left = 0.0
export var boundary_right = 0.0
export var boundary_top = 0.0
export var boundary_bottom = 0.0

export var only_camera = false
var camera

func _ready():
	#connect("body_enter", self, "on_body_enter")
	if get_tree().get_nodes_in_group("Camera"):
		camera = get_tree().get_nodes_in_group("Camera")[0]

# warning-ignore:unused_argument
func _process(delta):
		
	if not camera:
		if get_tree().get_nodes_in_group("Camera"):
			camera = get_tree().get_nodes_in_group("Camera")[0]
		else: return
	
	var bodies = get_overlapping_bodies()
	for body in bodies:
		if body.is_in_group("Player") and not only_camera:
			limit_camera_boundaries()
			queue_free()
			return
			
	if not only_camera: return
	if not camera: return
	
	if position.x < camera.position.x:
		limit_camera_boundaries()
		queue_free()


func limit_camera_boundaries():
	
	var limit_hor = camera.horizontalBoundary
	if limit_left: 	limit_hor.x = camera.position.x - 640
	if limit_right: limit_hor.y = camera.position.x + 640
	
	var limit_ver = camera.verticalBoundary
	if limit_top: 	limit_ver.x = camera.position.y - 360
	if limit_bottom: limit_ver.y = camera.position.y + 360
	
	if boundary_left != 0.0: 	limit_hor.x = boundary_left
	if boundary_right != 0.0: 	limit_hor.y = boundary_right
	if boundary_top != 0.0: 	limit_ver.x = boundary_top
	if boundary_bottom != 0.0: 	limit_ver.y = boundary_bottom
	
	Utils.interpolate(camera, 'horizontalBoundary', camera.horizontalBoundary, limit_hor, 0.1)
	Utils.interpolate(camera, 'verticalBoundary', camera.verticalBoundary, limit_ver, 0.1)
	
	"""
	if limit_left: 		Utils.interpolate(camera.horizontalBoundary, "x", camera.horizontalBoundary.x, camera.position.x - 640, 0.3)
	if limit_right: 		Utils.interpolate(camera.horizontalBoundary, "y", camera.horizontalBoundary.y, camera.position.x + 640, 0.3)
	if limit_top: 		Utils.interpolate(camera.verticalBoundary, "x", camera.verticalBoundary.x, camera.position.x - 360, 0.3)
	if limit_bottom: 		Utils.interpolate(camera.verticalBoundary, "y", camera.verticalBoundary.y, camera.position.x + 360, 0.3)
	
	if limit_right: 	camera.horizontalBoundary.y = camera.position.x + 640
	if limit_top: 		camera.verticalBoundary.x 	= camera.position.y - 360
	if limit_bottom: 	camera.verticalBoundary.y 	= camera.position.y + 360
	"""
	
	"""
	if limit_left: 		camera.horizontalBoundary.x = camera.position.x - 640
	if limit_right: 	camera.horizontalBoundary.y = camera.position.x + 640
	if limit_top: 		camera.verticalBoundary.x 	= camera.position.y - 360
	if limit_bottom: 	camera.verticalBoundary.y 	= camera.position.y + 360
	
	if boundary_left != 0.0: 	camera.horizontalBoundary.x = boundary_left
	if boundary_right != 0.0: 	camera.horizontalBoundary.y = boundary_right
	if boundary_top != 0.0: 	camera.verticalBoundary.x 	= boundary_top
	if boundary_bottom != 0.0: 	camera.verticalBoundary.y 	= boundary_bottom
	"""
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass
