Пример #1
0
        private GemMatch MatchInGem(Gem gem, Vector2 gemPosition, bool canCheckSelf = true)
        {
            GemMatch horizontalMatch = HorizontalMatchOfGem(gem, gemPosition, canCheckSelf);

            horizontalMatch.AddGem(gem);

            GemMatch verticalMatch = VerticalMatchOfGem(gem, gemPosition, canCheckSelf);

            verticalMatch.AddGem(gem);

            GemMatch definitiveMatch = new GemMatch();

            if (horizontalMatch.IsMatch())
            {
                foreach (var _gem in horizontalMatch.gems)
                {
                    definitiveMatch.AddGem(_gem);
                }
            }

            if (verticalMatch.IsMatch())
            {
                foreach (var _gem in verticalMatch.gems)
                {
                    definitiveMatch.AddGem(_gem);
                }
            }

            return(definitiveMatch);
        }
Пример #2
0
        private GemMatch VerticalMatchOfGem(Gem gem, Vector2 gemPosition, bool canCheckSelf = true)
        {
            GemMatch match = new GemMatch();

            Gem nextGem;

            // Up
            var nextGemPosition = gemPosition + Vector2.up;

            while (CanCheckPosition(nextGemPosition))
            {
                nextGem = GetGemInPosition(nextGemPosition);
                bool isCompatible = nextGem.IsCompatibleWith(gem);
                if (!canCheckSelf && nextGem == gem)
                {
                    isCompatible = false;
                }

                if (isCompatible)
                {
                    match.AddGem(nextGem);
                    nextGemPosition += Vector2.up;
                }

                else
                {
                    break;
                }
            }

            // Down
            nextGemPosition = gemPosition + Vector2.down;
            while (CanCheckPosition(nextGemPosition))
            {
                nextGem = GetGemInPosition(nextGemPosition);
                bool isCompatible = nextGem.IsCompatibleWith(gem);
                if (!canCheckSelf && nextGem == gem)
                {
                    isCompatible = false;
                }

                if (isCompatible)
                {
                    match.AddGem(nextGem);
                    nextGemPosition += Vector2.down;
                }

                else
                {
                    break;
                }
            }

            return(match);
        }
Пример #3
0
        private GemMatch HorizontalMatchOfGem(Gem gem, Vector2 gemPosition, bool canCheckSelf = true)
        {
            GemMatch match = new GemMatch();

            Gem nextGem = null;

            // Right
            var nextGemPosition = gemPosition + Vector2.right;

            while (CanCheckPosition(nextGemPosition))
            {
                nextGem = GetGemInPosition(nextGemPosition);
                bool isCompatible = nextGem.IsCompatibleWith(gem);
                if (!canCheckSelf && nextGem == gem)
                {
                    isCompatible = false;
                }

                if (isCompatible)
                {
                    match.AddGem(nextGem);
                    nextGemPosition += Vector2.right;
                }

                else
                {
                    break;
                }
            }

            // Left
            nextGemPosition = gemPosition + Vector2.left;
            while (CanCheckPosition(nextGemPosition))
            {
                nextGem = GetGemInPosition(nextGemPosition);
                bool isCompatible = nextGem.IsCompatibleWith(gem);
                if (!canCheckSelf && nextGem == gem)
                {
                    isCompatible = false;
                }

                if (isCompatible)
                {
                    match.AddGem(nextGem);
                    nextGemPosition += Vector2.left;
                }

                else
                {
                    break;
                }
            }

            return(match);
        }
Пример #4
0
        private IEnumerator MatchGemRoutine(GemMatch match)
        {
            // Pre Match
            foreach (var gem in match.gems)
            {
                gem.OnMatchStart();
            }
            yield return(new WaitForSeconds(_gameManager.gemMatchAnimTime));

            // On Match
            DoMatch(match);
        }
Пример #5
0
        private IEnumerator OnLegalGemSwitchRoutine(GemMatch firstGemMatch, GemMatch secondGemMatch)
        {
            GemMatch allGemsMatch = new GemMatch();

            foreach (var gem in firstGemMatch.gems)
            {
                allGemsMatch.AddGem(gem);
            }
            foreach (var gem in secondGemMatch.gems)
            {
                allGemsMatch.AddGem(gem);
            }

            var mainRoutine = _gameManager.StartCoroutine(MatchGemRoutine(allGemsMatch));

            yield return(mainRoutine);

            yield return(new WaitForSeconds(_gameManager.boardDelayAfterMatch));

            // Do all other matches that can have in board.
            var boardMatches = GetAllMatchsInBoard();

            while (boardMatches.Length > 0)
            {
                // Do first other match im board.
                var firstOtherMatch   = boardMatches[0];
                var otherFirstRoutine = _gameManager.StartCoroutine(MatchGemRoutine(firstOtherMatch));

                // Start all other matches at the same time if have.
                if (boardMatches.Length > 1)
                {
                    for (int i = 1; i < boardMatches.Length; i++)
                    {
                        var match = boardMatches[i];
                        _gameManager.StartCoroutine(MatchGemRoutine(match));
                    }
                }

                // Wait for any match to stop, (all matches have the same time)
                yield return(otherFirstRoutine);

                yield return(new WaitForSeconds(_gameManager.boardDelayAfterMatch));

                boardMatches = GetAllMatchsInBoard();
            }

            ChangeBoardState(BoardState.Playing);
            TryGameOver();
        }
Пример #6
0
        private void DoMatch(GemMatch match)
        {
            bool isMatchLegal = match.IsMatch();

            if (!isMatchLegal)
            {
                return;
            }

            foreach (var hGem in match.gems)
            {
                hGem.OnMatchEnd();
                hGem.TransformIntoNewGem(GetRandomGemData());
                FallAllGemsFromPosition(hGem.boardPosition + Vector2.up);
                SendGemToTop(hGem);
            }
        }