Example #1
0
    private IEnumerator FindMatchesAndCollapse(RaycastHit2D hit2)
    {
        Vector3 t = hitGo.transform.position;

        //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));

        Debug.Log("pos=" + t);

        int combo = getCombo(hitGo, hitGo2);

        if (combo < 0)
        {
            Debug.Log("moving from pos=" + hitGo.transform.position + "to pos=" + t);
            hitGo.transform.positionTo(Constants.AnimationDuration, t);
            // hitGo2.transform.positionTo(Constants.AnimationDuration, hitGo.transform.position);
            yield return(new WaitForSeconds(Constants.AnimationDuration));

            Debug.Log("combo not recognized, type1 is" + hitGo.GetComponent <Shape>().Type + " type2 is " + hitGo2.GetComponent <Shape>().Type);
        }
        else
        {
            GameObject newCandy = Instantiate(CandyPrefabs[levels[currentLevelIndex].indexOfFirstElement + combo], hitGo2.transform.position, Quaternion.identity)
                                  as GameObject;
            newCandy.GetComponent <Shape>().Row    = hitGo2.GetComponent <Shape>().Row;
            newCandy.GetComponent <Shape>().Column = hitGo2.GetComponent <Shape>().Column;
            newCandy.GetComponent <Shape>().Type   = combo;
            shapes.Remove(hitGo);
            RemoveFromScene(hitGo);

            ///If this is a last item, remove it from the board and score
            if (combo >= levels[currentLevelIndex].firstComplete)
            {
                shapes.Remove(hitGo2);
                //RemoveFromScene(newCandy);
                newCandy.GetComponent <Shape>().eject();
                score += 5;
            }
            else
            {
                shapes.setShape(newCandy);
            }
            //shapes.Remove(hitGo2);
            RemoveFromScene(hitGo2);
            //hitGo2.GetComponent<Sprite>().texture = CandyPrefabs[combo].GetComponent<Sprite>().texture;
            //shapes.changeTo(newCandy, combo);



            //var collapsedCandyInfo = shapes.Collapse(hitGo.GetComponent<Shape>().Column);
            var collapsedCandyInfo = shapes.Collapse();

            var newCandyInfo = CreateNewCandyInSpecificColumns();

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

            var matches = shapes.getMatches();
            foreach (GameObject go in matches)
            {
                shapes.Remove(go);
                RemoveFromScene(go);
            }
            //var collapsedCandyInfo = shapes.Collapse(hitGo.GetComponent<Shape>().Column);
            collapsedCandyInfo = shapes.Collapse();

            newCandyInfo = CreateNewCandyInSpecificColumns();

            maxDistance = Mathf.Max(collapsedCandyInfo.MaxDistance, newCandyInfo.MaxDistance);

            MoveAndAnimate(newCandyInfo.AlteredCandy, maxDistance);
            MoveAndAnimate(collapsedCandyInfo.AlteredCandy, maxDistance);
        }

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