コード例 #1
0
    void Update()
    {
        if (bMovable)
        {
            //move sideways
            if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                gameObject.transform.position += new Vector3Int(-1, 0, 0);
                if (IsValidGridPosition())
                {
                    UpdateWorldGrid();
                }
                else
                {
                    gameObject.transform.position += new Vector3Int(1, 0, 0);
                }
            }
            else if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                gameObject.transform.position += new Vector3Int(1, 0, 0);
                if (IsValidGridPosition())
                {
                    UpdateWorldGrid();
                }
                else
                {
                    gameObject.transform.position += new Vector3Int(-1, 0, 0);
                }
            }
            else if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                gameObject.transform.position += new Vector3Int(0, 0, 1);
                if (IsValidGridPosition())
                {
                    UpdateWorldGrid();
                }
                else
                {
                    gameObject.transform.position += new Vector3Int(0, 0, -1);
                }
            }
            else if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                gameObject.transform.position += new Vector3Int(0, 0, -1);
                if (IsValidGridPosition())
                {
                    UpdateWorldGrid();
                }
                else
                {
                    gameObject.transform.position += new Vector3Int(0, 0, 1);
                }
            }

            //rotation
            if (Input.GetKeyDown(KeyCode.D))
            {
                transform.Rotate(new Vector3Int(0, 0, 90), Space.World);
                if (IsValidGridPosition())
                {
                    UpdateWorldGrid();
                }
                else
                {
                    transform.Rotate(new Vector3Int(0, 0, -90), Space.World);
                }
            }
            else if (Input.GetKeyDown(KeyCode.A))
            {
                transform.Rotate(new Vector3Int(90, 0, 0), Space.World);
                if (IsValidGridPosition())
                {
                    UpdateWorldGrid();
                }
                else
                {
                    transform.Rotate(new Vector3Int(-90, 0, 0), Space.World);
                }
            }
            else if (Input.GetKeyDown(KeyCode.W))
            {
                transform.Rotate(new Vector3Int(0, 90, 0), Space.World);
                if (IsValidGridPosition())
                {
                    UpdateWorldGrid();
                }
                else
                {
                    transform.Rotate(new Vector3Int(0, 90, 0), Space.World);
                }
            }


            timer += 1 * Time.deltaTime;
            //Drop
            if (Input.GetKey(KeyCode.Space) && timer >= GameManager.gmInstance.quickDropTime)//quick drop
            {
                gameObject.transform.position += new Vector3Int(0, -1, 0);
                timer = 0;
                if (IsValidGridPosition())
                {
                    UpdateWorldGrid();
                }
                else
                {
                    gameObject.transform.position += new Vector3Int(0, 1, 0);

                    WorldGrid.DeleteWholeRows();

                    FindObjectOfType <BlockSpawner>().SpawnRandomBlock();

                    enabled = false;
                }
            }
            else if (timer >= GameManager.gmInstance.dropTime) //normal drop
            {
                gameObject.transform.position += new Vector3Int(0, -1, 0);
                timer = 0;
                if (IsValidGridPosition())
                {
                    UpdateWorldGrid();
                }
                else
                {
                    gameObject.transform.position += new Vector3Int(0, 1, 0);

                    WorldGrid.DeleteWholeRows();
                    FindObjectOfType <BlockSpawner>().SpawnRandomBlock();

                    enabled = false;
                }
            }
        }
    }