extends Area2D


var speed = 120

onready var bullet_scene = load("res://scenes/bullet_a.tscn")
onready var bullet_scene_1 = load("res://scenes/bullet_a_1.tscn")
onready var bullet_scene_2 = load("res://scenes/bullet_a_2.tscn")
onready var bullet_scene_3 = load("res://scenes/bullet_c_1.tscn")
onready var explossion_scene = load("res://scenes/explossion.tscn")

var face_down = false
var is_moving = false

var extra_bullets = false
var back_fire = false
var shield = false

var death_pos = Vector2()
var respawn_time = 2

func _process(delta):
	var move_left = Input.is_action_pressed("ui_left")
	var move_right = Input.is_action_pressed("ui_right")
	var move_up = Input.is_action_pressed("ui_up")
	var move_down = Input.is_action_pressed("ui_down")
	var shift = Input.is_action_just_pressed("ui_shift")
	if shift:
		do_the_barell_roll()
	if move_left:
		position += Vector2(-1,0) * speed * delta
	elif move_right:
		position += Vector2(1,0) * speed * delta
	if move_up:
		position += Vector2(0,-1) * speed * delta
	elif move_down:
		position += Vector2(0,1) * speed * delta
	is_moving = move_left or move_up or move_right or move_down
	position.x = clamp(position.x, 16, 208)
	position.y = clamp(position.y, 32, 272)

func shoot():
	if !extra_bullets:
		var bullet = bullet_scene.instance()
		if is_moving:
			bullet.speed += speed
		bullet.global_position = $shoot_pos.global_position
		if face_down:
			bullet.dir.y = 1
		get_parent().add_child(bullet)
	else:
		var bullet = bullet_scene.instance()
		var bullet_1 = bullet_scene_1.instance()
		var bullet_2 = bullet_scene_2.instance()
		if is_moving:
			bullet.speed += speed
			bullet_1.speed += speed
			bullet_2.speed += speed
		bullet.global_position = $shoot_pos.global_position
		bullet_1.global_position = $shoot_pos.global_position
		bullet_2.global_position = $shoot_pos.global_position
		if face_down:
			bullet.dir.y = 1
			bullet_1.dir *= -1
			bullet_2.dir *= -1
		get_parent().add_child(bullet)
		get_parent().add_child(bullet_1)
		get_parent().add_child(bullet_2)
	$bullet_time.start()
	$shoot.play()

func _on_bullet_time_timeout():
	shoot()
	
func death():
	var e = explossion_scene.instance()
	death_pos = position
	e.position = death_pos
	get_tree().root.add_child(e)
	global.game.get_node("bg/anim").play("explossion")
	position = Vector2(-1024, -1024)
	set_process(false)
	$bullet_time.stop()
	$explossion.play()
	if global.change_lives(-1) > 0:
		respawn()
	else:
		global.game_over()

func _on_player_area_entered(area):
	if area.collision_mask == 4:
		apply_powerup(area.type)
		area.queue_free()
		return
	if area.collision_mask == 2 and shield:
		area.destroy()
		return
	death()

func apply_powerup(t):
	if t == 0:
		extra_bullets = true
	elif t == 1:
		shield(0.1, 9, 3)
	elif t == 2:
		global.change_lives(1)
	elif t == 3:
		pass
	$powerup_collect.play()
	
func shield(st, it, wt):
	shield = true
	$shield/shield_sprie.visible = true
	$shield/shield_timer.wait_time = st
	$shield/shield_timer.start()
	yield($shield/shield_timer,"timeout")
	$shield.monitorable = true
	$shield/shield_timer.wait_time = it
	$shield/shield_timer.start()
	yield($shield/shield_timer,"timeout")
	$shield/anim.play("blink")
	$shield/shield_timer.wait_time = wt
	$shield/shield_timer.start()
	yield($shield/shield_timer,"timeout")
	shield = false
	$shield/anim.stop()
	$shield/shield_sprie.visible = false
	$shield.monitorable = false
	
func respawn():
	var respawn_timer = Timer.new()
	add_child(respawn_timer)
	respawn_timer.wait_time = respawn_time
	respawn_timer.start()
	yield(respawn_timer, "timeout")
	position = death_pos
	set_process(true)
	$bullet_time.start()
	shield(0.1, 0.1, 3)
	respawn_timer.queue_free()
	
func do_the_barell_roll():
	if !face_down:
		$bullet_time.stop()
		$anim.play("barell_roll")
		yield($anim, "animation_finished")
		$bullet_time.start()
		face_down = true
	else:
		$bullet_time.stop()
		$anim.play_backwards("barell_roll")
		yield($anim, "animation_finished")
		$bullet_time.start()
		face_down = false
	pass
	
func _ready():
	position = Vector2(112, 256)
	global.player = self
	$bullet_time.connect("timeout", self, "_on_bullet_time_timeout")
	shoot()
	