Exemplo n.º 1
0
    //Fuction to calculate the down movement of the block
    private void CalculateDown()
    {
        spawn.currentActicObject.transform.position += new Vector3(0, -1, 0);

        //Checks for a valid grid position
        if (isValidGridPos())
        {
            updateGrid();
        }
        else
        {
            //Set the variabels
            allObjects.Add(spawn.currentActicObject);
            lastObject = spawn.currentActicObject;
            spawn.currentActicObject.transform.position += new Vector3(0, 1, 0);

            int currentScore;
            int deleteRows = Grid.deleteAllFullRows();

            //If rows are deleted play a sound and delete also all emty blocks
            if (deleteRows > 0)
            {
                deleteBlocks();

                audioSource.clip = clearLine;
                audioSource.Play();
            }

            //Calculate the score
            switch (deleteRows)
            {
            case 1:
                currentScore = 40 * (Score.level + 1);
                break;

            case 2:
                currentScore = 100 * (Score.level + 1);
                break;

            case 3:
                currentScore = 300 * (Score.level + 1);
                break;

            case 4:
                currentScore = 1200 * (Score.level + 1);
                break;

            default:
                currentScore = 0;
                break;
            }

            //Add to the score the number of down pressed grid spaces
            if (softDropCount > 0)
            {
                currentScore += softDropCount - 1;
            }
            softDropCount = 0;

            //Display the score
            dispalyScore.AddScore(currentScore);

            //Spawn next block
            spawn.spawnNext();

            dropCounter++;

            //Update the list with the next objects
            objectList.UpdateQueue();

            //Ends the Slow Motion after 5 blocks
            if (slowCounter > 0 && dropCounter > slowCounter + 5)
            {
                print(slowCounter.ToString());
                slowCounter = 0;
                downTime    = oldDownTime;
                oldDownTime = 0f;
                diffTime    = 0f;

                audioSource.clip = timeStart;
                audioSource.Play();
            }

            //Change the level and makes the game harder
            if (diffTime > 20f && downTime > 0.2f && slowCounter == 0)
            {
                print(diffTime.ToString());
                downTime -= 0.1f;
                diffTime  = 0f;
                dispalyScore.AddLevel(1);

                audioSource.clip = levelUp;
                audioSource.Play();
            }

            if (!isValidGridPos())
            {
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
                Debug.Log("GAME OVER");
            }

            foreach (Transform child in spawn.currentActicObject.transform)
            {
                Vector2 vector = Grid.roundVec2(child.position);
                Grid.grid[(int)vector.x, (int)vector.y] = child;
            }
        }
    }