extends Node2D

var game = false
var stop = false

var race_length = 16
var road_width = 21
var _wave = 0
var total_time = 0

var bonuses = ["fuel"]

var waves = [{rw = 21, o = 0, f = 1.4, t = 16},
			{rw = 17, o = 1, f = 1, t = 16},
			{rw = 21, o = 0, f = 1, t = 12},
			{rw = 13, o = 2, f = 1, t = 20}]

onready var border_scene = load("res://scenes/border.tscn")
onready var enemie_car_scene = load("res://scenes/enemie_car_1.tscn")
onready var enemie_truck = load("res://scenes/enemie_truck.tscn")
onready var bonus_scene = load("res://scenes/bonus.tscn")

onready var objects = [enemie_car_scene, border_scene, enemie_truck]

func _process(delta):
	total_time -= delta
	$gul/racebar.value = $gul/racebar.max_value - total_time
	if stop:
		if $road/anim.playback_speed > 0:
			$road/anim.playback_speed -= 1 * delta
			
func flash():
	$effect/anim.play("flash")
	
func damage():
	$effect/anim.play("damage")
			
func wave():
	var w = waves[_wave]
	change_road_width(w.rw)
	var dt = 0.2 * (abs(road_width - w.rw))
	var t
	if dt > 0:
		t = _create_timer(dt)
	else:
		t = _create_timer(0.2)
	t.start()
	yield(t, "timeout")
	t.wait_time = w.t
	t.start()
	var _t = _create_timer(w.f)
	var dx = 1
	while t.time_left > 0:
		var lim = $player.limits
		_t.start()
		yield(_t, "timeout")
		var b = objects[w.o].instance()
		b.pos_x = int(rand_range(lim.x, lim.y + 1)) + 1
		b.limits = lim
		b.dir_x = dx
		add_child(b)
		dx *= -1
	_t.queue_free()
	_wave += 1
	if _wave != waves.size():
		t.wait_time = 5
		t.start()
		yield(t, "timeout")
		t.queue_free()
		wave()
	else:
		t.wait_time = 8
		t.start()
		yield(t, "timeout")
		next_level()
		
func next_level():
	$fader/anim.play("fade_in")
	global.chapter = 2
	yield($fader/anim, "animation_finished")
	get_tree().change_scene("res://scenes/transition_screen.tscn")
			
func change_road_width(w):
	if road_width == w:
		return
	var r = road_width - w
	var s = sign(r)
	var t  = _create_timer(0.2)
	for i in range(abs(r)):
		$road/road_part_left/middle_road_part.scale.x -= 2 * s
		$road/road_part_left.position.x += s
		$road/road_part_right.position.x -= s
		if $player != null:
			$player.limits += Vector2(1, -1) * s
		road_width -= s
		if road_width > 19:
			$road/strip.visible = false
			$road/double_strip.visible = true
		elif road_width < 15:
			$road/strip.visible = false
			$road/double_strip.visible = false
		elif road_width >= 15:
			$road/strip.visible = true
			$road/double_strip.visible = false
		yield(t, "timeout")
		t.start()
	road_width = w

func _create_timer(t):
	var _timer = Timer.new()
	_timer.wait_time = t
	_timer.one_shot = true
	add_child(_timer)
	_timer.start()
	return _timer
			
func bonus_spawner():
	var t = _create_timer(12)
	while game:
		t.start()
		spawn_bonus("fuel")
		yield(t, "timeout")
		continue

func spawn_bonus(n):
	var bonus = bonus_scene.instance()
	var lim = $player.limits
	var x = int(rand_range(lim.x, lim.y + 1)) + 1
	var pos = Vector2(x, -2)
	bonus.bonus_name = n
	bonus.position = pos
	bonus.speed = 8
	add_child(bonus)

func restart():
	game = false
	var timer = _create_timer(3)
	yield(timer, "timeout")
	timer.queue_free()
	get_tree().reload_current_scene()
	
func _total_time():
	total_time += 6
	for i in waves.size():
		total_time += waves[i].t
	return total_time

func _ready():
	randomize()
	$gul/racebar.max_value = _total_time()
	$gul/racebar.value = 0
	$fader/anim.play("fade_out")
	var timer = _create_timer(1)
	yield(timer, "timeout")
	game = true
	wave()
	timer = _create_timer(6)
	yield(timer, "timeout")
	bonus_spawner()
	timer.queue_free()
