Пример #1
0
    // Handles user input and acts upon the block.
    // Only one block per user will be active at a given time.
    private void CheckUserInput()
    {
        if (Input.GetKeyDown(up))
        {
            // try changing the position
            transform.position += new Vector3(0, 1, 0);

            if (isValidPos())
            {
                // valid position: commit changes
                updateBlock();
            }
            else
            {
                // invalid position: roll back
                transform.position -= new Vector3(0, 1, 0);
            }
        }
        else if (Input.GetKeyDown(down))
        {
            transform.position += new Vector3(0, -1, 0);

            if (isValidPos())
            {
                updateBlock();
            }
            else
            {
                transform.position -= new Vector3(0, -1, 0);
            }
        }
        else if (Input.GetKeyDown(fastFall) || (Time.time - timeLastFall > Game.gameSpeed))
        {
            // second if condition ^ handles automatic falling

            // try a new position
            transform.position += new Vector3(fallDirection, 0, 0);

            if (isValidPos())
            {
                // commit the new position
                updateBlock();
            }
            else // not a valid position: a new block will spawn and the old one will be stuck
            {
                // revert change to position
                transform.position -= new Vector3(fallDirection, 0, 0);

                Game.DeleteFullCols();

                // spawn next block
                BlockSpawner spawner = Game.GetSpawner(transform.position.x).GetComponent <BlockSpawner>();
                spawner.spawnNextBlock();

                // disable the script
                enabled = false;
            }
            timeLastFall = Time.time;
        }
        else if (Input.GetKeyDown(rotate) && canRotate)
        {
            if (restrictRotation)
            // I, S and Z Blocks
            {
                if (transform.rotation.eulerAngles.z < 90)
                {
                    transform.Rotate(0, 0, 90);
                    if (isValidPos())
                    {
                        updateBlock();
                    }
                    else
                    {
                        transform.Rotate(0, 0, -90);
                    }
                }
                else
                {
                    transform.Rotate(0, 0, -90);
                    if (isValidPos())
                    {
                        updateBlock();
                    }
                    else
                    {
                        transform.Rotate(0, 0, 90);
                    }
                }
            }
            else
            // L, J, T blocks
            {
                transform.Rotate(0, 0, 90);
                if (isValidPos())
                {
                    updateBlock();
                }
                else
                {
                    transform.Rotate(0, 0, -90);
                }
            }
        }
    }