Eat() public method

public Eat ( ) : bool
return bool
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            //Get mouse position
            Vector3 mousePos = Input.mousePosition;
            Vector3 target   = Camera.main.ScreenToWorldPoint(mousePos);
            target.z = transform.position.z;

            //If the mouse is not inside the circle, return
            if (Vector3.Distance(target, transform.position) > radius)
            {
                Debug.Log("returned " + target);
                return;
            }

            //Check all colliders and eat any food
            Collider2D[] colliders = Physics2D.OverlapCircleAll(target, 1f);
            foreach (Collider2D collider in colliders)
            {
                Food food = collider.GetComponent <Food>();

                if (food != null)
                {
                    food.Eat();
                    return;
                }
            }
        }
    }
示例#2
0
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (isHit || !isReady)
        {
            return;
        }

        if (other.gameObject.CompareTag("Food") || other.gameObject.CompareTag("Giant Carrot"))
        {
            Food incomingFood = other.GetComponent <Food>();

            switch (incomingFood.FoodEnum)
            {
            case Food.EFood.CARROT:
                Love(incomingFood.GetPlayerNo());
                break;

            case Food.EFood.GIANT_CARROT:
                Love(incomingFood.GetPlayerNo(), false);
                break;

            case Food.EFood.SHIT:
                Hate(incomingFood.GetPlayerNo());
                break;
            }

            isHit = true;
            incomingFood.Eat();
        }
    }
示例#3
0
 void OnTriggerStay(Collider collider)
 {
     if (collider.gameObject.GetComponent <Food>() == food)
     {
         Energy += food.Eat();
     }
 }
示例#4
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        switch (collision.tag)
        {
        case "Food":
            // Get the food component in the object tagged food.
            Food food = collision.GetComponent <Food>();
            // Make sure the food have a food script attached to it.
            Assert.IsNotNull(food, $"Food without food component. Game objects name: {collision.name}");

            // Call eat, add growth to the snake and remove time.
            food.Eat();
            snake.GrowAmount += food.GrowAmount;
            GameController.Instance.RemoveTime(food.TimeRemoval);
            break;

        case "Snake":
            // The snake ate itself. Die.
            GameController.Instance.Die();
            Debug.Log("I ate myself :(");
            break;

        case "Wall":
            // The snake tried to eat a wall. Die.
            GameController.Instance.Die();
            Debug.Log("I broke my teeth on a wall :(");
            break;

        default:
            Debug.Log($"I hit a: {collision.tag}");
            break;
        }
    }
示例#5
0
    IEnumerator EatFood(GameObject foodObject)
    {
        if (!eatAudio.isPlaying)
        {
            eatAudio.pitch = Random.Range(1f, 2f);
            eatAudio.Play();
        }

        isEating = true;
        IncreaseSpeed();
        currentState = States.eating;
        Food      f = foodObject.GetComponent <Food>();
        Transform lastBodySpawnPos = controller.GetLastBodySpawnPos();

        f.Eat(lastBodySpawnPos.position);
        score += f.points;
        if (score > highScore)
        {
            highScore = score;
        }
        controller.AddBody(controller.startingBody);
        currentState = States.moving;
        yield return(new WaitForSeconds(0.25f));

        isEating = false;
    }
示例#6
0
 public void EatFood()
 {
     if (foodInSlot == null)
     {
         return;
     }
     foodInSlot.Eat();
 }
示例#7
0
        public override void OnClick()
        {
            if (m_Food.Deleted || !m_Food.Movable || !m_From.CheckAlive() || !m_Food.CheckItemUse(m_From))
            {
                return;
            }

            m_Food.Eat(m_From);
        }
示例#8
0
    private void OnTriggerEnter(Collider other)
    {
        Food food = other.GetComponent <Food>();

        if (food == null)
        {
            return;
        }
        IncreaseEnergy(food.Eat());
    }
