extends Area2D

var type = "body"
var activated = false
var support_time = 16
var health = 4

func on_area_entered(area):
	var t = area.type
	if t == "bullet":
		apply_bullet_damage(area.damage)
	elif t == "bonus":
		var bn = area.bonus_name
		if bn == "support":
			return
		elif bn == "rockets":
			get_parent().rockets_bonus_collect()
		elif bn == "hp":
			get_parent().hp_bonus_collect()
		area.collect()
		
func activate():
	activated = true
	$anim.play("activate")
	$life_time.wait_time = support_time
	$life_time.start()
	yield($life_time,"timeout")
	if activated:
		deactivate()

func deactivate():
	activated = false
	get_parent().set(self.get_name(), false)
	$anim.play_backwards("activate")
	
func apply_bullet_damage(d):
#	if !activated:
#		return
	health -= d
	if health < 0:
		destroy()
	else:
		$anim.play("damage")

func apply_damage(d):
	return true
	
func destroy():
	$sprite.visible = false
	var _exp = load("res://scenes/explossion.tscn").instance()
	_exp.global_position = global_position
	get_parent().get_parent().add_child(_exp)
	deactivate()
	
func _ready():
	self.connect("area_entered", self, "on_area_entered")
