示例#1
0
    private IEnumerator FindMatchesAndCollapse(RaycastHit2D rayCast)
    {
        GameObject go = rayCast.collider.gameObject;

        candyArray.SwapCandiesGO(hitGO, go);

        Vector3 tempVector = hitGO.transform.position;

        hitGO.transform.position = rayCast.transform.position;
        go.transform.position    = tempVector;
        yield return(new WaitForSeconds(1f));

        var hitGOMatchesInfo = candyArray.GetMatches(hitGO);
        var goMatchesInfo    = candyArray.GetMatches(go);

        IEnumerable <GameObject> totalMatches = hitGOMatchesInfo.GetMatchedCandy.Union(goMatchesInfo.GetMatchedCandy).Distinct();
        int tempCount = totalMatches.Count();

        if (tempCount >= GameVariables.MinimumMatches)
        {
            Vector3 _tempVector = hitGO.transform.position;
            hitGO.transform.position   = go.transform.position;
            go.transform.localPosition = _tempVector;
            //yield return new WaitForSeconds(1f);

            candyArray.UndoSwap();

            Candy hitGOCache = null;
            int   timeRun    = 1;

            while (totalMatches.Count() >= GameVariables.MinimumMatches)
            {
                foreach (var item in totalMatches)
                {
                    candyArray.Remove(item);
                    RemoveFromScene(item);
                }

                var column        = totalMatches.Select(_go => _go.GetComponent <Candy>().column).Distinct();
                var collapsedInfo = candyArray.Collapse(column);
                var newCandyInfo  = CreateNewCandyinSpecificColumn(column);
                int maxDistance   = Mathf.Max(collapsedInfo.MaxDistance, newCandyInfo.MaxDistance);
                MoveCandy(newCandyInfo.GetNewCandies(), maxDistance);
                MoveCandy(collapsedInfo.GetNewCandies(), maxDistance);

                //yield return new WaitForSeconds(1f * maxDistance);

                totalMatches = candyArray.GetMatches(collapsedInfo.GetNewCandies()).Union(candyArray.GetMatches(newCandyInfo.GetNewCandies())).Distinct();
                timeRun++;
            }

            gameState = GameState.None;
            StartCheckForPotentialMatches();
        }
    }
    private IEnumerator FindMatchesAndCollapse(RaycastHit2D hit2)
    {
        // assign 2nd game object to the one that was touched.
        var hitGo2 = hit2.collider.gameObject;

        // Swap the candy positions
        candies.Swap(hitGo, hitGo2);

        // Move passed item into position 2
        hitGo.transform.positionTo(GameVariables.AnimationDuration, hit2.transform.position);

        // Move item 2 into position
        hitGo2.transform.positionTo(GameVariables.AnimationDuration, hitGo.transform.position);

        // wait until its over.
        yield return(new WaitForSeconds(GameVariables.AnimationDuration));


        var hitGoMatchesInfo = candies.GetMatches(hitGo);

        var hitGo2MatchesInfo = candies.GetMatches(hitGo2);

        // join the first and second lists, de-duplicating, to result in the total matches
        var totalMatches = hitGoMatchesInfo.MatchedCandy.Union(hitGo2MatchesInfo.MatchedCandy).Distinct();


        // if there is not a valid match, such as only being 2 in a row, wrong colors, etc... we need to undo the animated swap.
        if (totalMatches.Count() < GameVariables.MinimumMatches)
        {
            // Move passed item back to position 1
            hitGo.transform.positionTo(GameVariables.AnimationDuration, hitGo2.transform.position);

            // Move item 2 back to its position
            hitGo2.transform.localPositionTo(GameVariables.AnimationDuration, hitGo.transform.position);

            yield return(new WaitForSeconds(GameVariables.AnimationDuration));

            candies.UndoSwap();
        }


        // figure out if we're going to grant a bonus
        bool addBonus = totalMatches.Count() >= GameVariables.MinimumMatchesForBonus &&
                        !BonusTypeChecker.ContainsDestroyWholeWorColumn(hitGoMatchesInfo.BonusesContained) && // if the items being destroyed doesn't already contain a bonus
                        !BonusTypeChecker.ContainsDestroyWholeWorColumn(hitGo2MatchesInfo.BonusesContained);  // if the items being destroyed doesn't already contain a bonus


        Candy hitGoCache = null;

        if (addBonus == true)
        {
            hitGoCache = new Candy();

            // assign one of the two game objects depending if its the same type
            var sameTypeGo = hitGoMatchesInfo.MatchedCandy.Count() > 0 ? hitGo : hitGo2;

            // get a reference to the Candy component on the SameTypeGo gameobject
            var candy = sameTypeGo.GetComponent <Candy>();

            // initialize the bonus candy
            hitGoCache.Initialize(candy.Type, candy.Row, candy.Column);
        }

        // track the while loop
        int timesRun = 1;

        while (totalMatches.Count() >= GameVariables.MinimumMatches)
        {
            // earn points for each 3-item match
            IncreaseScore(totalMatches.Count() - 2 * GameVariables.Match3Score);

            // bonus if multiples
            if (timesRun >= 2)
            {
                IncreaseScore(GameVariables.SubsequelMatchScore);
            }

            // trigger sound effect
            soundManager.PlaySound();

            // Now remove the individual items
            foreach (var item in totalMatches)
            {
                candies.Remove(item);
                RemoveFromScene(item);
            }

            // If there is a bonus candy to be generated, create it.
            if (addBonus)
            {
                CreateBonus(hitGoCache);
            }

            addBonus = false;


            var columns = totalMatches.Select(go => go.GetComponent <Candy>().Column).Distinct();

            var collapsedCandyInfo = candies.Collapse(columns);

            var newCandyInfo = CreateNewCandyInSpecificColumns(columns);

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

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

            // wait for time for each movement animation that has to occur
            yield return(new WaitForSeconds(GameVariables.MoveAnimationDuration * maxDistance));

            // Checking for new matches in the collapsed columns
            totalMatches = candies.GetRefillMatches(collapsedCandyInfo.AlteredCandy).Union(candies.GetRefillMatches(newCandyInfo.AlteredCandy)).Distinct();

            timesRun++;
        } // end while

        // reset game state
        state = GameState.None;

        // start checking for potential matches again.
        StartCheckForPotentialMatches();
    }
