Example #1
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))   // can be improved
        {
            transform.position += new Vector3(-1, 0, 0);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.position += new Vector3(1, 0, 0);
            }
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow))     // can be improved
        {
            transform.position += new Vector3(1, 0, 0);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.position += new Vector3(-1, 0, 0);
            }
        }
        else if (Input.GetKeyDown(KeyCode.UpArrow))     // can be improved
        {
            transform.Rotate(0, 0, -90);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.Rotate(0, 0, 90);
            }
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - lastFall >= 1)
        {
            Debug.Log(Time.time);
            Debug.Log(lastFall);
            // Modify position
            transform.position += new Vector3(0, -1, 0);

            // See if valid
            if (isValidGridPos())
            {
                // It's valid. Update grid.
                updateGrid();
            }
            else
            {
                // It's not valid. revert.
                transform.position += new Vector3(0, 1, 0);

                // Clear filled horizontal lines
                PlayGrid.deleteFullRows();

                // Spawn next Group
                FindObjectOfType <BlockSpawner>().spawnNext();

                // Disable script
                enabled = false;
            }
            lastFall = Time.time;
        }
    }