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); }
private GemBehaviour PlaceGem(GemBehaviour gem, GridIndex index) { gem.gameObject .ResetTransformation() .SetPosition(index.GridX, index.GridY); SetGemPosition(gem, index); return(gem); }
private GemBehaviour FillRandomGemAt(GridIndex index, int yOffset = 0) { return (PlaceGem( gem: board.InsantiateGem() .GetComponent <GemBehaviour>() .Init(board), index: index) .ApplyOffsetY(yOffset, gemFallSpeed)); }
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); }
private void ClearGemAt(GridIndex index) { var gem = GetGem(index); if (gem != null) { allGems[index.GridX, index.GridY] = null; gem.OnMoveComplete -= HandleGemMoveComplete; BoardBehaviour.DestroyGem(gem); } }
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>()); }
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); }
private bool HasMatchOnFill(GridIndex index) { return(matchingHelper.FindMatches(index, Vector2.left).Count() >= 3 || matchingHelper.FindMatches(index, Vector2.down).Count() >= 3); }
private void SetGemPosition(GemBehaviour gem, GridIndex index) { allGems[index.GridX, index.GridY] = gem; gem.SetCord(index); }
public GemBehaviour GetGem(GridIndex index) { return(BoardUtils.IsInBounds(index, columns, rows) ? allGems[index.GridX, index.GridY] : null); }
private void HandleGemMoveComplete(GemBehaviour gem, GridIndex newIndex) { PlaceGem(gem, newIndex); }