示例#3
0
    private IEnumerator FindMatchesAndCollapse(RaycastHit2D hit2)
    {
        //set second item
        var selectedObj2 = hit2.collider.gameObject;

        candies.Swap(selectedObj, selectedObj2);

        //move the swapped ones
        selectedObj.transform.positionTo(Constant.AnimationDuration, selectedObj2.transform.position);
        selectedObj2.transform.positionTo(Constant.AnimationDuration, selectedObj.transform.position);
        yield return(new WaitForSeconds(Constant.AnimationDuration));

        //get the matches via the helper methods
        var hitGomatchesInfo  = candies.GetMatches(selectedObj);
        var hitGo2matchesInfo = candies.GetMatches(selectedObj2);

        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() < Constant.MinimumMatches)
        {
            selectedObj.transform.positionTo(Constant.AnimationDuration, selectedObj2.transform.position);
            selectedObj2.transform.positionTo(Constant.AnimationDuration, selectedObj.transform.position);
            yield return(new WaitForSeconds(Constant.AnimationDuration));

            candies.UndoSwap();
        }

        int timesRun = 1;

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

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



            foreach (var item in totalMatches)
            {
                candies.Remove(item);
                RemoveFromScene(item);
            }
            //get the columns that we had a collapse
            var columns = totalMatches.Select(go => go.GetComponent <Candy>().Column).Distinct();

            //the order the 2 methods below get called is important!!!
            //collapse the ones gone
            var collapsedCandyInfo = candies.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(Constant.MoveAnimationMinDuration * maxDistance));

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



            timesRun++;
        }
        state = GameState.None;
        StartCheckForPotentialMatches();
    }
示例#4
0
    private IEnumerator FindMatchesAndCollapse(RaycastHit2D hit2)
    {
        var hitGo2 = hit2.collider.gameObject;

        candies.Swap(hitGo, hitGo2);

        hitGo.transform.positionTo(GameVariables.AnimationDuration, hit2.transform.position);
        hitGo2.transform.positionTo(GameVariables.AnimationDuration, hitGo.transform.position);
        yield return(new WaitForSeconds(GameVariables.AnimationDuration));

        var hitGoMatchesInfo  = candies.GetMatches(hitGo);
        var hitGo2MatchesInfo = candies.GetMatches(hitGo2);

        var totalMatches = hitGoMatchesInfo.MatchedCandy.Union(hitGo2MatchesInfo.MatchedCandy).Distinct();

        if (totalMatches.Count() < GameVariables.MinimumMatches)
        {
            hitGo.transform.positionTo(GameVariables.AnimationDuration, hitGo2.transform.position);
            hitGo2.transform.localPositionTo(GameVariables.AnimationDuration, hitGo.transform.position);
            yield return(new WaitForSeconds(GameVariables.AnimationDuration));

            candies.UndoSwap();
        }
        bool addBonus = totalMatches.Count() >= GameVariables.MinimumMatchesForBonus &&
                        !BonusTypeChecker.ContainsDestroyWholeRowColumn(hitGoMatchesInfo.BonusesContained) &&
                        !BonusTypeChecker.ContainsDestroyWholeRowColumn(hitGo2MatchesInfo.BonusesContained);

        Candy hitGoCache = null;

        if (addBonus)
        {
            hitGoCache = new Candy();

            var sameTypeGo = hitGoMatchesInfo.MatchedCandy.Count() > 0 ? hitGo : hitGo2;
            var candy      = sameTypeGo.GetComponent <Candy> ();

            hitGoCache.Initialize(candy.Type, candy.Row, candy.Column);
        }
        int timesRun = 1;

        while (totalMatches.Count() >= GameVariables.MinimumMatches)
        {
            IncreaseScore(Mathf.Abs(totalMatches.Count() - 2 * GameVariables.Match3Score));

            if (timesRun >= 2)
            {
                IncreaseScore(GameVariables.SubsequelMatchScore);
            }

            soundManager.PlaySound();

            foreach (var item in totalMatches)
            {
                candies.Remove(item);
                RemoveFromScene(item);
            }

            if (addBonus)
            {
                CreateBonus(hitGoCache);
            }
            addBonus = false;

            var columns           = totalMatches.Select(go => go.GetComponent <Candy> ().Column).Distinct();
            var collapseCandyInfo = candies.Collapse(columns);
            var newCandyInfo      = CreateNewCandyInSpecificColumns(columns);
            int maxDistance       = Mathf.Max(collapseCandyInfo.maxDistance, newCandyInfo.maxDistance);

            MoveAndAnimate(newCandyInfo.AlteredCandy, maxDistance);
            MoveAndAnimate(collapseCandyInfo.AlteredCandy, maxDistance);

            yield return(new WaitForSeconds(GameVariables.AnimationDuration * maxDistance));

            totalMatches = candies.GetMatches(collapseCandyInfo.AlteredCandy)
                           .Union(candies.GetMatches(newCandyInfo.AlteredCandy)).Distinct();

            timesRun++;
        }
        state = GameState.None;
        StartCheckForPotentialMatches();
    }