extends Entity

const ARMOR_EFFECT = preload("res://Effects/Armor.tscn")

var armor_per_row = 0
var armor_per_row_min = 1
var armor_per_row_max = 2

func target_enemy_blocked():
	if animation_player.current_animation == "Idle":
		if can_use_items and items.size() > 0 and randf() < 0.25:
			item_use()
		else:
			attack()
		clear_armor_effect()
		$Timer.start(rand_range(1.5, 2) / agility_base)

#func target_enemy_attacked():
#	$Timer.start(2)

func _on_Timer_timeout():
	#if animation_player.current_animation == "Idle":
	if (armor_per_row >= armor_per_row_max) \
	or (armor_per_row >= armor_per_row_min and randf() < 0.5):
		attack()
		clear_armor_effect()
		armor_per_row = 0
		$Timer.start(0.75 / agility_base)
	else:
		add_armor_effect()
		armor_per_row += 1
		$Timer.start(rand_range(1, 1.5) / agility_base)

func add_armor_effect():
	var inst = ARMOR_EFFECT.instance()
	inst.damage_divider = 10
	inst.set_timers(5, 5)
	add_effect(inst)

func clear_armor_effect():
	for effect in current_effects.get_children():
		if effect.type == "Armor":
			effect.queue_free()
			break

func hurt(damage):
	print(name, " has ", health, " health")
	for effect in current_effects.get_children():
		if effect.type == "Armor":
			var _no_use
			_no_use = tween.interpolate_property(
				draw_pivot, "position", 
				Vector2(draw_pivot.scale.x * 3, 0), Vector2.ZERO, 1.25, 
				Tween.TRANS_ELASTIC, Tween.EASE_OUT
			)
			_no_use = tween.start()
			return
	self.health -= damage
	add_armor_effect()
	var _no_use
	_no_use = tween.interpolate_property(
		draw_pivot, "position", 
		Vector2(draw_pivot.scale.x * 5, 0), Vector2.ZERO, 0.5, 
		Tween.TRANS_ELASTIC, Tween.EASE_OUT
	)
	_no_use = tween.start()
	#print(damage)
