Пример #1
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);
                }
            }
        }
Пример #2
0
        private void ApplyRules()
        {
            Vector3 vavoid = Vector3.zero, vcenter = Vector3.zero;
            float   groupSpeed = 0;
            int     groupSize  = 0;

            foreach (GameObject fish in Flock.Fishes)
            {
                float dist = Vector3.Distance(fish.transform.position, transform.position);
                if (dist <= NeighborDistance)
                {
                    vcenter += fish.transform.position;
                    groupSize++;
                    if (dist <= NeighborAvoidance && dist > 0)
                    {
                        vavoid += transform.position - fish.transform.position;
                    }
                    groupSpeed += fish.GetComponent <Fish>()._speed;
                }
            }

            vcenter /= groupSize;
            vcenter += vavoid;
            Vector3 goalPos = vcenter;

            float min = float.MaxValue;

            foreach (GameObject food in Flock.FishFoods)
            {
                if (!food.activeSelf)
                {
                    continue;
                }
                float tmp = Vector3.Distance(transform.position, food.transform.position);
                if (tmp > _vision)
                {
                    continue;
                }
                FishFood fishFood = food.GetComponent <FishFood>();
                tmp -= fishFood.GetEnergy() - fishFood.GetWaste() + Inertia;
                if (tmp < min)
                {
                    min     = tmp;
                    goalPos = food.transform.position;
                }
            }

            if (Vector3.Distance(transform.position, goalPos) <= Stop)
            {
                _speed = MinSpeed;
            }
            else
            {
                _speed = MaxSpeed - 1;
            }

            groupSpeed += _speed;
            vcenter    += goalPos - transform.position;
            vcenter    -= transform.position;
            if (vcenter == Vector3.zero)
            {
                return;
            }
            _speed             = Mathf.Clamp(groupSpeed / groupSize, MinSpeed, MaxSpeed);
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(vcenter), RotationSpeed);
        }