private IEnumerator FindMatchesAndCollapse(GameObject hitGo2) { bool matchPerformed = false; shapes.Swap(hitGo, hitGo2); // move the swapped ones hitGo.transform.DOMove(hitGo2.transform.position, Constants.SwapAnimationDuration); hitGo2.transform.DOMove(hitGo.transform.position, Constants.SwapAnimationDuration); yield return(new WaitForSeconds(Constants.SwapAnimationDuration)); BonusType hitGoBonus = hitGo.GetComponent <Shape>().Bonus; BonusType hitGo2Bonus = hitGo2.GetComponent <Shape>().Bonus; // check if one of the two objects was combined with an ultimate if ((hitGo2Bonus == BonusType.Ultimate) && (hitGoBonus != BonusType.Ultimate) && (hitGoBonus != BonusType.None)) { DuplicateCandy(hitGo); } if ((hitGoBonus == BonusType.Ultimate) && (hitGo2Bonus != BonusType.Ultimate) && (hitGo2Bonus != BonusType.None)) { DuplicateCandy(hitGo2); } List <MatchesInfo> matchesInfos = new List <MatchesInfo>(); // get the matches via the helper methods matchesInfos.Add(shapes.GetMatches(hitGo, hitGo2)); matchesInfos.Add(shapes.GetMatches(hitGo2, hitGo)); var totalMatches = matchesInfos[0].MatchedCandy.Union(matchesInfos[1].MatchedCandy).Distinct(); // if user's swap didn't create at least a 3-match, undo their swap if (totalMatches.Count() < Constants.MinimumMatches) { hitGo.transform.DOMove(hitGo2.transform.position, Constants.SwapAnimationDuration); hitGo2.transform.DOMove(hitGo.transform.position, Constants.SwapAnimationDuration); yield return(new WaitForSeconds(Constants.SwapAnimationDuration)); shapes.UndoSwap(); } else { // user performed a match, no need to show the hints anymore matchPerformed = true; StopCheckForPotentialMatches(); } // decide if a bonus shall be added to one of the 2 user matches foreach (var item in matchesInfos) { if (item.Matches >= Constants.MinimumMatchesForBonus) { item.CreateBonus = true; } } int timesRun = 0; while (totalMatches.Count() >= Constants.MinimumMatches) { // boolean array to not create score for the same match more than onces bool[,] matchScoreCounted = new bool[Constants.Rows, Constants.Columns]; for (int i = 0; i < Constants.Rows; i++) { for (int j = 0; j < Constants.Columns; j++) { matchScoreCounted[i, j] = false; } } // increase score foreach (var item in matchesInfos) { if (item.DestroyedByBonus == true) { bool countScore = true; foreach (var go in item.MatchedCandy) { if (matchScoreCounted[go.GetComponent <Shape>().Row, go.GetComponent <Shape>().Column] == true) { countScore = false; } } if (countScore == true) { foreach (var go in item.MatchedCandy) { matchScoreCounted[go.GetComponent <Shape>().Row, go.GetComponent <Shape>().Column] = true; Vector3 position = Camera.main.WorldToScreenPoint(go.transform.position); IncreaseScoreAndCreatePopUp(Constants.BonusMatchScore, position); } } } else if (item.Matches == 3) { CheckForScoreAndIncrease(item, ref matchScoreCounted, Constants.Match3Score, timesRun); } else if (item.Matches == 4) { CheckForScoreAndIncrease(item, ref matchScoreCounted, Constants.Match4Score, timesRun); } else if (item.Matches >= 5) { CheckForScoreAndIncrease(item, ref matchScoreCounted, Constants.Match5Score, timesRun); } } soundManager.PlayCrinkle(); foreach (var item in totalMatches) { shapes.Remove(item); RemoveFromScene(item); } // check if there are any bonus objects to create foreach (var item in matchesInfos) { if (item.CreateBonus == true) { CreateBonus(item.OriginGameObject, item); item.CreateBonus = false; } } // get the columns that we had a collapse var columns = totalMatches.Select(go => go.GetComponent <Shape>().Column).Distinct(); // the order the 2 methods below get called is important! // collapse the ones gone var collapsedCandyInfo = shapes.Collapse(columns); // create new ones var newCandyInfo = CreateNewCandyInSpecificColumns(columns); // wait until explosion animation has finished yield return(new WaitForSeconds(Constants.ExplosionDuration)); yield return(new WaitForSeconds(Constants.DelayAfterExplosion)); MoveAndAnimate(newCandyInfo.AlteredCandy); MoveAndAnimate(collapsedCandyInfo.AlteredCandy); // will wait for both of the above animations yield return(new WaitForSeconds(Constants.MoveAnimationMinDuration)); // search if there are matches with the collapsed objects and then new objects matchesInfos = new List <MatchesInfo>(); matchesInfos.AddRange(shapes.GetMatchesInfos(collapsedCandyInfo.AlteredCandy)); matchesInfos.AddRange(shapes.GetMatchesInfos(newCandyInfo.AlteredCandy)); // boolean array to not create boni for the same match more than onces bool[,] bonusCreated = new bool[Constants.Rows, Constants.Columns]; for (int i = 0; i < Constants.Rows; i++) { for (int j = 0; j < Constants.Columns; j++) { bonusCreated[i, j] = false; } } // loop throw all found matches List <GameObject> matches = new List <GameObject>(); foreach (var item in matchesInfos) { // check if bonus should be created if (item.Matches >= Constants.MinimumMatchesForBonus) { bool bonus = true; foreach (var go in item.MatchedCandy) { if (bonusCreated[go.GetComponent <Shape>().Row, go.GetComponent <Shape>().Column] == true) { bonus = false; } } if (bonus == true) { item.CreateBonus = true; foreach (var go in item.MatchedCandy) { bonusCreated[go.GetComponent <Shape>().Row, go.GetComponent <Shape>().Column] = true; } } } // save each object to save it then to totalMatches matches.AddRange(item.MatchedCandy); } totalMatches = matches; timesRun++; } state = GameState.None; if (matchPerformed == true) { StartCheckForPotentialMatches(); } }
private IEnumerator FindMatchesAndCollapse(RaycastHit2D hit2) { //get the second item that was part of the swipe var hitGo2 = hit2.collider.gameObject; shapes.Swap(hitGo, hitGo2); //move the swapped ones hitGo.transform.positionTo(Constants.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(Constants.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(Constants.AnimationDuration)); //get the matches via the helper methods var hitGomatchesInfo = shapes.GetMatches(hitGo); var hitGo2matchesInfo = shapes.GetMatches(hitGo2); var totalMatches = hitGomatchesInfo.MatchedFood .Union(hitGo2matchesInfo.MatchedFood).Distinct(); //if user's swap didn't create at least a 3-match, undo their swap if (totalMatches.Count() < Constants.MinimumMatches) { hitGo.transform.positionTo(Constants.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(Constants.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(Constants.AnimationDuration)); shapes.UndoSwap(); } int timesRun = 1; while (totalMatches.Count() >= Constants.MinimumMatches) { //increase score IncreaseScore((totalMatches.Count() - 2) * Constants.Match3Score); if (timesRun >= 2) { IncreaseScore(Constants.SubsequentMatchScore); } foreach (var item in totalMatches) { shapes.Remove(item); RemoveFromScene(item); } //get the columns that we had a collapse var columns = totalMatches.Select(go => go.GetComponent <Shape>().Column).Distinct(); //the order the 2 methods below get called is important!!! //collapse the ones gone var collapsedFoodInfo = shapes.Collapse(columns); //create new ones var newFoodInfo = CreateNewFoodInSpecificColumns(columns); int maxDistance = Mathf.Max(collapsedFoodInfo.MaxDistance, newFoodInfo.MaxDistance); MoveAndAnimate(newFoodInfo.AlteredFood, maxDistance); MoveAndAnimate(collapsedFoodInfo.AlteredFood, maxDistance); //will wait for both of the above animations yield return(new WaitForSeconds(Constants.MoveAnimationMinDuration * maxDistance)); //search if there are matches with the new/collapsed items totalMatches = shapes.GetMatches(collapsedFoodInfo.AlteredFood). Union(shapes.GetMatches(newFoodInfo.AlteredFood)).Distinct(); timesRun++; } state = GameState.None; StartCheckForPotentialMatches(); }
private IEnumerator FindMatchesAndCollapse(RaycastHit2D hit2) { //get the second item that was part of the swipe var hitGo2 = hit2.collider.gameObject; shapes.Swap(hitGo, hitGo2); //move the swapped ones hitGo.transform.positionTo(Constants.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(Constants.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(Constants.AnimationDuration)); //get the matches via the helper methods var hitGomatchesInfo = shapes.GetMatches(hitGo); var hitGo2matchesInfo = shapes.GetMatches(hitGo2); var totalMatches = hitGomatchesInfo.MatchedCandy .Union(hitGo2matchesInfo.MatchedCandy).Distinct(); //if user's swap didn't create at least a 3-match, undo their swap if (totalMatches.Count() < Constants.MinimumMatches) { hitGo.transform.positionTo(Constants.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(Constants.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(Constants.AnimationDuration)); shapes.UndoSwap(); } //if more than 3 matches and no Bonus is contained in the line, we will award a new Bonus bool addBonus = totalMatches.Count() >= Constants.MinimumMatchesForBonus && !BonusTypeUtilities.ContainsDestroyWholeRowColumn(hitGomatchesInfo.BonusesContained) && !BonusTypeUtilities.ContainsDestroyWholeRowColumn(hitGo2matchesInfo.BonusesContained); Shape hitGoCache = null; if (addBonus) { //get the game object that was of the same type var sameTypeGo = hitGomatchesInfo.MatchedCandy.Count() > 0 ? hitGo : hitGo2; hitGoCache = sameTypeGo.GetComponent <Shape>(); } int timesRun = 1; while (totalMatches.Count() >= Constants.MinimumMatches) { //increase score IncreaseScore((totalMatches.Count() - 2) * Constants.Match3Score); if (timesRun >= 2) { IncreaseScore(Constants.SubsequentMatchScore); } soundManager.PlayCrincle(); foreach (var item in totalMatches) { shapes.Remove(item); RemoveFromScene(item); } //check and instantiate Bonus if needed if (addBonus) { CreateBonus(hitGoCache); } addBonus = false; //get the columns that we had a collapse var columns = totalMatches.Select(go => go.GetComponent <Shape>().Column).Distinct(); //the order the 2 methods below get called is important!!! //collapse the ones gone var collapsedCandyInfo = shapes.Collapse(columns); //create new ones var newCandyInfo = CreateNewCandyInSpecificColumns(columns); int maxDistance = Mathf.Max(collapsedCandyInfo.MaxDistance, newCandyInfo.MaxDistance); MoveAndAnimate(newCandyInfo.AlteredCandy, maxDistance); MoveAndAnimate(collapsedCandyInfo.AlteredCandy, maxDistance); //will wait for both of the above animations yield return(new WaitForSeconds(Constants.MoveAnimationMinDuration * maxDistance)); //search if there are matches with the new/collapsed items totalMatches = shapes.GetMatches(collapsedCandyInfo.AlteredCandy). Union(shapes.GetMatches(newCandyInfo.AlteredCandy)).Distinct(); timesRun++; } state = GameState.None; StartCheckForPotentialMatches(); }
private IEnumerator FindMatchesAndCollapse(GameObject hitGo2) { bool addBonus = false; bool isBall = false; bool isRef = false; bool isBlue = false; bool isGreen = false; bool isRed = false; bool isCoin = false; bool isGoalie = false; int numBalls = 0; int numRefs = 0; int numBlues = 0; int numGreens = 0; int numReds = 0; int numCoins = 0; int numGoalies = 0; shapes.Swap(hitGo, hitGo2); //move the swapped ones hitGo.transform.positionTo(Constants.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(Constants.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(Constants.AnimationDuration)); //get the matches via the helper methods var hitGomatchesInfo = shapes.GetMatches(hitGo); var hitGo2matchesInfo = shapes.GetMatches(hitGo2); var totalMatches = hitGomatchesInfo.MatchedBlock .Union(hitGo2matchesInfo.MatchedBlock).Distinct(); //if user's swap didn't create at least a 3-match, undo their swap if (totalMatches.Count() < Constants.MinimumMatches) { hitGo.transform.positionTo(Constants.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(Constants.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(Constants.AnimationDuration)); shapes.UndoSwap(); } //if more than 3 matches and no Bonus is contained in the line, we will award a new Bonus if ((hitGomatchesInfo.MatchedBlock.Count() >= Constants.MinimumMatchesForBonus || hitGo2matchesInfo.MatchedBlock.Count() >= Constants.MinimumMatchesForBonus) && !timedTurns) { addBonus = true; turnTime = 30f; StartCoroutine(DisplayText(bonusText)); } int timesRun = 1; //can be used for subsequent matching bonus point while (totalMatches.Count() >= Constants.MinimumMatches) { isBall = false; isRef = false; isBlue = false; isGreen = false; isRed = false; isCoin = false; isGoalie = false; numBalls = 0; numRefs = 0; numBlues = 0; numGreens = 0; numReds = 0; numCoins = 0; numGoalies = 0; soundManager.PlayCrincle(); foreach (var item in totalMatches) { if (item.GetComponent <Shape>().Type == "Ball") { isBall = true; numBalls++; } else if (item.GetComponent <Shape>().Type == "Ref") { isRef = true; numRefs++; } else if (item.GetComponent <Shape>().Type == "player_blue") { isBlue = true; numBlues++; } else if (item.GetComponent <Shape>().Type == "player_green") { isGreen = true; numGreens++; } else if (item.GetComponent <Shape>().Type == "player_red") { isRed = true; numReds++; } else if (item.GetComponent <Shape>().Type == "Coin") { isCoin = true; numCoins++; } else if (item.GetComponent <Shape>().Type == "Goalie") { isGoalie = true; numGoalies++; } shapes.Remove(item); RemoveFromScene(item); } if (isBall) { DealDamage(numBalls); } if (isRef) { AddAttribute(0, numRefs, false); } if (isBlue) { AddAttribute(1, numBlues, false); } if (isGreen) { AddAttribute(2, numGreens, false); } if (isRed) { AddAttribute(3, numReds, false); } if (isCoin) { AddCoins(numCoins); } if (isGoalie) { RegainHp(numGoalies); } //get the columns that we had a collapse var columns = totalMatches.Select(go => go.GetComponent <Shape>().Column).Distinct(); //collapse the ones gone var collapsedCandyInfo = shapes.Collapse(columns); //create new ones var newCandyInfo = CreateNewCandyInSpecificColumns(columns); int maxDistance = Mathf.Max(collapsedCandyInfo.MaxDistance, newCandyInfo.MaxDistance); MoveAndAnimate(newCandyInfo.AlteredCandy, maxDistance); MoveAndAnimate(collapsedCandyInfo.AlteredCandy, maxDistance); //will wait for both of the above animations yield return(new WaitForSeconds(Constants.MoveAnimationMinDuration * maxDistance)); //search if there are matches with the new/collapsed items totalMatches = shapes.GetMatches(collapsedCandyInfo.AlteredCandy). Union(shapes.GetMatches(newCandyInfo.AlteredCandy)).Distinct(); //if we have not hit bonus and we don't have timed turns, change turn if (!timedTurns && !addBonus && totalMatches.Count() < Constants.MinimumMatches) { ChangeTurn(); } timesRun++; } state = GameState.None; StartCheckForPotentialMatches(); }