extends Area2D

var type = "body"

var pos_x = 16

var y_speed = 8
var x_speed = 0

var x = 0
var y = 0
var new_x = 0
var new_y = 0
var dir_x = 1
var dir_y = 1

var limits = Vector2(0, 29)

func _process(delta):
	new_y += y_speed * delta
	if ceil(y) != ceil(new_y):
		y = new_y
		position.y += 1
		
	new_x += x_speed * delta
	if ceil(x) != ceil(new_x):
		x = new_x
		position.x += 1 * dir_x
		
	if position.x < limits.x + 1 or position.x > limits.y + 1:
		dir_x *= -1
		position.x += 1 * dir_x
		
func _on_area_entered(area):
	area.destroy()
	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().flash()
	queue_free()
	
func _ready():
	$life_time.connect("timeout", self, "queue_free")
	$life_time.start()
	var rnd = randi () % 10
	if rnd > 7:
		x_speed = 8
		$sprite.frame = 1
	global_position.x = pos_x
	self.connect("area_entered", self, "_on_area_entered")
