private void UpdatePredator(Predator predator, Velocity velocity, Settings settings)
        {
            predator.Velocity    = BoidHelper.AdjustVelocity(velocity, settings.MaxVelocityBoid);
            predator.Position.X += velocity.X;
            predator.Position.Y += velocity.Y;

            //Out of bounce
            predator.Position.X = (predator.Position.X > 100) ? predator.Position.X - 100 : (predator.Position.X < 0) ? predator.Position.X + 100 : predator.Position.X;
            predator.Position.Y = (predator.Position.Y > 100) ? predator.Position.Y - 100 : (predator.Position.Y < 0) ? predator.Position.Y + 100 : predator.Position.Y;

            //Angle
            predator.Velocity.Deg = Math.Atan2(predator.Position.Y, predator.Position.X) * 180.0 / Math.PI;
        }
示例#2
0
        private void Reproduce(Predator other)
        {
            float      aux   = _globalFlock.EnvironmentSize - 2;
            Vector3    pos   = Random.insideUnitSphere * aux;
            GameObject whale = Instantiate(_globalFlock.PredatorPrefab, pos, Quaternion.identity);

            Flock.Predators.Add(whale);
            whale.GetComponent <Predator>().Inherit(
                Random.value >= 0.5f ? _vision : other._vision,
                Random.value >= 0.5f ? _expectedLife : other._expectedLife,
                Random.value >= 0.5f ? _capacity : other._capacity,
                Random.value >= 0.5f ? _metabolism : other._metabolism,
                _energy + other._energy
                );
            _nextReproduction = Time.time + RestReproduction * Random.value >= FertilityChance ? 0.5f : 1f;
        }
示例#3
0
        private void OnTriggerEnter(Collider other)
        {
            if (other.CompareTag("Tree"))
            {
                _energy   = Mathf.Min(_energy + other.GetComponent <FishFood>().Eat(_bite), _capacity);
                _lastTree = other.gameObject.GetComponent <FishFood>();
            }
            else if (other.CompareTag("Predator"))
            {
                Predator predator = other.GetComponent <Predator>();
                if (predator.Energy >= predator.Capacity)
                {
                    return;
                }
                predator.Eat();
                Die();
            }
            else if (!_isMale && other.CompareTag("Fish"))
            {
                Fish fish = other.gameObject.GetComponent <Fish>();
                if (fish._isMale == _isMale || Time.time < _nextReproduction || Time.time < fish._nextReproduction ||
                    fish._energy <= fish._capacity / 2.0f || _energy <= _capacity / 2.0f)
                {
                    return;
                }

                if (Random.value > NoDiscriminationChance)
                {
                    float alpha1     = transform.GetChild(0).gameObject.GetComponent <InhibitorActivator>().GetAlpha();
                    float alpha2     = other.transform.GetChild(0).gameObject.GetComponent <InhibitorActivator>().GetAlpha();
                    float skinFactor = Mathf.Abs(alpha1 - alpha2);
                    float dist       = Vector3.Distance(transform.localScale, other.transform.localScale);
                    if (skinFactor > MaxSDifference || dist > MaxTDifference)
                    {
                        return;
                    }
                }

                fish.UpdateNexReproduction();
                UpdateNexReproduction();
                Reproduce(fish);
                if (Random.value <= DoubleChildProb)
                {
                    Reproduce(fish);
                }
            }
        }
        public Predator CreateNewPredator()
        {
            var predator = new Predator
            {
                Position = new Position {
                    X = BoidHelper.GetRandomNumber(0, 800), Y = BoidHelper.GetRandomNumber(0, 600)
                },
                Velocity = new Velocity {
                    X = BoidHelper.GetRandomNumber(-10, 10), Y = BoidHelper.GetRandomNumber(-10, 10)
                }
            };

            predator.Velocity = BoidHelper.AdjustVelocity(predator.Velocity, 5);

            //Out of bounce
            predator.Position.X = (predator.Position.X > 800) ? predator.Position.X - 800 : (predator.Position.X < 0) ? predator.Position.X + 800 : predator.Position.X;
            predator.Position.Y = (predator.Position.Y > 600) ? predator.Position.Y - 600 : (predator.Position.Y < 0) ? predator.Position.Y + 600 : predator.Position.Y;


            //Angle
            predator.Velocity.Deg = Math.Atan2(predator.Position.Y, predator.Position.X) * 180.0 / Math.PI;

            return(predator);
        }
示例#5
0
        private void Update()
        {
            if (Time.time >= _nextSeason)
            {
                _nextSeason = Time.time + SeasonRate;
                int a = Mathf.RoundToInt(Random.Range(0, _soureces.Count - 0.51f));
                int i = 0;
                foreach (List <GameObject> l in _soureces)
                {
                    bool active = Random.value <= SourceChance || a == i;
                    foreach (GameObject plant in l)
                    {
                        plant.SetActive(active);
                    }
                    i++;
                }
            }

            if (!_predators && Predator.MalesAlive + Predator.FemalesAlive < 2)
            {
                Predators.Add(Instantiate(PredatorPrefab, Vector3.zero, Quaternion.identity));
            }

            if (_predators && Time.time >= AppearanceTime)
            {
                _predators = false;
                float aux1 = EnvironmentSize - 2f;
                for (int i = 0; i < PredatorsNum; i++)
                {
                    Vector3 tmp = new Vector3(
                        Random.Range(-aux1, aux1),
                        Random.Range(-aux1, aux1),
                        Random.Range(-aux1, aux1));
                    Predators.Add(Instantiate(PredatorPrefab, tmp, Quaternion.identity));
                }
            }

            if (Time.time >= _nextMating)
            {
                if (_matingSeason)
                {
                    _matingSeason = false;
                    _nextMating  += MatingSeason;
                    foreach (GameObject whale in Predators)
                    {
                        Predator predator = whale.GetComponent <Predator>();
                        if (!predator.IsMale)
                        {
                            predator.Mating = false;
                        }
                    }
                    if (_bestWhale != null)
                    {
                        _bestWhale.SetAlpha(false);
                    }
                }
                else
                {
                    float maxEnergy = 0;
                    foreach (GameObject whale in Predators)
                    {
                        Predator predator = whale.GetComponent <Predator>();
                        if (predator.IsMale && predator.Energy > maxEnergy)
                        {
                            maxEnergy  = predator.Energy;
                            _bestWhale = predator;
                        }
                    }

                    if (_bestWhale != null)
                    {
                        foreach (GameObject whale in Predators)
                        {
                            Predator predator = whale.GetComponent <Predator>();
                            if (!predator.IsMale)
                            {
                                predator.Mate(_bestWhale.gameObject);
                            }
                        }
                        _bestWhale.SetAlpha(true);
                        _bestWhale.Energy /= 2;
                        _matingSeason      = true;
                        _nextMating       += MatingDuration;
                    }
                    else
                    {
                        _nextMating += MatingSeason;
                    }
                }
            }
        }