tool

extends Node
export (PackedScene) var Creature
export (float) var mutation_rate = 0.05

var selected = []
var rng = RandomNumberGenerator.new()
const M_PI = 3.14

func _ready():
	rng.randomize()
	self.spawn_a_bitch()
	
func spawn_a_bitch():
	var c = Creature.instance()
	add_child(c)
	c.transform = c.transform.scaled(Vector3(0.1, 0.1, 0.1))
	c.transform = c.transform.translated(Vector3(0.0, 10.0, 0.0))
	c.transform = c.transform.rotated(Vector3(0.0, 1.0, 0.0), -0.5*M_PI)
	return c
	
func kill_a_bitch():
	var c = get_child(get_child_count()-1)
	if c:
		c.queue_free()

func combine_a_bitch():
	if selected.size() == 2:
		var new_dna = []
		for i in range(selected[0].dna.size()):
			if rng.randf() > 0.5:
				new_dna.append(selected[0].dna[i])
			else:
				new_dna.append(selected[1].dna[i])
			if rng.randf() <= mutation_rate:
				new_dna.append(!new_dna.pop_back())
		var new_child = spawn_a_bitch()
		new_child.dna = new_dna
		deselect_all()
		select(new_child)
		new_child.describe()
		new_child.set_color()

func deselect_all():
	selected = []

func select(creature):
	if selected.has(creature):
		creature.get_node("SelectionIndicator").visible = false;
		selected.erase(creature)
	else:	
		selected.append(creature)
		creature.get_node("SelectionIndicator").visible = true;

func select_a_bitch(camera, event, click_position, click_normal, index, creature_id):
	if event.is_action_pressed("select_creature"):
		var kindeke = get_child(creature_id)
		select(kindeke)
		print(creature_id)


func _on_Respawn_pressed():
	kill_a_bitch()
	spawn_a_bitch()
