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(ConstantsVariable_4.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(ConstantsVariable_4.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(ConstantsVariable_4.AnimationDuration)); //get the matches via the helper methods var hitGoMatchesSet = shapes.GetMatches(hitGo); var hitGo2MatchesSet = shapes.GetMatches(hitGo2); var totalMatches = hitGoMatchesSet.MatchedCandy .Union(hitGo2MatchesSet.MatchedCandy).Distinct(); //if user's swap didn't create at least a 3-match, undo their swap if (totalMatches.Count() < ConstantsVariable_4.MinimumMatches) { Audio_Source.PlayOneShot(Sounds[0]); hitGo.transform.positionTo(ConstantsVariable_4.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(ConstantsVariable_4.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(ConstantsVariable_4.AnimationDuration)); shapes.UndoSwap(); } else { Audio_Source.PlayOneShot(Sounds[1]); } if (totalMatches.Count() > ConstantsVariable_4.MinimumMatches) { Audio_Source.PlayOneShot(Sounds[4]); } //if more than 3 matches and no Bonus is contained in the line, we will award a new Bonus bool addBonus = totalMatches.Count() == ConstantsVariable_4.MinimumMatchesForBonus && !BonusTypeUtilities_4.ContainsDestroyWholeRowColumn(hitGoMatchesSet.BonusesContained) && !BonusTypeUtilities_4.ContainsDestroyWholeRowColumn(hitGo2MatchesSet.BonusesContained); bool addbigbonus = totalMatches.Count() >= ConstantsVariable_4.MinimumMatchesForBigBonus && !BonusTypeUtilities_4.ContainsDestroyWholeRowColumn(hitGoMatchesSet.BonusesContained) && !BonusTypeUtilities_4.ContainsDestroyWholeRowColumn(hitGo2MatchesSet.BonusesContained) && !BonusTypeUtilities_4.ContainsDestroyBothWholeRowColumn(hitGoMatchesSet.BonusesContained) && !BonusTypeUtilities_4.ContainsDestroyBothWholeRowColumn(hitGo2MatchesSet.BonusesContained); Shape_4 hitGoCache = null; if (addBonus) { //get the game object that was of the same type var sameTypeGo = hitGoMatchesSet.MatchedCandy.Count() > 0 ? hitGo : hitGo2; hitGoCache = sameTypeGo.GetComponent <Shape_4>(); } if (addbigbonus) { var allTypeGo = hitGoMatchesSet.MatchedCandy.Count() > 0?hitGo : hitGo2; hitGoCache = allTypeGo.GetComponent <Shape_4>(); } int timesRun = 1; while (totalMatches.Count() >= ConstantsVariable_4.MinimumMatches) { //increase score IncreaseScore((totalMatches.Count() - 2) * ConstantsVariable_4.Match3Score); if (timesRun >= 2) { IncreaseScore(ConstantsVariable_4.SubsequentMatchScore); } foreach (var item in totalMatches) { shapes.Remove(item); RemoveFromScene(item); } //check and instantiate Bonus if needed if (addBonus) { CreateBonus(hitGoCache); } if (addbigbonus) { Debug.Log("Bigbonus"); CreateBigBonus(hitGoCache); } addbigbonus = false; addBonus = false; //get the columns that we had a collapse var columns = totalMatches.Select(go => go.GetComponent <Shape_4>().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(ConstantsVariable_4.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_4.None; StartCheckForPotentialMatches(); }
// Update is called once per frame void Update() { if (Gameover == false) { if (state == GameState_4.None) { //user has clicked or touched if (Input.GetMouseButton(0)) { //get the hit position var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit.collider != null) //we have a hit!!! { hitGo = hit.collider.gameObject; state = GameState_4.SelectionStarted; } } } else if (state == GameState_4.SelectionStarted) { //user dragged if (Input.GetMouseButton(0)) { var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); //we have a hit if (hit.collider != null && hitGo != hit.collider.gameObject) { //user did a hit, no need to show him hints StopCheckForPotentialMatches(); //if the two shapes are diagonally aligned (different row and column), just return if (!GameUtilities_4.AreVerticalOrHorizontalNeighbors(hitGo.GetComponent <Shape_4>(), hit.collider.gameObject.GetComponent <Shape_4>())) { state = GameState_4.None; } else { state = GameState_4.Animating; FixSortingLayer(hitGo, hit.collider.gameObject); StartCoroutine(FindMatchesAndCollapse(hit)); } } } } //Touch touch = Input.GetTouch(0); //if (state == GameState.None) //{ // //user has clicked or touched // if (Input.touchCount > 0) // { // //get the hit position // var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Vector2.zero); // if (hit.collider != null) //we have a hit!!! // { // hitGo = hit.collider.gameObject; // state = GameState.SelectionStarted; // } // } //} //else if (state == GameState.SelectionStarted) //{ // //user dragged // if (Input.touchCount > 0) // { // var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Vector2.zero); // //we have a hit // if (hit.collider != null && hitGo != hit.collider.gameObject) // { // //user did a hit, no need to show him hints // StopCheckForPotentialMatches(); // //if the two shapes are diagonally aligned (different row and column), just return // if (!GameUtilities.AreVerticalOrHorizontalNeighbors(hitGo.GetComponent<Shape>(), // hit.collider.gameObject.GetComponent<Shape>())) // { // state = GameState.None; // } // else // { // state = GameState.Animating; // FixSortingLayer(hitGo, hit.collider.gameObject); // StartCoroutine(FindMatchesAndCollapse(hit)); // } // } // } //} } }