Exemplo n.º 1
0
    public void DropCandy()
    {
        int candyDropped = (candy + 1) / 2;

        candy = candy - candyDropped;
        while (candyDropped > 0)
        {
            int amntDropped = Random.Range(1, candyDropped);
            candyDropped -= amntDropped;
            Vector2 throwDirection = new Vector2((Random.Range(0, 200) - 100), (Random.Range(0, 200) - 100)); // picks a location within 1x1 of the player
            Candy   c = Instantiate(PFLooseCandy, transform.position, transform.rotation);
            c.Initialize(amntDropped, throwDirection);
        }
    }
    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();
    }
Exemplo n.º 3
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();
    }