extends Node2D

# Level's spawning settings
var level_width = 1920
var x_pos = 0

# Links to game's objects
var levels = ["res://levels/Level_One.tscn", "res://levels/TheOutOfCity_fixed.tscn"]
var player_link = "res://assets/Player/Player.tscn"
var overlay_link = "res://assets/GUI/HP_Bar/HP_Bar.tscn"

# For instances of game objects
var player: KinematicBody2D = null
var overlay: Node2D = null

func environment_init():
	for link in levels:
		var level = load(link)
		if level == null:
			continue
		var level_instance = level.instance()
		level_instance.position.x += x_pos
		add_child(level_instance)
		
		x_pos += level_width

func player_init():
	player = load(player_link).instance()
	player.position.x = 20
	add_child(player)
	return player

func overlay_init():
	overlay = load(overlay_link).instance()
	add_child(overlay)
	return overlay

func _ready():
	environment_init()
	player_init()
	overlay_init()

# Unload a unused level
func unload_prev_level(self_level):
	var children:Array = get_children()
	var index:int = children.find(self_level)
	
	# Error processing 
	if index == -1:
		# Nothing found
		return -1
	elif index == 0:
		# The previous level is the first level 
		return 0
	# Unload the previous (unused) level
	else:
		children[index - 1].queue_free()