示例#9
0
 void OnCollisionEnter(Collision coll)
 {
     if (coll.gameObject.layer == m_EnvironmentLayer)
     {
         m_State = BugState.OnGround;
     }
     if (coll.gameObject.layer == m_FoodLayer)
     {
         Food food = coll.gameObject.GetComponent <Food>();
         if (food.HP > 0)
         {
             food.Eat(FoodHPPerByte.value);
             DropPheromone(Pheromone.PheromoneType.Food, coll.gameObject.transform.position);
         }
     }
 }
示例#10
0
 public void OnControllerColliderHit(ControllerColliderHit hit)
 {
     if (_testing)
     {
         Food food = hit.collider.GetComponent <Food>();
         if (food != null)
         {
             food.Eat();
         }
         else
         {
             Debug.Log(hit.gameObject.name);
             SceneManager.LoadScene("GameOver");
         }
         _testing = false;
     }
 }
示例#11
0
    IEnumerator EatRoutine(Food nearbyFoodPiece)
    {
        while (foodFullness < maxFullness)
        {
            if (nearbyFoodPiece != null)
            {
                nearbyFoodPiece.Eat(eatPower);
                foodFullness += eatPower;

                transform.DOShakePosition(.5f);
                yield return(new WaitForSeconds(.5f));
            }
            else
            {
                //No food nearby, exit routine
                break;
            }
        }
        State = AnimalState.IDLE;
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            Vector3 target = Vector3.zero;

            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000, groundLayer))
            {
                target = hit.point + new Vector3(0, 0.5f, 0);
            }

            //If the mouse is not inside the circle, return
            if (Vector3.Distance(target, transform.position) > radius)
            {
                Debug.Log("returned " + target);
                return;
            }

            //Check all colliders and eat any food
            Collider[] colliders = Physics.OverlapSphere(target, 1f);
            foreach (Collider collider in colliders)
            {
                Food food = collider.GetComponent <Food>();

                if (food != null)
                {
                    //If we have enough appetite to eat this food
                    if (healthStats.appetiteCurrentLevel - food.appetiteFilling >= 0)
                    {
                        food.Eat();                         //Delete the food
                        healthStats.EatFood(food);          //Update the player health
                        return;
                    }
                }
            }
        }
    }
示例#13
0
    public void Move(Field next)
    {
        var last = tail.Last();

        if (!next.IsWalkable())
        {
            // Die();
            Debug.Log("dead");
            return;
        }

        if (next.IsFood())
        {
            food.Eat();
            AddTail(last);
            Slither(next);
        }
        else
        {
            last.ChangeField(Map.Fields.empty);
            Slither(next);
        }
    }
示例#14
0
    public void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (testing)
        {
            Food food = hit.collider.GetComponent <Food>();
            if (food != null)
            {
                food.Eat();
                sparkle.transform.position = food.transform.position;
                sparkle.SetActive(true);

                audio.PlayOneShot(eatSound);

                speed         += 0.2f;
                rotationSpeed += 5;
                AddTail();
            }
            else
            {
                if (hit.collider.tag == "Rock")
                {
                    GameManager.lives -= 1;
                    GameManager.size   = 0;
                    if (GameManager.lives > 0)
                    {
                        Application.LoadLevel(sceneName);
                    }
                    else
                    {
                        Application.LoadLevel("GameOver");
                        audio.PlayOneShot(gameOverSound);
                    }
                }
            }
            testing = false;
        }
    }
示例#15
0
    private void OnCollisionEnter(Collision collision)
    {
        if (mState != AlState.MovingToFood && mState != AlState.LookingForFood)
        {
            //ignore the collision if the life form doesn't actively wants to eat
            return;
        }

        //check if collusion object still exists
        if (collision != null && collision.gameObject != null)
        {
            //is it food?
            Food f = collision.gameObject.GetComponent <Food>();
            if (f != null && f.IsEaten == false)
            {
                if (status._Eaten < Config.Status.MAX_FOOD)
                {
                    if (VERBOSE_LOG)
                    {
                        Debug.Log(this.gameObject.name + " ate " + f.name);
                    }

                    //Using Destroy alone will make the results undeterministic as it
                    //depends on the CPU time / FPS when the object will actually be deleted
                    f.Eat();
                    f.gameObject.SetActive(false);
                    Destroy(f.gameObject);
                    status._Eaten++;

                    if (status._Eaten == Config.Status.MAX_FOOD)
                    {
                        mState = AlState.Napping;
                    }
                }
            }
        }
    }
