extends Area2D

var destroyed = false
var damage = 4
var speed = 28
var y = 0
var new_y = 0
var dir_x = 1

func _process(delta):
	new_y += speed * delta
	if ceil(y) != ceil(new_y):
		y = new_y
		position.y += 1
	
func _on_area_entered(area):
	if destroyed:
		return
	destroyed = true
	var t = area.type
	if t == "bullet":
		area.queue_free()
		get_parent().flash()
		destroy()
	elif t == "rocket":
		area.queue_free()
		var _exp = load("res://scenes/explossion.tscn").instance()
		_exp.global_position = global_position
		get_parent().add_child(_exp)
		get_parent().flash()
		yield(get_tree().create_timer(0.15), "timeout")
		destroy()
	elif t == "body":
		if !area.apply_damage(damage):
			destroy()
			get_parent().damage()
		else:
			area.destroy()
			get_parent().flash()
			yield(get_tree().create_timer(0.15), "timeout")
			destroy()
		
func destroy():
	$life_time.disconnect("timeout", self, "queue_free")
	var _exp = load("res://scenes/explossion.tscn").instance()
	_exp.global_position = global_position
	get_parent().add_child(_exp)
	get_parent().n -= 1
	queue_free()
	
func _ready():
	$life_time.connect("timeout", self, "queue_free")
	$life_time.start()
	self.connect("area_entered", self, "_on_area_entered")
	yield($ready_time, "timeout")
	monitoring = true
