Beispiel #1
0
    private IEnumerator RespawnBall()
    {
        yield return(new WaitForSeconds(.5f));

        var ballsInWorld = FindObjectsOfType <Ball>();

        if (ballsInWorld.Length < ballCart.maxBalls)
        {
            ballCart.UpdateBalls(1);
        }
    }
    private void HandleGrab()
    {
        GameObject closest  = null;
        float      distance = 999;

        //Loop through all objects the player is colliding with, and find the closest
        foreach (GameObject collidedObj in countCollisions.collisions)
        {
            if (collidedObj.CompareTag("shopSlot") || collidedObj.CompareTag("ballCart") || collidedObj.CompareTag("waveStart"))
            {
                if (Vector2.Distance(transform.position, collidedObj.transform.position) < distance)
                {
                    distance = Vector2.Distance(transform.position, collidedObj.transform.position);
                    closest  = collidedObj;
                }
            }
        }

        //grab hold of the closest object
        if (closest)
        {
            if (closest.CompareTag("shopSlot"))
            {
                var shopSlot = closest.GetComponent <ShopSlot>();
                if (!shopSlot.currentKit)
                {
                    return;
                }
                //picking up a building kit or making another purchase from the store
                bool canAfford = playerBase.SpendGold(shopSlot.currentKit.cost);    //true if have enough money. automatically detracts cost.
                if (canAfford)
                {
                    holding = shopSlot.currentKit.gameObject;
                    holding.transform.SetParent(transform);
                    shopSlot.SellKit();
                    firstRunHolding = true;
                }
                else
                {
                    GameObject x = Instantiate(textPrefab);
                    x.transform.position = transform.position;
                    x.GetComponentInChildren <TextMeshProUGUI>().text = "Not enough money!";
                }
            }
            else if (closest.CompareTag("ballCart") && ballCart.currentBalls > 0)
            {
                ballCart.UpdateBalls(-1);

                //picking up a ball or another holdable object
                holding = Instantiate(unthrownBallPrefab, transform);
                //holding.transform.SetParent(transform);
                firstRunHolding = true;
            }
            else if (closest.CompareTag("waveStart"))
            {
                FindObjectOfType <WaveManager>().StartWave();
                BuildingKit[] kits = FindObjectsOfType <BuildingKit>();
                foreach (BuildingKit kit in kits)
                {
                    Destroy(kit.gameObject);
                }
            }
        }
    }