Exemplo n.º 1
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));
        }
    }
Exemplo n.º 2
0
 private void Update()
 {
     if (Input.GetKeyDown("space"))
     {
         // sleep
         if (animal.GetHungerLevel() > 50f && animal.GetThirstLevel() > 50f)
         {
             animal.UpdateHungerLevel(-40f);
             animal.UpdateThirstLevel(-40f);
             animal.gameObject.transform.localScale *= 1.5f;
         }
     }
 }
Exemplo n.º 3
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
    }