extends Node2D

var materials : Array = []

var minion = preload("res://Minion.tscn")
var enemy = preload("res://Enemy.tscn")

func _physics_process(delta):
	var bones = []
	var staffs = []
	var keys = []
	var books = []
	var souls = []
	var gems = []
	
	for item in materials:
		match item.item_type:
			"bone":
				bones.append(item)
			"book":
				books.append(item)
			"gem":
				gems.append(item)
			"key":
				keys.append(item)
			"soul":
				souls.append(item)
			"staff":
				staffs.append(item)
	
	if len(bones) > 0:
		$Hints/skeleton_bone_1.frame = 1
		$Hints/soldier_bone.frame = 1
		$Hints/ogre_bone.frame = 1
	else:
		$Hints/skeleton_bone_1.frame = 0
		$Hints/soldier_bone.frame = 0
		$Hints/ogre_bone.frame = 0
	
	if len(bones) > 1:
		$Hints/skeleton_bone_2.frame = 1
	else:
		$Hints/skeleton_bone_2.frame = 0
	
	if len(staffs) > 0:
		$Hints/ghost_staff.frame = 1
		$Hints/soldier_staff.frame = 1
	else:
		$Hints/ghost_staff.frame = 0
		$Hints/soldier_staff.frame = 0
	
	if len(keys) > 0:
		$Hints/soldier_key.frame = 1
		$Hints/ogre_key.frame = 1
	else:
		$Hints/soldier_key.frame = 0
		$Hints/ogre_key.frame = 0
	
	if len(books) > 0:
		$Hints/ghost_book.frame = 1
		$Hints/ogre_book.frame = 1
	else:
		$Hints/ghost_book.frame = 0
		$Hints/ogre_book.frame = 0
	
	if len(souls) > 0:
		$Hints/skeleton_soul.frame = 1
		$Hints/ghost_soul.frame = 1
		$Hints/soldier_soul.frame = 1
		$Hints/ogre_soul.frame = 1
	else:
		$Hints/skeleton_soul.frame = 0
		$Hints/ghost_soul.frame = 0
		$Hints/soldier_soul.frame = 0
		$Hints/ogre_soul.frame = 0
	
	if len(gems) > 0:
		$Hints/ogre_gem.frame = 1
	else:
		$Hints/ogre_gem.frame = 0
	
	if len(souls) > 0:
		if len(bones) >= 2:
			self.remove_child(souls[0])
			materials.erase(souls[0])
			souls.remove(0)
			self.remove_child(bones[0])
			materials.erase(bones[0])
			bones.remove(0)
			self.remove_child(bones[0])
			materials.erase(bones[0])
			bones.remove(0)
			
			var new_minion = minion.instance()
			new_minion.minion_type = "skeleton"
			$"../Defence Game".add_child(new_minion)
			$"../Score".score += 25
			$spawn.play()
			
		elif len(books) >= 1 and len(staffs) >= 1:
			self.remove_child(souls[0])
			materials.erase(souls[0])
			souls.remove(0)
			self.remove_child(books[0])
			materials.erase(books[0])
			books.remove(0)
			self.remove_child(staffs[0])
			materials.erase(staffs[0])
			staffs.remove(0)
			
			var new_minion = minion.instance()
			new_minion.minion_type = "ghost"
			$"../Defence Game".add_child(new_minion)
			$"../Score".score += 50
			$spawn.play()
			
		elif len(staffs) >= 1 and len(keys) >= 1 and len(bones) >= 1:
			self.remove_child(souls[0])
			materials.erase(souls[0])
			souls.remove(0)
			self.remove_child(staffs[0])
			materials.erase(staffs[0])
			staffs.remove(0)
			self.remove_child(keys[0])
			materials.erase(keys[0])
			keys.remove(0)
			self.remove_child(bones[0])
			materials.erase(bones[0])
			bones.remove(0)
			
			var new_minion = minion.instance()
			new_minion.minion_type = "soldier"
			$"../Defence Game".add_child(new_minion)
			$"../Score".score += 75
			$spawn.play()
			
		elif len(gems) >= 1 and len(books) >= 1 and len(keys) >= 1 and len(bones) >= 1:
			self.remove_child(souls[0])
			materials.erase(souls[0])
			souls.remove(0)
			self.remove_child(gems[0])
			materials.erase(gems[0])
			gems.remove(0)
			self.remove_child(books[0])
			materials.erase(books[0])
			books.remove(0)
			self.remove_child(keys[0])
			materials.erase(keys[0])
			keys.remove(0)
			self.remove_child(bones[0])
			materials.erase(bones[0])
			bones.remove(0)
			
			var new_minion = minion.instance()
			new_minion.minion_type = "ogre"
			$"../Defence Game".add_child(new_minion)
			$"../Score".score += 100
			$spawn.play()
