extends Node2D

export (Array,PackedScene) var scenes

var random_scene = RandomNumberGenerator.new()
var scene_index = 0;
var last_object
var end_of_game = false
var spawner_enabled = false
var timer_speed = 0.99
var random_threshold = 50
var scenes_offset = 0

onready var timer := $Timer
onready var items := $items

func _ready():
	Signals.connect("die",self,"game_over")
	Signals.connect("win",self,"game_over")


func _on_Timer_timeout():
	if spawner_enabled:
		random_scene.randomize()
		scene_index = random_scene.randi_range(1,100)
		if(scene_index >= random_threshold):
			scene_index = 1
		else:
			scene_index = 0
		var tmp = scenes[scene_index+scenes_offset*2].instance()
		items.add_child(tmp)
		last_object = tmp
		if(timer.wait_time <= 1):
			timer.wait_time = 1
		else:
			timer.wait_time *= timer_speed
		timer.start()


func game_over():
	timer.set_paused(true)
	#last_object.queue_free()


func start(difficulty, version):
	match difficulty:
		"easy":
			timer_speed = 0.99
			random_threshold = 50
		"hard":
			timer_speed = 0.95
			random_threshold = 33
		_:
			pass
	scenes_offset = version
	spawner_enabled = true
	timer.set_paused(false)


func stop():
	spawner_enabled = false
	for obj in $items.get_children():
		remove_child(obj)
		obj.queue_free()
