Beispiel #1
0
    /// <summary>
    /// Variant that is called either when a bomb is detonated to remove the surrounding objects (bomb = true)
    /// or after a shuffle to check for any matches on the whole field (bomb = false or no argument)
    /// </summary>
    /// <param name="bomb"></param>
    /// <returns></returns>
    private IEnumerator FindMatchesAndCollapse(bool bomb = false)
    {
        //get all matches via the helper method
        IEnumerable <GameObject> totalMatches;

        if (bomb)
        {
            var startPos = Bomb.transform.position;
            ThrowBomb(hitGo.transform.position);
            yield return(new WaitForSeconds(Variables.MoveAnimationMinDuration));

            ResetBomb(startPos);
            totalMatches = shapes.GetBombMatches(hitGo);
        }
        else
        {
            totalMatches = shapes.GetMatches();
        }

        int timesRun = 1;

        while (totalMatches.Count() >= Variables.MinimumMatches)
        {
            //increase score
            IncreaseScore((totalMatches.Count() - 2) * Variables.Match3Score);

            if (timesRun >= 2)
            {
                IncreaseScore(Variables.SubsequentMatchScore);
            }

            //soundManager.PlayCrincle();

            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 collapsedCandyInfo = shapes.Collapse(columns);
            //create new ones
            var newCandyInfo = CreateNewCandyInSpecificColumns(columns);

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

            //wait a bit after explosions
            yield return(new WaitForSeconds(0.1f));

            MoveAndAnimate(collapsedCandyInfo.AlteredCandy, maxDistance);

            foreach (GameObject item in newCandyInfo.AlteredCandy)
            {
                MoveAndAnimateSingle(item, maxDistance);
                yield return(new WaitForSeconds(0.05f));
            }



            //will wait for both of the above animations
            yield return(new WaitForSeconds(Variables.MoveAnimationMinDuration /* * maxDistance*/));

            //search if there are matches with the new/collapsed items
            totalMatches = shapes.GetMatches(collapsedCandyInfo.AlteredCandy).
                           Union(shapes.GetMatches(newCandyInfo.AlteredCandy)).Distinct();



            timesRun++;
        }

        CheckWinCondition();
    }