Example #1
0
    private void IdentifyClosestfood()
    {
        // we need to adjust the line of sight for carnivorous creatures as opposed to
        // herbivorous creatures -simply to ensure that they can find the player
        TargetFood = null;                      // reference to the script of the closest food
        GameObject closest = null;
        float      dist    = Mathf.Infinity;

        if (_myCreature.TypeOfCreature == "Carnivorous")
        {
            ColliderCloud = Physics.OverlapSphere(_myTransform.position, (float)_lineOfSight);

            int l = Random.Range(0, 100);
            // 70% chance that the creature will just target the player (resets every 5 seconds)
            if (l >= 0 && l < 70)
            {
                GameObject f = GameObject.FindGameObjectWithTag("Player");
                Vector3    locationDifference = f.transform.position - _myTransform.position;
                float      currDist           = locationDifference.magnitude;
                if (currDist < dist)
                {
                    closest = f;
                    dist    = currDist;
                }

                if (f.tag == "Player")
                {
                    EffectTrigger et = f.GetComponent <EffectTrigger>();
                    et.TriggerAudio();
                }

                float _tempRange = 2.5f;
                if (currDist < _tempRange && (_myCreature.State == Creature.CurrentState.PersuingFood))
                {
                    if (f.tag == "Player")
                    {
                        PlayerStats   ps = f.GetComponent <PlayerStats>();
                        EffectTrigger et = f.GetComponent <EffectTrigger>();
                        _myCreature.Energy += 100f;
                        ps.RemoveEnergy(20f);
                        et.PlayerEaten(f.transform.position);
                        f.GetComponent <ControllerSetup>().AccelerateAway();
                        ps.RemoveFood();
                        _myCreature.FoodEaten++;
                        ps.AddDevour(1);
                    }
                }
            }
            else
            {
                foreach (Collider c in ColliderCloud)
                {
                    GameObject f = c.gameObject;

                    if (f)
                    {
                        f = f.transform.root.gameObject;
                        if (f.tag == "Creature" || f.tag == "Player")
                        {
                            _myCreature.TargetFood = f.gameObject;
                            Vector3 locationDifference = f.transform.position - _myTransform.position;
                            float   currDist           = locationDifference.magnitude;
                            if (currDist < dist)
                            {
                                closest = f;
                                dist    = currDist;
                            }

                            if (f.tag == "Player")
                            {
                                EffectHandler et = f.GetComponent <EffectHandler>();
                                et.TriggerAudio();
                            }

                            float tempRange = 2.5f;
                            if (currDist < tempRange && (_myCreature.State == Creature.CurrentState.PersuingFood))
                            {
                                if (f.tag == "Player")
                                {
                                    PlayerStats   ps = f.GetComponent <PlayerStats>();
                                    EffectHandler et = f.GetComponent <EffectHandler>();
                                    _myCreature.Energy += 100f;
                                    ps.RemoveEnergy(20f);
                                    et.PlayerEaten(f.transform.position);
                                    f.GetComponent <ControllerSetup>().AccelerateAway();
                                    ps.RemoveFood();
                                    _myCreature.FoodEaten++;
                                    ps.AddDevour(1);
                                }
                                else if (f.GetComponent <Creature>().TypeOfCreature == "Herbiverous")
                                {
                                    Creature ct = f.GetComponent <Creature>();
                                    ct.Energy          -= 100f;
                                    _myCreature.Energy += 100f;
                                }
                            }

                            if (currDist < (float)_creatureEatRange && (_myCreature.State == Creature.CurrentState.PersuingFood))
                            {
                                if (f.name == "food" && _myCreature.TypeOfCreature == "Herbiverous")
                                {
                                    _fbit = f.GetComponent <Food>();
                                    _myCreature.Energy += _fbit.Energy;
                                    _fbit.Destroy();
                                    _myCreature.FoodEaten++;
                                }
                            }
                        }
                    }
                }
            }
        }
        else
        {
            ColliderCloud = Physics.OverlapSphere(_myTransform.position, (float)_lineOfSight);

            foreach (Collider c in ColliderCloud)
            {
                GameObject f = c.gameObject;

                if (f && f.name == "food" || f)
                {
                    _myCreature.TargetFood = f.gameObject;
                    Vector3 diff     = f.transform.position - _myTransform.position;
                    float   currDist = diff.magnitude;
                    if (currDist < dist)
                    {
                        closest = f;
                        dist    = currDist;
                    }

                    if (currDist < (float)_creatureEatRange && (_myCreature.State == Creature.CurrentState.PersuingFood))
                    {
                        if (f.name == "food" && _myCreature.TypeOfCreature == "Herbiverous")
                        {
                            _fbit = f.GetComponent <Food>();
                            _myCreature.Energy += _fbit.Energy;
                            _fbit.Destroy();
                            _myCreature.FoodEaten++;
                        }
                    }
                }
            }
        }

        DistanceToGoal = 0F;
        if (closest)
        {
            TargetFood = closest.gameObject;
        }

        Goal = TargetFood;
        if (Goal)
        {
            DistanceToGoal = DistanceUntilGoal();
        }
    }