extends Area2D

const Bullet = preload("res://Scenes/Objects/Bullet.tscn")
const Explosion = preload("res://Scenes/ExplosionEffect.tscn")

export (int) var speed = 100

signal player_death

func _process(delta):
	if Input.is_action_pressed("ui_up"):
		position.y -= speed * delta
		
	if Input.is_action_pressed("ui_down"):
		position.y += speed * delta
		
	if Input.is_action_just_pressed("ui_select"):
		fire_bullet()


func fire_bullet():
	var bullet = Bullet.instance()
	var root = get_tree().current_scene # World scene root node
	
	bullet.global_position = global_position
	
	root.add_child_below_node(root.get_node("Background"), bullet)


func _on_Ship_area_entered(area):
	queue_free()
	area.queue_free()


func _exit_tree():
	var explosion = Explosion.instance()
	explosion.global_position = global_position
	get_tree().current_scene.call_deferred("add_child", explosion)
	emit_signal("player_death")
