extends Area2D

export(String,
	"skeleton",
	"ghost",
	"soldier",
	"ogre"
) var minion_type

var health : float = 1
var speed : float = 1.0
var damage : float = 1

func _ready():
	match minion_type:
		"skeleton":
			$AnimatedSprite.animation = "skeleton"
			health = 4
			speed = 4
			damage = 2
		"ghost":
			$AnimatedSprite.animation = "ghost"
			health = 3
			speed = 5
			damage = 1
		"soldier":
			$AnimatedSprite.animation = "soldier"
			health = 7
			speed = 3
			damage = 3
		"ogre":
			$AnimatedSprite.animation = "ogre"
			health = 11
			speed = 2
			damage = 5
	
	self.position = Vector2(166, 33)
	
	$CPUParticles2D.emitting = true

func _physics_process(delta):
	if health <= 0:
		self.queue_free()
	
	for area in self.get_overlapping_areas():
		if area.position.x > self.position.x or area.is_in_group("enemy"):
			if area.is_in_group("enemy"):
				area.health -= damage*delta
				$"../../Score".score += damage*delta
				
				$AnimatedSprite.playing = true
				
				if $AnimatedSprite.frame == 1:
					$hit.playing = true
				
				return
	
	for area in self.get_overlapping_areas():
		if area.is_in_group("enemybase"):
			if area.is_in_group("enemybase"):
				area.health -= damage*delta
				$"../../Score".score += damage*delta*10
				
				$AnimatedSprite.playing = true
				
				if $AnimatedSprite.frame == 1:
					$hit.playing = true
				
				self.health -= 1*delta
				
				return
	
	$AnimatedSprite.playing = false
	$AnimatedSprite.frame = 0
	
	for area in self.get_overlapping_areas():
		if area.is_in_group("minion") and area.position.x > self.position.x:
			return
	
	self.position.x += delta * speed
