Beispiel #1
0
    private void Controll()
    {
        if (Input.GetKey(KeyCode.UpArrow) && (Time.time > timeTo_NextKeyRotate))        //change object direction
        {
            current_block.RotateRight();
            timeTo_NextKeyRotate = Time.time + keyRepeatRate_Rotate;
            sh.Playtransform();

            //make sure block dont over lap
            if (!layout.isValidPosition(current_block))
            {
                current_block.RotateLeft();
            }
        }

        if (Input.GetKey(KeyCode.LeftControl) && (Time.time > timeTo_NextKeyRotate))    //change object direction
        {
            current_block.RotateLeft();
            timeTo_NextKeyRotate = Time.time + keyRepeatRate_Rotate;

            //make sure block dont over lap
            if (!layout.isValidPosition(current_block))
            {
                current_block.RotateRight();
            }
        }

        if (Time.time - previous_time > (Input.GetKey(KeyCode.DownArrow) ? drop_interval / 10 : drop_interval))         //change object direction
        {
            current_block.MoveDown();
            previous_time = Time.time;

            //make sure block dont over lap
            //make sure block dont over lap
            if (!layout.isValidPosition(current_block))
            {
                if (layout.IsOverLimit(current_block))
                {
                    GameOver();
                }
                else
                {
                    //call method to handle block landing
                    LandBlock();
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            drop_timer         = Time.time + dropInterval_new;
            timeToNextKey_Down = Time.time + keyRepeatRate_Down;

            while (layout.isValidPosition(current_block) && !hard_drop)
            {
                current_block.MoveDown();
            }


            //make sure block dont over lap
            if (!layout.isValidPosition(current_block))
            {
                if (layout.IsOverLimit(current_block))
                {
                    GameOver();
                }
                else
                {
                    //call method to handle block landing
                    LandBlock();
                }
            }
        }
        else if (Input.GetKeyUp(KeyCode.Space))
        {
            hard_drop = false;
        }

        if ((Input.GetKey(KeyCode.LeftArrow) && (Time.time > timeTo_NextKey_LeftRight)) || Input.GetKeyDown(KeyCode.LeftArrow))
        {
            current_block.MoveLeft();
            timeTo_NextKey_LeftRight = Time.time + keyRepeatRate_LeftRight;
            sh.PlayMove();
            //make sure block dont over lap
            if (!layout.isValidPosition(current_block))
            {
                current_block.MoveRight();
            }
        }
        if ((Input.GetKey(KeyCode.RightArrow) && (Time.time > timeTo_NextKey_LeftRight)) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            current_block.MoveRight();
            timeTo_NextKey_LeftRight = Time.time + keyRepeatRate_LeftRight;
            sh.PlayMove();
            //make sure block dont over lap
            if (!layout.isValidPosition(current_block))
            {
                current_block.MoveLeft();
            }
        }

        //press p to pause game
        if (Input.GetKeyDown(KeyCode.P) || Input.GetKeyDown(KeyCode.Escape))
        {
            pause_game.PauseMenu();
        }

        /*
         * if it past a certain time, start making the block fall
         * allows player time to react
         */

        if (Time.time > drop_timer)
        {
            drop_timer = Time.time + drop_interval;
            //if there is a block, set it to fall down on default
            if (current_block)
            {
                current_block.MoveDown();

                /*
                 *  once block falls to bottom,
                 *  we stop its MoveDown method and push it back up one
                 *  we have to push back up because our script only detects
                 *  that its out of boundary when it actually goes out of boundary
                 *  afterwards set current_block to a new block, to spawn new block
                 */
                if (!layout.isValidPosition(current_block))
                {
                    //block lands
                    current_block.MoveUp();
                    layout.StoreBlock(current_block);   //store the block
                    if (spawner)
                    {
                        current_block = spawner.SpawnBlock();
                    }
                }
            }
        }
    }