Exemple #1
0
    void Update()
    {
        // Shrink the dots in list of dots to destroy
        foreach (GameObject dotToDestroy in dotsToDestroy)
        {
            ShrinkDot(dotToDestroy);
        }

        // Touch handling
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            // If user's finger is touching the screen, figure out which dots the user's picking up
            if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
            {
                RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);

                // Touched a dot
                if (hit.collider != null && hit.collider.gameObject.name == "Dot(Clone)")
                {
                    GameObject dotTouched       = hit.collider.gameObject;
                    DotScript  dotTouchedScript = dotTouched.GetComponent("DotScript") as DotScript;

                    // Determine if we should add the newly touched dot to the list of connected dots.
                    // If the new dot passed all the tests, add dot to list of connected dots
                    if (ShouldAddDotToList(dotTouched, dotTouchedScript))
                    {
                        dotsTouched.Add(dotTouched);

                        // If this is the first dot in the list, set the line color of the connecting line to be the color of the dot
                        if (dotsTouched.Count == 1)
                        {
                            vectorLineScript.lineColor = colorIDs[dotTouchedScript.colorID];
                        }

                        // Connect this dot to the line
                        vectorLineScript.AddPoint(dotTouched.transform.position);
                    }
                }

                // When user releases the touch, it's time to destroy some dots
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                if (dotsTouched.Count > 1)
                {
                    DotScript lastDotInListScript = dotsTouched [dotsTouched.Count - 1].GetComponent("DotScript") as DotScript;

                    // Keeps track of which columns need replenishing after destroying dots.
                    // The index is the column number, and the value is the number of dots to
                    // replenish in that column
                    int[] dotsToReplenish = new int[gridWidth];

                    // If dots form a square, destroy all dots on board of matching color
                    if (FormsSquare(dotsTouched))
                    {
                        dotsToReplenish = ShrinkDotsWithColorID(lastDotInListScript.colorID);

                        // Replenish the board with dots, excluding the color that was just square'd away
                        StartCoroutine(ReplenishExcludingColorID(lastDotInListScript.colorID, dotsToReplenish));

                        // Otherwise, destroy the string of connected dots
                    }
                    else
                    {
                        foreach (GameObject dotTouched in dotsTouched)
                        {
                            // Figure out coordinates of dot destroyed and note it in the replenish list
                            Point point = ScreenCoordsToPoint(dotTouched.transform.position);
                            dotsToReplenish [point.x] += 1;

                            // Prepare the dot for destruction
                            dotsToDestroy.Add(dotTouched);
                        }
                        StartCoroutine(ReplenishExcludingColorID(-1, dotsToReplenish));
                    }
                    StartCoroutine(DestroyDotsAfterDelay(0.5f));
                }

                // Clear the list of dots touched and clear the line
                if (dotsTouched.Count > 0)
                {
                    dotsTouched.Clear();
                    vectorLineScript.ClearLine();
                }
            }
        }
    }