extends Node2D

var camera = null
var object_duplicate = []

export var fixedHorizontal = false
export var horizontalSpeed = 100.0

export var fixedVertical = true
export var verticalSpeed = 0.0

# TODO develop static parallax and automatic speed
export var staticPosition = false

export var endlessHorizontal = false
export var endlessVertical = false

export var automaticHorizontalSpeed = 0.0
export var automaticVerticalSpeed = 0.0

export var horizontalLimit = 10000.0

var previous_camera_pos = Vector2(0.0, 0.0)
var initialPosX = 0
var initialPosY = 0


# Called when needed to instantiate a object within the Scene
func instantiate(obj, obj_x, obj_y):
	if !obj:
		return
	var object2 = obj.duplicate()
	object2.set_name("parallax_instantiate")
	object2.position.x = obj_x
	object2.position.y = obj_y
	object2.show()
	call_deferred("add_child", object2)
	


func set_camera(_camera):
	camera = _camera

# Called when the node enters the scene tree for the first time.
func _ready():
	initialPosX = self.position.x
	initialPosY = self.position.y


func setup():
	if object_duplicate:
		return
	
	for obj in get_children():
		object_duplicate.append(obj)
		
	if !object_duplicate:
		return


# ----- TODO: vertical parallax ------
func repeatParallax():
	
	var camera_pos = camera.position.x + get_viewport().size.x
	
	for obj in object_duplicate:
		
		var obj_pos_x = obj.position.x
		var obj_pos_y = obj.position.y
		var obj_size = obj.texture.get_size()
		
		var new_x = obj_pos_x + obj_size.x
		
		#if the object on the right is near the end of the camera, duplicate it and move further
		if new_x + self.position.x < camera_pos + 1280:
			instantiate(obj, obj_pos_x, obj_pos_y)
			obj.position = Vector2(new_x, obj_pos_y)
	
	# And now, for cleaning objects that are completely away from the screen, if it's an automatic movement.
	# If it's not an automatic movement, it means the camera changes its pose with the player moving around,
	# so the old object can't be deleted (the player can access it again)
	if automaticHorizontalSpeed == 0.0: return	
	for obj in self.get_children():
		if obj.position.x + self.position.x < camera_pos - horizontalLimit:
			remove_child(obj)
			obj.queue_free()
	


func moveParallax():
	if staticPosition: # static position has no movement and is always camera positioned
		self.position.x = initialPosX + camera.get_camera_screen_center().x - 630
		self.position.y = initialPosY + camera.get_camera_screen_center().y - 360
		return
	
	initialPosX = initialPosX - automaticHorizontalSpeed
	initialPosY = initialPosY - automaticVerticalSpeed
	
	if horizontalSpeed != 0.0 and !fixedHorizontal:
		self.position.x = initialPosX + ((camera.get_camera_screen_center().x - 630) / 100.0 * horizontalSpeed)
	elif fixedHorizontal:
		self.position.x = initialPosX + (camera.get_camera_screen_center().x) - automaticHorizontalSpeed
	
	if (verticalSpeed != 0.0) and !fixedVertical:
		self.position.y = initialPosY + ((camera.get_camera_screen_center().y - 360) / 100.0 * verticalSpeed)
	elif fixedVertical:
		self.position.y = initialPosY + camera.get_camera_screen_center().y + -automaticVerticalSpeed


# warning-ignore:unused_argument
func _physics_process(delta):
	
	if !camera:
		if get_tree().get_nodes_in_group("Camera"):
			camera = get_tree().get_nodes_in_group("Camera")[0]
		else:
			return
		
	setup()
	
	repeatParallax()
		
	#automaticSpeed()
	moveParallax()
	
