Esempio n. 1
0
        private IEnumerator FindMatchesAndCollapse(RaycastHit2D hit2)
        {
            //get the second item that was part of the swipe
            Item hitItem2 = hit2.collider.gameObject.GetComponent <Item>();

            board.Swap(hitItem.Cell, hitItem2.Cell);

            //animate swapping
            hitItem.transform.TweenPosition(swapDuration, hitItem2.transform.localPosition);
            hitItem2.transform.TweenPosition(swapDuration, hitItem.transform.localPosition);
            yield return(new WaitForSeconds(swapDuration));

            //get the matches
            var hitItemMatch  = board.GetMatch(hitItem.Cell);
            var hitItem2Match = board.GetMatch(hitItem2.Cell);

            //gather matches in one list
            var totalMatches = hitItemMatch.Cells.Union(hitItem2Match.Cells).Distinct();

            //if user's swap didn't create at least a min matches, undo their swap
            if (totalMatches.Count() < board.minMatches)
            {
                hitItem.transform.TweenPosition(swapDuration, hitItem2.transform.localPosition);
                hitItem2.transform.TweenPosition(swapDuration, hitItem.transform.localPosition);
                yield return(new WaitForSeconds(swapDuration));

                board.UndoLastSwap();
            }

            while (totalMatches.Count() >= board.minMatches)
            {
                AddScore((totalMatches.Count() - board.minMatches + 1) * matchScore);

                audio.Play();

                foreach (var cell in totalMatches)
                {
                    DestroyItem(cell.Item);
                }

                var columns = totalMatches.Select(cell => cell.Column).Distinct();

                //the order the 2 methods below get called is important!!!
                //collapse the ones gone
                var collapse = board.CollapseColumns(columns);
                //create new ones
                var newItems = GenerateNewItems(columns);

                int maxDistance = Mathf.Max(collapse.MaxDistance, newItems.MaxDistance);

                MoveAndAnimate(newItems.Cells, maxDistance);
                MoveAndAnimate(collapse.Cells, maxDistance);

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

                totalMatches = board.GetMatches(collapse.Cells).Union(board.GetMatches(newItems.Cells)).Distinct();
            }

            state = State.Default;
        }