Example #1
0
    //Reset the balloon for reuse
    public void resetBalloon(GameObject obj)
    {
        BalloonCollider balCol = obj.GetComponentInChildren <BalloonCollider> ();

        balCol.gameObject.transform.localPosition = new Vector3(0, 0, 0);
        balCol.popped     = false;
        balCol.collided   = false;
        balCol.updated    = false;
        balCol.alreadyOff = false;
        balCol.bomb       = false;
        obj.transform.SetParent(this.gameObject.transform);
        obj.SetActive(false);
    }
    //Set up a row of balloons using pooled objects instead of instantiating new prefabs
    void createBalloonsRecycle(int row, int color, string name, int points, bool reset)
    {
        bool  placeBomb = false;
        Color rowColor;
        int   balloonScore;

        //Create the color for the pop particle effect
        if (color == 1)
        {
            rowColor = new Color(240 / 255f, 105 / 255f, 2 / 255f, 1f);        //Orange
        }
        else if (color == 2)
        {
            rowColor = new Color(237 / 255f, 251 / 255f, 0f, 1f);          //Yellow
        }
        else if (color == 3)
        {
            rowColor = new Color(61 / 255f, 255 / 255f, 36 / 255f, 1f);        //Green
        }
        else if (color == 4)
        {
            rowColor = new Color(2 / 255f, 43 / 255f, 240 / 255f, 1f);        //Blue
        }
        else if (color == 5)
        {
            rowColor = new Color(167 / 255f, 0f, 255 / 255f, 1f);          //Purple
        }
        else
        {
            rowColor = new Color(251 / 255f, 5 / 255f, 0f, 1f);          //Red
        }

        offset = .82f;         //Balloon spacing

        GameObject start, target;

        if (row == 1)
        {
            start        = start1;
            target       = target1;
            balloonScore = 30;            // + (4 * controlScript.speedLevel);
        }
        else if (row == 2)
        {
            start        = start2;
            target       = target2;
            balloonScore = 10;            // + (4 * controlScript.speedLevel);
        }
        else
        {
            start        = start3;
            target       = target3;
            balloonScore = 20;            // + (4 * controlScript.speedLevel);
        }

        if (reset)
        {
            resetRowObjects(row);
            controlScript.updateScore(points);              //Update Score, bonus row clear points
        }

        for (int i = 0; i < numBalloons; i++)
        {
            GameObject newBalloon = PoolingManager.pooler.getPooledObject(5);
            float      spacing    = i * offset;
            if (row == 3)               //For balloons moving right to left, spacing is negative
            {
                spacing = -spacing;
            }

            if (newBalloon != null)               //If the gameobject returned from the pooler is available, use it as the next balloon
            {
                BalloonCollider balCol    = newBalloon.GetComponentInChildren <BalloonCollider> ();
                BalloonAnchor   balAnchor = newBalloon.GetComponent <BalloonAnchor> ();
                newBalloon.SetActive(true);
                newBalloon.transform.SetParent(null);

                if (i == 0)                   //Position the balloons
                {
                    newBalloon.transform.position = new Vector2(start.transform.position.x, start.transform.position.y);
                }
                else
                {
                    newBalloon.transform.position = new Vector2(start.transform.position.x + spacing, start.transform.position.y);
                }

                //Place a bomb instead of balloon if needed
                if (bomb && i == bombIndex)
                {
                    balCol.bomb = true;
                    bomb        = false;
                    placeBomb   = true;
                }

                //Initiallize variables
                balCol.turnOnSafe();
                newBalloon.name          = name + i.ToString() + " anchor";
                balCol.name              = name + i.ToString();
                balCol.row               = row;
                balCol.index             = i;
                balCol.color             = rowColor;
                balCol.pointValue        = balloonScore;
                balCol.poppedByExplosion = false;
                balAnchor.begin          = start;
                balAnchor.target         = target;

                //Set the dispay to bomb animation or the correct color balloon
                if (placeBomb)
                {
                    balCol.anim.SetInteger("State", 7);
                    placeBomb = false;
                }
                else
                {
                    balCol.anim.SetInteger("State", color);
                }

                //Save the balloon info to PlayerPrefs
                if (!PlayerPrefs.HasKey(name + i.ToString()))
                {
                    PlayerPrefs.SetInt(name + i.ToString(), 1);
                }

                //Insert the new balloon into the row array
                if (row == 1)
                {
                    row1 [i] = newBalloon;
                }
                else if (row == 2)
                {
                    row2 [i] = newBalloon;
                }
                else
                {
                    row3 [i] = newBalloon;
                }
            }
        }
    }