Exemplo n.º 1
0
    /*
     * This will regulary retrieve and handle the user's input
     * 1 - If we're not already combining elements, this will detect the first element touched
     * 2 - If we are combining elements, this will handle the dragging and combination of elements
     */
    private void UpdateInput()
    {
        // Did we first touch the screen
        if (inputController.IsTouched())
        {
            // Ignore hint button touch
            Vector3 touchScreenPosition = Camera.main.WorldToScreenPoint(inputController.GetTouchingPosition());
            Vector3 buttonPosition      = hintButton.transform.position;
            Vector3 buttonSize          = new Vector3(hintButtonRect.rect.width, hintButtonRect.rect.height);
            if (GameMath.IsInRange(touchScreenPosition, buttonPosition, buttonSize, 0.5f))
            {
                return;
            }

            // Get the touch position
            Vector2 touchingPosition = inputController.GetTouchingPosition();
            Vector3 touchPosition    = new Vector3(touchingPosition.x, touchingPosition.y, 0f);
            // Loop through the objects
            Vector3 position = new Vector3();
            Vector3 bounds   = elementSpriteRenderer.bounds.size;
            foreach (GameObject currentElement in elements)
            {
                // Set the element's position
                position = currentElement.transform.position;
                // Did we touch it ?
                if (GameMath.IsInRange(touchPosition, position, bounds, 0.8f))
                {
                    // Stop playing hint animations if available
                    if (playingHint)
                    {
                        // Stop hint animations on hint elements
                        firstHintElement.GetComponent <Animation>().Stop("ElementHinted");
                        secondHintElement.GetComponent <Animation>().Stop("ElementHinted");
                        // Restore to original size
                        firstHintElement.transform.localScale  = new Vector3(0.25f, 0.25f, 1f);
                        secondHintElement.transform.localScale = new Vector3(0.25f, 0.25f, 1f);
                        // Reset playing hint flag
                        playingHint = false;
                    }
                    // Set it as the first touched element
                    combinedElements.Add(currentElement);
                    // Set the last touched element
                    lastTouchedElement = currentElement;
                    // Get the current element's color
                    Color color = currentElement.GetComponent <SpriteRenderer>().color;
                    // Create a new line
                    CreateLine(color);
                    // Play the element touched animation
                    currentElement.GetComponent <Animation>().Play("ElementTouched", PlayMode.StopAll);
                    // Get the element's number
                    Element elementScript = currentElement.GetComponent <Element>();
                    long    number        = elementScript.GetNumber();
                    currentPower = elementScript.GetPower() + 1;
                    combinedSum += number;
                    lastNumber   = number;
                    // Play touched audio
                    audioSource.PlayOneShot(elementSelectionAudio);
                    break;
                }
            }
        }
        // Are we dragging ?
        else if (inputController.IsDragging())
        {
            // Ensure we have a first element
            if (combinedElements.Count == 0)
            {
                return;
            }

            // Get the touch position
            Vector2 touchingPosition = inputController.GetTouchingPosition();
            Vector3 touchPosition    = new Vector3(touchingPosition.x, touchingPosition.y, 0f);
            // Change the line's position
            currentLineRenderer.SetPosition(0, lastTouchedElement.transform.position);
            currentLineRenderer.SetPosition(1, touchPosition);
            // Loop through the objects
            Vector3 position = new Vector3();
            Vector3 bounds   = elementSpriteRenderer.bounds.size;
            bounds.x /= 2f;
            bounds.y /= 2f;
            foreach (GameObject currentElement in elements)
            {
                // Is it one of the surrounding elements ?
                if (GameMath.GetDistanceBetweenVectors(currentElement.transform.position,
                                                       lastTouchedElement.transform.position) > elementSpriteRenderer.bounds.size.x + DISTANCE_BETWEEN_ELEMENTS * 3f)
                {
                    continue;
                }

                // Skip already touched elements
                if (combinedElements.Contains(currentElement) && currentElement != connectedElement)
                {
                    // If its the last touched element then remove it from the list
                    continue;
                }
                // Set the element's position
                position = currentElement.transform.position;
                // Did we touch it ?
                if (GameMath.IsInRange(touchPosition, position, bounds, 1.15f))
                {
                    // Is it the connected element ?
                    if (currentElement == connectedElement)
                    {
                        // Remove the current line from the lines array
                        lines.Remove(currentLine);
                        // Destroy the current line
                        Destroy(currentLine);
                        // Set the current line to the one projected from the connected element
                        currentLine         = (GameObject)lines[lines.Count - 1];
                        currentLineRenderer = currentLine.GetComponent <LineRenderer>();
                        // Remove the current element from the combined elements list
                        combinedElements.Remove(lastTouchedElement);
                        // Point the last touched element to this one element
                        lastTouchedElement = connectedElement;
                        // Get element script
                        Element elementScript = lastTouchedElement.GetComponent <Element>();
                        // Restore the power
                        currentPower = elementScript.GetPower() + 1;
                        // Remove its points
                        long number = elementScript.GetNumber();
                        combinedSum -= number;
                        lastNumber   = number;
                        // Set the connected element
                        if (combinedElements.Count > 1)
                        {
                            connectedElement       = (GameObject)combinedElements[combinedElements.Count - 2];
                            connectedElementNumber = connectedElement.GetComponent <Element>().GetNumber();
                        }
                        else
                        {
                            connectedElement       = null;
                            connectedElementNumber = 0;
                        }
                    }
                    else
                    {
                        // Can we combine it ?
                        long number = currentElement.GetComponent <Element>().GetNumber();
                        if (number != lastNumber && number != connectedElementNumber + lastNumber)
                        {
                            continue;
                        }

                        // Increase power if necessary
                        if (number != lastNumber)
                        {
                            currentPower++;
                        }

                        // Change the line's position to hit the center of this element
                        currentLineRenderer.SetPosition(0, lastTouchedElement.transform.position);
                        currentLineRenderer.SetPosition(1, currentElement.transform.position);
                        // Set it as the first touched element
                        combinedElements.Add(currentElement);
                        // Set the connected element
                        connectedElement = lastTouchedElement;
                        // Set the connected element's number
                        connectedElementNumber = lastNumber;
                        // Update the last number
                        combinedSum += number;
                        lastNumber   = number;
                        // Set the last touched element
                        lastTouchedElement = currentElement;
                        // Get the current element's color
                        Color color = currentElement.GetComponent <SpriteRenderer>().color;
                        // Create a new line
                        CreateLine(color);
                        // Play the element touched animation
                        currentElement.GetComponent <Animation>().Play("ElementTouched", PlayMode.StopAll);
                        // Play touched audio
                        audioSource.PlayOneShot(elementSelectionAudio);
                    }
                    break;
                }
            }
        }
        // Clear all elements after dragging
        else if (combinedElements.Count != 0)
        {
            // If its a single element then clear the combined elements only
            if (combinedElements.Count == 1)
            {
                combinedElements.Clear();
                // Destroy all lines
                foreach (GameObject line in lines)
                {
                    // Destroy line
                    Destroy(line);
                }

                // Clear lines
                lines.Clear();
                // Reset values
                lastNumber             = 0;
                combinedSum            = 0;
                connectedElementNumber = 0;
                currentPower           = 0;
                lastTouchedElement     = null;
                currentLine            = null;
                currentLineRenderer    = null;
                connectedElement       = null;
                return;
            }
            // Loop through the combined elements
            foreach (GameObject currentElement in combinedElements)
            {
                // If its the last one then update its number
                if (currentElement == lastTouchedElement)
                {
                    // Update its number
                    currentElement.GetComponent <Element>().UpdateNumber(currentPower);
                }
                else
                {
                    // Translate it to the last touched element's position
                    currentElement.GetComponent <Element>().TranslateToObjectAndDestroy(lastTouchedElement);
                    // Remove it from the elements array
                    elements.Remove(currentElement);
                }
            }
            // Get combined elements number
            int combinedElementsCount = combinedElements.Count;
            // Clear combined elements
            combinedElements.Clear();
            // Destroy all lines
            foreach (GameObject line in lines)
            {
                // Destroy line
                Destroy(line);
            }

            // Clear lines
            lines.Clear();
            // Update score
            score += combinedSum;
            // Update the score UI
            UpdateScoreUI();
            // Increase highest power if necessary
            if (currentPower > highestPower + 1)
            {
                highestPower = currentPower - 1;
            }

            // Calculate the number
            int numberScored = (int)Mathf.Pow(2, currentPower);
            // Reset values
            lastNumber             = 0;
            combinedSum            = 0;
            connectedElementNumber = 0;
            currentPower           = 0;
            lastTouchedElement     = null;
            currentLine            = null;
            currentLineRenderer    = null;
            connectedElement       = null;
            // Play combination audio
            audioSource.PlayOneShot(combinationAudio);
            // Re arrange elements
            ReArrangeElements();
            // Look for hints
            LookForHints();
            // If the game is not over yet then save the elements data
            if (!gameOver)
            {
                GamePersistence.SetElementsData(elements);
                GamePersistence.gameData.lastScore          = score;
                GamePersistence.gameData.lastTimeSinceStart = timeSinceStart;
                GamePersistence.gameData.lastHighestPower   = highestPower;
                GamePersistence.Save();
                // Are we signed in to GPGS ?
                if (GPGSController.IsSignedIn())
                {
                    // Unlock achievements
                    GPGSController.UnlockAchievement(GPGSResources.achievement_combined);
                    if (combinedElementsCount >= 5)
                    {
                        GPGSController.UnlockAchievement(GPGSResources.achievement_tiled);
                    }
                    if (combinedElementsCount >= 10)
                    {
                        GPGSController.UnlockAchievement(GPGSResources.achievement_really_tiled);
                    }
                    if (numberScored >= 128)
                    {
                        GPGSController.UnlockAchievement(GPGSResources.achievement_hundreads);
                    }
                    if (numberScored >= 1000)
                    {
                        GPGSController.UnlockAchievement(GPGSResources.achievement_thousands);
                    }
                    if (numberScored >= 4000)
                    {
                        GPGSController.UnlockAchievement(GPGSResources.achievement_4k);
                    }
                    if (numberScored >= 16000)
                    {
                        GPGSController.UnlockAchievement(GPGSResources.achievement_tens_of_thousands);
                    }
                    if (numberScored >= 128000)
                    {
                        GPGSController.UnlockAchievement(GPGSResources.achievement_hundreads_of_thousands);
                    }
                    if (numberScored >= 1000000)
                    {
                        GPGSController.UnlockAchievement(GPGSResources.achievement_millionaire);
                    }
                }
                // Do we need to display an ad ?
                combinationsSinceLastAd++;
                if (combinationsSinceLastAd >= AD_COMBINATIONS_NUMBER)
                {
                    // Show interstitial
                    AdsController.ShowInterstitialAd();
                    hasJustShowenbAd = true;
                    // Reset combinations since last ad
                    combinationsSinceLastAd = 0;
                }
            }
        }
    }