예제 #1
0
    // Update is called once per frame
    void Update()
    {
        if (moving && !isGhost)
        {
            if (Time.time - fall >= 1 / speed)
            {
                Vector3 oldPos = transform.position;

                transform.position += new Vector3(0, -1, 0);

                if (!Playfield.CheckPos(transform))
                {
                    transform.position = oldPos;
                    Playfield.AddToPlayspace(transform);
                    moving = false;

                    Destroy(ghost.transform.gameObject);
                    return;
                }

                fall = Time.time;
            }

            if (Input.GetKeyDown("left"))
            {
                Vector3 oldPos = transform.position;

                transform.position += new Vector3(-1, 0, 0);

                if (!Playfield.CheckPos(transform))
                {
                    transform.position = oldPos;
                }

                UpdateGhost();
            }
            else if (Input.GetKeyDown("right"))
            {
                Vector3 oldPos = transform.position;

                transform.position += new Vector3(1, 0, 0);

                if (!Playfield.CheckPos(transform))
                {
                    transform.position = oldPos;
                }

                UpdateGhost();
            }
            else if (Input.GetKeyDown("space"))
            {
                Vector3 oldPos = transform.position;

                while (Playfield.CheckPos(transform))
                {
                    oldPos              = transform.position;
                    transform.position += new Vector3(0, -1, 0);
                }

                transform.position = oldPos;
                Playfield.AddToPlayspace(transform);
                moving = false;

                Destroy(ghost.transform.gameObject);
                return;
            }
            else if (canRotate && Input.GetKeyDown("up"))
            {
                transform.Rotate(0, 0, 90);

                if (!Playfield.CheckPos(transform))
                {
                    transform.Rotate(0, 0, -90);
                }
                else
                {
                    foreach (Transform child in transform)
                    {
                        child.Rotate(0, 0, -90);
                    }

                    UpdateGhost();
                }
            }
            else if (Input.GetKeyDown("down"))
            {
                speed *= 20;
            }

            if (Input.GetKeyUp("down"))
            {
                if (speed > 1)
                {
                    speed /= 20;
                }
            }
        }
    }