示例#1
0
        private GemBehaviour FillIndexWithGem(bool isRefill, GridIndex index)
        {
            GemBehaviour gem;

            var yOffest  = isRefill ? index.GridY + REFILL_Y_OFFSET : 0;
            var attempts = 0;

            do
            {
                if (attempts > 0)
                {
                    ClearGemAt(index);

                    if (attempts == MAX_FILL_ATTEMPTS)
                    {
                        throw new Exception("Max attempts to fill the board reached");
                    }
                }

                gem = FillRandomGemAt(index, yOffest);
                attempts++;
            } while (HasMatchOnFill(index));

            return(gem);
        }
 public static bool IsInBounds(GridIndex index, int columns, int rows)
 {
     return(index.GridX >= 0 &&
            index.GridX < columns &&
            index.GridY >= 0 &&
            index.GridY < rows);
 }
示例#3
0
        private GemBehaviour PlaceGem(GemBehaviour gem, GridIndex index)
        {
            gem.gameObject
            .ResetTransformation()
            .SetPosition(index.GridX, index.GridY);

            SetGemPosition(gem, index);
            return(gem);
        }
示例#4
0
 private GemBehaviour FillRandomGemAt(GridIndex index, int yOffset = 0)
 {
     return
         (PlaceGem(
              gem: board.InsantiateGem()
              .GetComponent <GemBehaviour>()
              .Init(board),
              index: index)
          .ApplyOffsetY(yOffset, gemFallSpeed));
 }
示例#5
0
        private GemBehaviour MakeGemFall(GemBehaviour gem, int xIndex, int initY, int newY)
        {
            var index = new GridIndex(xIndex, newY);

            gem.Move(index, gemFallSpeed * (initY - newY));

            SetGemPosition(gem, index);

            allGems[xIndex, initY] = null;
            return(gem);
        }
示例#6
0
        private void ClearGemAt(GridIndex index)
        {
            var gem = GetGem(index);

            if (gem != null)
            {
                allGems[index.GridX, index.GridY] = null;
                gem.OnMoveComplete -= HandleGemMoveComplete;
                BoardBehaviour.DestroyGem(gem);
            }
        }
示例#7
0
        public IEnumerable <GemBehaviour> FindMatchesAt(GridIndex index)
        {
            var hMatches = matchingHelper.FindHorizontalMatches(index);
            var vMatches = matchingHelper.FindVerticalMatches(index);

            hMatches = hMatches.Count >= 3 ? hMatches : new List <GemBehaviour>();
            vMatches = vMatches.Count >= 3 ? vMatches : new List <GemBehaviour>();

            return(hMatches.Any() || vMatches.Any()
                       ? hMatches.Union(vMatches)
                       : Enumerable.Empty <GemBehaviour>());
        }
示例#8
0
        public void FillBoard(bool isRefill = false)
        {
            for (var i = 0; i < columns; i++)
            {
                for (var j = 0; j < rows; j++)
                {
                    var index = new GridIndex(i, j);
                    if (GetGem(index) != null)
                    {
                        continue;
                    }

                    var gem = FillIndexWithGem(isRefill, index);
                    gem.OnMoveComplete += HandleGemMoveComplete;
                }
            }
        }
 public static bool IsDirecNeighbor(GridIndex indexA, GridIndex indexB)
 {
     return(Mathf.Abs(indexA.GridX - indexB.GridX) == 1 && indexA.GridY == indexB.GridY ||
            Mathf.Abs(indexA.GridY - indexB.GridY) == 1 && indexA.GridX == indexB.GridX);
 }
示例#10
0
 private bool HasMatchOnFill(GridIndex index)
 {
     return(matchingHelper.FindMatches(index, Vector2.left).Count() >= 3 ||
            matchingHelper.FindMatches(index, Vector2.down).Count() >= 3);
 }
示例#11
0
 private void SetGemPosition(GemBehaviour gem, GridIndex index)
 {
     allGems[index.GridX, index.GridY] = gem;
     gem.SetCord(index);
 }
示例#12
0
 public GemBehaviour GetGem(GridIndex index)
 {
     return(BoardUtils.IsInBounds(index, columns, rows)
                ? allGems[index.GridX, index.GridY]
                : null);
 }
示例#13
0
 private void HandleGemMoveComplete(GemBehaviour gem, GridIndex newIndex)
 {
     PlaceGem(gem, newIndex);
 }