Exemplo n.º 1
0
    private void IdentifySimilarCreature()
    {
        TargetCreature = null;                          // Reference to the script of the closest creature
        GameObject target     = null;
        float      similarity = Mathf.Infinity;

        ColliderCloud = Physics.OverlapSphere(_myTransform.position, (float)_lineOfSight);

        if (ColliderCloud.Length == 0)
        {
            target = null;
            return;
        }

        foreach (Collider col in ColliderCloud)
        {
            var currentCollider = col.transform.gameObject;             // Reference of the current collider
            if (currentCollider && currentCollider.gameObject.name == "root" && currentCollider != _myCreature.Root.gameObject)
            {
                _otherCreature = currentCollider.transform.parent.GetComponent <Creature>();
                var currentSimilarity = GeneticsUtilities.CheckSimilarColour(_myCreature.ChromosomeComposition, _otherCreature.ChromosomeComposition);

                if (currentSimilarity < similarity)
                {
                    target     = currentCollider.transform.parent.gameObject;
                    similarity = currentSimilarity;
                }

                Vector3 locationDifference = currentCollider.transform.position - _myTransform.position;
                if (locationDifference.magnitude < (float)_creatureMateRange)
                {
                    _otherCreature = currentCollider.transform.parent.GetComponent <Creature>();
                    Genitalia otherGenital = _otherCreature.Genital.GetComponent <Genitalia>();

                    if (_myCreature.State != Creature.CurrentState.PersuingMate &&
                        _otherCreature.State != Creature.CurrentState.PersuingMate)
                    {
                    }
                    else
                    {
                        _collisionHandler.ObserveColliders(_myCreature.Genital.gameObject, otherGenital.gameObject);
                        _otherCreature.ChangeState(Creature.CurrentState.Mating);
                        _myCreature.ChangeState(Creature.CurrentState.Mating);
                    }

                    similarity = currentSimilarity;
                }
            }

            DistanceToGoal = 0F;
            Goal           = null;
            if (!target)
            {
                continue;
            }
            TargetCreature = target.GetComponent <Creature>();
            Goal           = TargetCreature.Root;
            DistanceToGoal = DistanceUntilGoal();
        }
    }
Exemplo n.º 2
0
    public void ObserveColliders(GameObject colliderA, GameObject colliderB)
    {
        // Create a new event handler reference
        CollisionEventsStorer.Add(new EventHandler(colliderA, colliderB));

        // Check if the event already exists
        var eventDuplicate = CheckIfEventExists(colliderA, colliderB);

        // If the event doesn't exists, create a new handler
        if (null != eventDuplicate)
        {
            CollisionEventsStorer.Clear();
            // Capture the location of the collision and create a new reference point 0.5 units away from it
            var eventPosition = (colliderA.transform.position - colliderB.transform.position) * 0.5F +
                                colliderB.transform.position;

            // Get reference to the Creature scripts on each collision actor (a and b)
            var creatureAScript = colliderA.transform.parent.parent.GetComponent <Creature>();
            var creatureBScript = colliderB.transform.parent.parent.GetComponent <Creature>();

            // Get reference to the energy of each collision actor (a and b)
            var aEnergy = creatureAScript.GetEnergy();
            var bEnergy = creatureBScript.GetEnergy();

            // Create A new crossover between each actors chromosome structure
            var newChromosome = GeneticsUtilities.Crossover(
                creatureAScript.ChromosomeComposition,
                creatureBScript.ChromosomeComposition,
                _crossoverRate);

            // Mutate the new chromosome
            newChromosome = GeneticsUtilities.Mutate(
                newChromosome,
                _mutationRate,
                _mutationFactor);

            // Transfer energy to child creature
            var creatureAEnergyToChild = aEnergy * _energyPassage;
            var creatureBEnergyToChild = bEnergy * _energyPassage;
            var newCreatureEnergy      = creatureAEnergyToChild + creatureBEnergyToChild;

            // Spawn the child creature
            EnvironmentController.SpawnerOrganiser.Spawn(
                eventPosition, Vector3.zero,
                newCreatureEnergy,
                newChromosome
                );

            // Deduct energy from each parent creature
            creatureAScript.SetEnergy(aEnergy - creatureAEnergyToChild);
            creatureBScript.SetEnergy(bEnergy - creatureBEnergyToChild);

            // Increment each creatures offspring counter
            creatureAScript.Offspring++;
            creatureBScript.Offspring++;
        }
        else
        {
            // Create new event
            CollisionEventsStorer.Add(new EventHandler(colliderB, colliderA));
        }
    }