extends Position2D

export(String) var LINE_COLOR_HEX = '#FFEB3B'

var grid_position = Vector2()
var grid_size = Vector2()
var t = 0.0

onready var parent = PlayerInfo
#onready var parent = $Player

func _ready():
	# If you drag the camera from the OffsetPivot node,
	# its position will not be (0, 0)
	#$Camera2D.global_position = Vector2(160,90) # when doing grid snapping
	#$Left.position = Vector2(160,90)
	$Camera2D.position = Vector2(0,-4)
	#grid_size = OS.get_screen_size()
	grid_size = get_node("/root").size
	#grid_size = get_node("/root").get_visible_rect().size
	print(grid_size)
	set_as_toplevel(true)
	update_grid_position()
	print("Camera Set: ",grid_position, position)
	print("Ray Left: ", $Left.global_position, $Left.cast_to)



func _physics_process(delta):
	t += delta * 0.4
	#print(parent.position.x)
	update_grid_position()
	#draw_line( Vector2(0, -half_line_length), Vector2(0, half_line_length), color, 3.0)

#func _draw():
#	var color = Color(LINE_COLOR_HEX)
#	draw_line( $Left.position, $Left.cast_to, color, 3.0)
#	draw_line( Vector2(0,0), Vector2(200,200), color, 3.0)


func update_grid_position():
	var new_grid_position = calculate_grid_position()
#	if $Left.is_colliding():
#		print("Oh God! No!")
#	if grid_position == new_grid_position:
#		return
	grid_position = new_grid_position
	jump_to_grid_position()
	#$Left.position = Vector2(grid_position * grid_size)
	#print("Ray Left: ", $Left.global_position, $Left.cast_to)


func calculate_grid_position():
	# this needs to heavily round off the parents position
	var x_offset = floor(parent.player_position.x / grid_size.x)
	var y_offset = floor(parent.player_position.y / grid_size.y)
	var x = round(x_offset)
	#var x = (((grid_position.x * grid_size.x) + ((grid_position.x + 1) * grid_size.x))/2) 
	var y = round(y_offset)
	#var y = round(parent.position.y / grid_size.y)
	return Vector2(x, y)


func jump_to_grid_position():
	#var x_pos = (((grid_position.x * grid_size.x) + ((grid_position.x + 1) * grid_size.x))/2) 
	# rounding or flooring position seems to just cause a lot of jitter 
	var x_pos = (parent.player_position.x - grid_size.x/2)
#	if (x_pos - grid_size.x/2) < 0:
#		x_pos = grid_size.x/2
#	if x_pos < 0:
#		x_pos = 0
	if $Left.is_colliding():
		#print("Oh God! No! ", $Left.get_collision_point().x, $Left.global_position)
		$Camera2D.limit_left = $Left.get_collision_point().x
		# attempts to just change the position to the collision point created a
		# ghosting behavior, now I just set the limit of the camera to the point
		
		#x_pos = $Left.get_collision_point().x+1
		#position = Vector2($Left.get_collision_point().x+1, grid_position.y * grid_size.y)
	else:
		$Camera2D.limit_left = -10000000
	if $Right.is_colliding():
		#print("Oh God! No! ", $Right.get_collision_point().x, $Right.global_position)
		$Camera2D.limit_right = $Right.get_collision_point().x
	else:
		$Camera2D.limit_right = 10000000
	position = Vector2( x_pos ,grid_position.y * grid_size.y)
	#$Camera2D.force_update_scroll()
	#position = Vector2( grid_position * grid_size)
	#print("Camera Move: ",grid_position, position)


#func _physics_process(delta):
#
#	$Sprite2D.position = $A.position.lerp($B.position, t)
