Beispiel #1
0
    public void Execute()
    {
        var hitObjects = Physics.OverlapSphere(animal.gameObject.transform.position,
                                               searchRadius, searchLayer);

        if (hitObjects.Length == 0)
        {
            // Just wandering around if no food given
            if (animal.GetEnergyLevel() < 20f)
            {
                // resting.Execute(); // Can't do this, because the energy level will just float above and below 20f
                animal.GetStateMachine().ChangeState(resting);
            }
            else
            {
                wanderAround.Execute();
            }
            return;
        }

        if (animal.GetThirstLevel() > 80f && animal.GetHungerLevel() > 50f)
        {
            animal.GetStateMachine().SwtichToPreviousState();
        }

        var index = -1;

        // Better performance than foreach
        for (int i = 0; i < hitObjects.Length; i++)
        {
            if (hitObjects[i].CompareTag(tagToLookFor))
            {
                this.agent.SetDestination(hitObjects[i].transform.position);
                if (Vector3.Distance(new Vector3(animal.gameObject.transform.position.x, animal.gameObject.transform.position.y, 0f),
                                     new Vector3(hitObjects[i].transform.position.x, hitObjects[i].transform.position.y, 0f)) < 1f)
                {
                    index = i;
                    break;
                }
            }
        }

        if (index > -1)
        {
            animal.GetStateMachine().ChangeState(new DrinkingWater(hitObjects[index].gameObject.GetComponent <Water>(),
                                                                   waterConsumingRate, agent, animal));
        }

        // No food found, stay where it is
    }
Beispiel #2
0
    public void Execute()
    {
        //Debug.Log("looking for resources");

        if (animal.GetHungerLevel() < animal.GetThirstLevel())
        // So far this condition checking should be safe.
        {
            // Debug.Log("Searching for food!");
            animal.GetStateMachine().ChangeState(searchForFood);
        }
        else if (animal.GetHungerLevel() > animal.GetThirstLevel())
        {
            // Debug.Log("Searching for water!");
            animal.GetStateMachine().ChangeState(searchForWater);
        }
        else
        {
            animal.GetStateMachine().ChangeState(new WanderAround(agent, animal.GetSpeed(), animal));
        }
    }
Beispiel #3
0
    public void Execute()
    {
        animal.UpdateEnergyLevel(Time.deltaTime * 2f);
        if (anim != null)
        {
            anim.Play(animationStr);
        }

        if (animal.GetEnergyLevel() >= 80f)
        {
            // No need to rest
            animal.ExitBusy();
            //animal.GetStateMachine().SwtichToPreviousState();
            animal.GetStateMachine().ChangeState(new WanderAround(agent, animal.GetSpeed(), animal));
        }
    }
    public void Execute()
    {
        var hitObjects = Physics.OverlapSphere(animal.gameObject.transform.position,
                                               searchRadius, searchLayer);

        if (hitObjects.Length == 0)
        {
            // Just wandering around if no food given
            //if (animal.GetEnergyLevel() < 20f)
            //{
            //    animal.GetStateMachine().SwtichToPreviousState();
            //    return;
            //}
            //else
            //{
            if (wanderAround.ExecuteManually())
            {
                if (anim != null)
                {
                    // Debug.Log("searching for stuff...");
                    anim.Play(animationStr);
                }
            }
            //}
            return;
        }

        if (animal.GetHungerLevel() > 80.0f)
        {
            // Debug.Log("no longer needs to search for food");
            // Debug.Break();
            // animal.GetStateMachine().SwtichToPreviousState();
            // animal.GetStateMachine().ChangeState(wanderAround);
            animal.ExitBusyWithStateChange(wanderAround);
            return;
        }

        var index = -1;

        // Better performance than foreach
        for (int i = 0; i < hitObjects.Length; i++)
        {
            if (hitObjects[i].CompareTag(tagToLookFor))
            {
                agent.SetDestination(hitObjects[i].transform.position);
                if (Vector3.Distance(new Vector3(animal.gameObject.transform.position.x, animal.gameObject.transform.position.y, 0f),
                                     new Vector3(hitObjects[i].transform.position.x, hitObjects[i].transform.position.y, 0f)) < 1f)
                {
                    index = i;
                    break;

                    // Debug.Log(animal.GetStateMachine().GetCurrentState());
                }
            }
        }

        if (index > -1)
        {
            animal.GetStateMachine().ChangeState(new EatingFood(hitObjects[index].gameObject.GetComponent <Food>(),
                                                                foodConsumingRate, agent, animal));
        }
    }