Exemplo n.º 1
0
    void DetectMatch(Block block)
    {
        if (block.Type == 5)
        {
            return;
        }

        bool incrementChain = false;
        int  left           = block.Column;

        while (left > 0 && BlockManager.Blocks[left - 1, block.Row].State == BlockState.Idle && BlockManager.Blocks[left - 1, block.Row].Type == block.Type)
        {
            left--;
        }

        int right = block.Column + 1;

        while (right < BlockManager.Columns && BlockManager.Blocks[right, block.Row].State == BlockState.Idle && BlockManager.Blocks[right, block.Row].Type == block.Type)
        {
            right++;
        }

        int bottom = block.Row;

        while (bottom > 0 && BlockManager.Blocks[block.Column, bottom - 1].State == BlockState.Idle && BlockManager.Blocks[block.Column, bottom - 1].Type == block.Type)
        {
            bottom--;
        }

        int top = block.Row + 1;

        while (top < BlockManager.Rows - 1 && BlockManager.Blocks[block.Column, top].State == BlockState.Idle && BlockManager.Blocks[block.Column, top].Type == block.Type)
        {
            top++;
        }

        int  width             = right - left;
        int  height            = top - bottom;
        int  matchedBlockCount = 0;
        bool horizontalMatch   = false;
        bool verticalMatch     = false;

        if (width >= minimumMatchLength)
        {
            horizontalMatch    = true;
            matchedBlockCount += width;
        }

        if (height >= minimumMatchLength)
        {
            verticalMatch      = true;
            matchedBlockCount += height;
        }

        if (!horizontalMatch && !verticalMatch)
        {
            return;
        }

        if (horizontalMatch && verticalMatch)
        {
            matchedBlockCount--;
        }

        int delayCounter = matchedBlockCount;

        if (horizontalMatch)
        {
            for (int matchColumn = left; matchColumn < right; matchColumn++)
            {
                BlockManager.Blocks[matchColumn, block.Row].Matcher.Match(matchedBlockCount, delayCounter--);
                if (BlockManager.Blocks[matchColumn, block.Row].Chainer.ChainEligible)
                {
                    incrementChain = true;
                }
            }
        }

        if (verticalMatch)
        {
            for (int matchRow = top - 1; matchRow >= bottom; matchRow--)
            {
                BlockManager.Blocks[block.Column, matchRow].Matcher.Match(matchedBlockCount, delayCounter--);
                if (BlockManager.Blocks[block.Column, matchRow].Chainer.ChainEligible)
                {
                    incrementChain = true;
                }
            }
        }

        bool playSound = false;

        if (matchedBlockCount > 3)
        {
            Score.ScoreCombo(matchedBlockCount);
            PanelManager.Panels[block.Column, block.Row].Play(PanelType.Combo, matchedBlockCount);
            playSound = true;
        }

        if (incrementChain)
        {
            ChainDetector.IncrementChain();
            int row = matchedBlockCount > 3 ? block.Row + 1 : block.Row;
            if (row <= PanelManager.Rows - 1)
            {
                PanelManager.Panels[block.Column, row].Play(PanelType.Chain, ChainDetector.ChainLength);
            }
            playSound = true;
        }

        if (playSound)
        {
            AudioSource.clip = BonusClip;
            AudioSource.Play();
        }
    }
Exemplo n.º 2
0
    void DetectMatch(Block block)
    {
        if (block.State != BlockState.Idle)
        {
            return;
        }

        int leftColumn = block.Column;

        while (leftColumn > 0 && blockManager.Blocks[leftColumn - 1, block.Row].State == BlockState.Idle && blockManager.Blocks[leftColumn - 1, block.Row].Type == block.Type)
        {
            leftColumn--;
        }

        int rightColumn = block.Column + 1;

        while (rightColumn < boardColumns.Value && blockManager.Blocks[rightColumn, block.Row].State == BlockState.Idle && blockManager.Blocks[rightColumn, block.Row].Type == block.Type)
        {
            rightColumn++;
        }

        int bottomRow = block.Row;

        while (bottomRow > 0 && blockManager.Blocks[block.Column, bottomRow - 1].State == BlockState.Idle && blockManager.Blocks[block.Column, bottomRow - 1].Type == block.Type)
        {
            bottomRow--;
        }

        int topRow = block.Row + 1;

        while (topRow < boardColumns.Value && blockManager.Blocks[block.Column, topRow].State == BlockState.Idle && blockManager.Blocks[block.Column, topRow].Type == block.Type)
        {
            topRow++;
        }

        int  width             = rightColumn - leftColumn;
        int  height            = topRow - bottomRow;
        int  matchedBlockCount = 0;
        bool horizontalMatch   = false;
        bool verticalMatch     = false;

        if (width >= minimumMatchLength.Value)
        {
            horizontalMatch    = true;
            matchedBlockCount += width;
        }

        if (height >= minimumMatchLength.Value)
        {
            verticalMatch      = true;
            matchedBlockCount += height;
        }

        if (!horizontalMatch && !verticalMatch)
        {
            block.Chainer.SetChainEligibility(false);
            return;
        }

        // If there's a horizontal and vertical match, remove the common block
        if (horizontalMatch && verticalMatch)
        {
            matchedBlockCount--;
        }

        int  delayCounter   = matchedBlockCount;
        bool incrementChain = false;

        if (horizontalMatch)
        {
            for (int matchColumn = leftColumn; matchColumn < rightColumn; matchColumn++)
            {
                blockManager.Blocks[matchColumn, block.Row].Matcher.Match(matchedBlockCount, delayCounter--);
                if (blockManager.Blocks[matchColumn, block.Row].Chainer.ChainEligible)
                {
                    chainDetector.AddChainContributingBlock(blockManager.Blocks[matchColumn, block.Row]);
                    incrementChain = true;
                }
            }
        }

        if (verticalMatch)
        {
            for (int matchRow = topRow - 1; matchRow >= bottomRow; matchRow--)
            {
                blockManager.Blocks[block.Column, matchRow].Matcher.Match(matchedBlockCount, delayCounter--);
                if (blockManager.Blocks[block.Column, matchRow].Chainer.ChainEligible)
                {
                    chainDetector.AddChainContributingBlock(blockManager.Blocks[block.Column, matchRow]);
                    incrementChain = true;
                }
            }
        }

        block.Chainer.SetChainEligibility(false);

        bool playSound = false;

        audioCue.Pitch = 1f;

        if (matchedBlockCount > minimumMatchLength.Value)
        {
            scoreManager.ScoreCombo(matchedBlockCount);
            panelManager.Panels[block.Column, block.Row].Play(PanelType.Combo, matchedBlockCount);
            playSound = true;
        }

        if (incrementChain)
        {
            chainDetector.IncrementChain();
            int row = matchedBlockCount > minimumMatchLength.Value ? block.Row + 1 : block.Row;
            if (row <= boardRows.Value - 1)  // BUG: Chains that occur on the top row won't show a panel
            {
                panelManager.Panels[block.Column, row].Play(PanelType.Chain, chainDetector.ChainLength);
                audioCue.Pitch = Mathf.Min(0.75f + chainDetector.ChainLength * 0.25f, 2f);
            }
            playSound = true;
        }

        if (playSound)
        {
            audioCue.Play(audioSource);
        }
    }