示例#16
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            SetState(SeagullState.FLYING);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            SetState(SeagullState.LANDING);
        }

        switch (state)
        {
        case SeagullState.IDLE:
            foreach (var food in Food.foodList)
            {
                if (Vector3.Distance(food.transform.position, transform.position) < foodAggroDistance &&
                    food.isPickedUp == false)
                {
                    selectedFood = food;
                    SetState(SeagullState.WALKING_TO_FOOD);
                }
            }

            wanderCooldownCurrent += Time.deltaTime;
            if (wanderCooldownCurrent >= wanderCooldown)
            {
                Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1);
                agent.SetDestination(newPos);
                wanderCooldownCurrent = 0;
            }
            break;

        case SeagullState.FLYING:

            if (mySeagull)
            {
                //Check for landing reasons
                foreach (var food in Food.foodList)
                {
                    if (Vector3.Distance(food.transform.position, SeagullManager.Instance.landPoint.position) < foodAggroDistance &&
                        food.isPickedUp == false)
                    {
                        selectedFood = food;
                        SetState(SeagullState.LANDING);
                    }
                }
            }

            transform.position = Vector3.Slerp(movePointStart, movePointFinish, moveProgress);
            //transform.rotation = Quaternion.Slerp(moveRotationStart, Quaternion.identity, moveProgress);

            Vector3    direction  = movePointFinish - movePointStart;
            Quaternion toRotation = Quaternion.FromToRotation(transform.forward, direction);
            transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, moveProgress);

            moveProgress += flySpeed * Time.deltaTime;
            if (moveProgress >= 1f)
            {
                SetState(SeagullState.FLYING);
            }

            break;

        case SeagullState.LANDING:
            transform.position = Vector3.Slerp(movePointStart, SeagullManager.Instance.landPoint.position, moveProgress);
            transform.rotation = Quaternion.Lerp(moveRotationStart, Quaternion.identity, moveProgress);
            moveProgress      += flySpeed * Time.deltaTime;
            if (moveProgress >= 1f)
            {
                SetState(SeagullState.IDLE);
            }
            break;

        case SeagullState.TAKEOFF:
            transform.position = Vector3.Slerp(movePointStart, SeagullManager.Instance.GetRandomFlyPoint(), moveProgress);
            transform.rotation = Quaternion.Lerp(moveRotationStart, Quaternion.identity, moveProgress);
            moveProgress      += flySpeed * Time.deltaTime;
            if (moveProgress >= 1f)
            {
                SetState(SeagullState.FLYING);
            }
            break;

        case SeagullState.WALKING_TO_FOOD:
            agent.SetDestination(selectedFood.transform.position);
            if (Vector3.Distance(selectedFood.transform.position, transform.position) < .5f)
            {
                SetState(SeagullState.EATING);
            }
            break;

        case SeagullState.WALKING_TO_TAKEOFF:
            agent.SetDestination(SeagullManager.Instance.landPoint.position);
            if (Vector3.Distance(SeagullManager.Instance.landPoint.position, transform.position) < .3f)
            {
                SetState(SeagullState.TAKEOFF);
            }
            break;

        case SeagullState.EATING:
            bool finishedEating = this.animator.GetCurrentAnimatorStateInfo(0).IsName("peck") == false;
            if (finishedEating)
            {
                selectedFood.Eat();

                foodPoints++;
                RecalculateScale();

                if (foodPoints >= 3)
                {
                    SetState(SeagullState.FOLLOWING);
                }
                else
                {
                    SetState(SeagullState.WALKING_TO_TAKEOFF);
                }
            }
            break;

        case SeagullState.FOLLOWING:
            var playerT = Player.Instance.transform;
            agent.SetDestination(playerT.position + playerT.forward);
            break;
        }
    }