Ejemplo n.º 1
0
    private void Hunger(float delta)
    {
        if (_hungerState != HungerState.Eating)
        {
            _currentHungerValue -= delta * _hungerDeleptionRate;
            _currentHungerValue  = Mathf.Max(0, _currentHungerValue);
        }

        switch (_hungerState)
        {
        case HungerState.Sated:
            _targetFood = null;
            if (_currentHungerValue <= 0)
            {
                _hungerState = HungerState.SearchingForFood;
            }

            break;

        case HungerState.SearchingForFood:
            _targetFood = null;
            float closestDist = Mathf.Infinity;
            foreach (Food food in FindObjectsOfType <Food>())
            {
                if (food.RemainingFood() >= 1 && food.BeingEatenBy().Count == 0)
                {
                    float dist = Vector3.Distance(transform.position, food.transform.position);
                    if (dist < closestDist)
                    {
                        _targetFood = food;
                        closestDist = dist;
                    }
                }
            }
            if (_targetFood != null)
            {
                _targetFood.AddEater(this);
                _hungerState = HungerState.MovingToFood;
                _navigator.SetCurrentNavPosition(transform.position);
                _navigator.SetTarget(_targetFood);
            }

            break;

        case HungerState.MovingToFood:
            if (_targetFood.BeingEatenBy().Count >= 2)
            {
                _targetFood  = null;
                _hungerState = HungerState.SearchingForFood;
            }
            else if (_navigator.HasReachedTarget())
            {
                _hungerState = HungerState.Eating;
            }

            break;

        case HungerState.Eating:
            if ((_eatTimer -= delta) <= 0)
            {
                _eatTimer = _eatTime;
                _targetFood.EatFromFood();
                _currentHungerValue += 0.1f;

                if (_currentHungerValue >= 1)
                {
                    _targetFood.RemoveFoodEater(this);
                    _targetFood = null;
                    _navigator.SetTarget(null);
                    _hungerState = HungerState.Sated;
                }
                else if (_targetFood.RemainingFood() <= 0)
                {
                    _targetFood.RemoveFoodEater(this);
                    _targetFood = null;
                    _navigator.SetTarget(null);
                    _hungerState = HungerState.SearchingForFood;
                }
            }

            break;
        }
    }