extends StaticBody2D

export var health = 10
export (String, "explosion_1", "explosion_2", "explosion_3", "explosion_4", "explosion_5", "explosion_6" ) var explosion_sound
export var has_damage_area = false
export var fire_danger = false
export var effect_mode = 1

var ignition = false

func _ready():
	pass
	
func apply_damage(var touch_pos, var touch_rot, var damage, var delay):
	health -= damage
	get_node("anim").play("hit")
	if fire_danger and health == 5:
		get_node("fire").set_emitting(true)
		get_node("auto_destroy_timer").start()
	if health <= 0:
		if delay != 0:
			var delay_timer = get_node("delay")
			delay_timer.set_wait_time(delay)
			delay_timer.start()
			yield(delay_timer, "timeout")	
		_destroy()
	
func _destroy():
	if has_damage_area:
		print ("booom!")
		var array = get_node("damage_area").get_overlapping_bodies()
		for i in range(array.size()):
			if array[i] != self and array[i].has_method("apply_damage"):
				var dir = Vector2(get_pos() - array[i].get_pos()).normalized()
				array[i].apply_damage(dir, 0, 30, (array.size() - i)*0.1)
	global.show_effect(get_pos(), 1, effect_mode)
	audio.play_sound(explosion_sound)
	queue_free()
