Пример #1
0
    private void Reproduce(float spawnSize)
    {
        int hightestGen = 0;

        if (spawnSize >= 0 && BirthTime > MATURE_AGE)
        {
            var parents = new List <Creature> {
                this
            };

            float availibleEnergy = Energy - SAFE_SIZE;

            for (int i = 0; i < SoftBodies.Count; i++)
            {
                var potentialMate = SoftBodies[i] as Creature;

                if (potentialMate != null &&
                    potentialMate.BirthTime > MATURE_AGE &&
                    (potentialMate.fightLevel < fightLevel ||
                     potentialMate.CreatureBrain.Outputs()[9] > -1))
                {
                    float distance = MathUtils.CalcTileDist(
                        transform.position.x, transform.position.y,
                        potentialMate.transform.position.x, potentialMate.transform.position.y);
                    float combinedRadius = GetRadius() * INFLUENCE_AREA + potentialMate.GetRadius();

                    if (distance < combinedRadius)
                    {
                        parents.Add(potentialMate);
                        availibleEnergy += potentialMate.Energy - SAFE_SIZE;
                    }
                }
            }

            if (availibleEnergy > spawnSize)
            {
                // To avoid / by 0 error...
                float newPosX = Random.Range(-0.01f, 0.01f);
                float newPosY = Random.Range(-0.01f, 0.01f);

                float newHue      = 0;
                float newSat      = 0;
                float newBri      = 0;
                float newMouthHue = 0;
                var   parentnames = new string[parents.Count];

                Brain newBrain = Brain.Evolve(parents);

                for (int i = 0; i < parents.Count; i++)
                {
                    int      chosenIndex = Random.Range(0, parents.Count);
                    Creature parent      = parents[chosenIndex];

                    parent.Energy -= spawnSize * (parent.Energy - SAFE_SIZE / availibleEnergy);

                    newPosX     += parent.transform.position.x / parents.Count;
                    newPosY     += parent.transform.position.y / parents.Count;
                    newHue      += parent.Hue / parents.Count;
                    newSat      += parent.Saturation / parents.Count;
                    newBri      += parent.Brightness / parents.Count;
                    newMouthHue += parent.MouthHue / parents.Count;

                    parentnames[i] = parent.name;

                    if (parent.Generation > hightestGen)
                    {
                        hightestGen = parent.Generation;
                    }
                }

                newSat = 1;
                newBri = 1;

                var newCreatureObj = Instantiate(Board.Instance.CreaturePrefab, new Vector3(newPosX, newPosY, 0), Random.rotation, Board.Instance.CreatureGroup.transform);
                var newCreature    = newCreatureObj.GetComponent <Creature>();

                newCreature.name          = NameGenerator.CombineNames(parentnames);
                newCreature.name          = name.Length >= 1 ? NameGenerator.SanitizeName(NameGenerator.MutateName(name)) : NameGenerator.NewName();
                newCreature.CreatureBrain = newBrain;
                newCreature.MouthHue      = newMouthHue;
                newCreature.Parents       = parents;
                newCreature.Generation    = hightestGen + 1;
                newCreature.Energy        = spawnSize;
                newCreature.Density       = Density;
                newCreature.Hue           = newHue;
                newCreature.Saturation    = newSat;
                newCreature.Brightness    = newBri;

                Board.Creatures.Add(newCreature);
            }
        }
    }