extends Node2D

onready var enemy_scene_a = load("res://scenes/enemy_a.tscn")
onready var enemy_scene_b = load("res://scenes/enemy_b.tscn")
onready var enemy_scene_c = load("res://scenes/enemy_c.tscn")
onready var bonus_scene = load("res://scenes/bonus.tscn")
onready var explossion_scene = load("res://scenes/explossion.tscn")
onready var hit_scene = load("res://scenes/hit.tscn")
onready var hit_scene_2 = load("res://scenes/hit_2.tscn")
onready var enemies = [enemy_scene_a, enemy_scene_b, enemy_scene_c]

var wave_factor
var wave_size = [0, 0, 0]
var enemies_counter = 0
var escaped_b = 0
var num_of_groups_b = 3
var bonus_b = true

func _on_enemy_death(e):
	enemies_counter -= 1
	global.ui.ui_update_enemies_count(enemies_counter)
	if enemies_counter <= 0:
		randomize()
		var bonus_roulete = randi()%3
		if bonus_roulete == 1:
			spawn_bonus(randi()%3)
		var timer = Timer.new()
		add_child(timer)
		timer.wait_time = 0.1
		timer.start()
		yield(timer, "timeout")
		start_new_wave()
		timer.queue_free()
		
func _on_enemy_escape():
	escaped_b += 1
	
func start_new_wave():
	bonus_b = true
	global.new_wave()
	wave_factor = global.wave * 0.05
	print(wave_factor)
	wave_size[0] = floor(80 * wave_factor)
	print(wave_size[0])
	wave_size[1] = floor(80 * wave_factor) 
	wave_size[2] = floor(wave_factor * 4)
	for i in range(wave_size.size()):
		enemies_counter += wave_size[i]
	print(enemies_counter)
	global.ui.ui_update_enemies_count(enemies_counter)	
	var groups_b =[]
	groups_b.append(int(wave_size[1])/num_of_groups_b)
	groups_b.append((int(wave_size[1]) - groups_b[0])/(num_of_groups_b-1))
	groups_b.append((int(wave_size[1]) - groups_b[0] - groups_b[1]))
	
	randomize()
	
	spawn_a(wave_size[0], 0.5)
	spawn_b(0.1, groups_b)
	spawn_c(wave_size[2])

func spawn_a(a, p):
	var groups_b = []
	var timer = Timer.new()
	add_child(timer)
	timer.wait_time = p
	for i in range(a):
		var enemy = enemies[0].instance()
		var pos_x = rand_range(16, 208)
		enemy.global_position = Vector2(pos_x, 0)
		enemy.pos_x = pos_x
		enemy.speed += enemy.speed * wave_factor
		add_child(enemy)
		timer.start()
		yield(timer, "timeout")
	timer.queue_free()
	
func spawn_b(p, g):
	var timer = Timer.new()
	add_child(timer)
	for i in g:
		var timer_2 = Timer.new()
		add_child(timer_2)
		timer_2.wait_time = p
		var pos_x = rand_range(32, 192)
		for j in range(i):
			var enemy = enemies[1].instance()
			enemy.global_position = Vector2(pos_x, 0)
			enemy.pos_x = pos_x
			enemy.speed += enemy.speed * wave_factor
			add_child(enemy)
			timer_2.start()
			yield(timer_2, "timeout")
		timer_2.queue_free()
		timer.wait_time = 5
		timer.start()
		yield(timer, "timeout")
	if escaped_b > 0:
		spawn_b(0.1, [escaped_b])
		escaped_b = 0
		bonus_b = false
	else:
		timer.queue_free()
		escaped_b = 0
	if bonus_b == true:
		spawn_bonus(randi()%0)
		
func spawn_c(a):
	var timer = Timer.new()
	add_child(timer)
	for i in a:
		var enemy = enemies[2].instance()
		enemy.global_position = Vector2(120, 0)
		enemy.pos_x = 120
		enemy.speed += enemy.speed * wave_factor
		add_child(enemy)
		timer.wait_time = 6
		timer.start()
		yield(timer, "timeout")
		
func spawn_bonus(t):
	var b = bonus_scene.instance()
	b.position.x = rand_range(16, 208) 
	b.type = t
	add_child(b)
	
func _ready():
	start_new_wave()