Beispiel #1
0
    void TryHarvestFruit()
    {
        Collider2D hit =
            Physics2D.OverlapCircle(transform.position, harvestRadius, bushesMask);

        if (hit != null)
        {
            BushFruits bush = hit.GetComponent <BushFruits>();
            if (bush.HasFruits())
            {
                audioSource.Play();
                playerMovement.HarvestStopMovement(harvestTime);
                backpack.AddFruits(bush.HarvestFruit());
            }
        }
    }
Beispiel #2
0
    /// <summary>
    /// This is a interesting one, instead of spamming <see cref="Vector2.Distance(Vector2, Vector2)"/> to search for a close bush,
    /// the wolf will start overlapping circles, each one exponentialy bigger than the previous one in a increasing loop, ending as soon as it hits a bush.
    /// <para>The loop finds a bush at around 2-4 iterations in most cases, more or less a median of 3 <see cref="Physics2D.OverlapCircleAll(Vector2, float)"/> calls per method call.</para>
    /// </summary>
    void SearchForTarget()
    {
        target = null;
        for (int i = 1; i < 50; i++) //iterate with a safe value of 50
        {
            Collider2D[] hits =
                Physics2D.OverlapCircleAll(transform.position, Mathf.Exp(i), bushesMask);

            foreach (Collider2D hit in hits)
            {
                if (hit != null && (hit.GetComponent <BushFruits>().HasFruits()) && hit.GetComponent <BushFruits>().enabled == true)
                {
                    target = hit.GetComponent <BushFruits>();
                    break;
                }
            }
            if (target != null)
            {
                break; //end the main for loop once we are sure that target is not null
            }
        }
    }