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); }
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); }
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); }
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(); }