// Find the match that this hexagon piece is part of (implementation) private void GetMatchingPiecesAt(HexagonPiece piece, HexagonMatch match) { bool isPieceAddedToMatch = false; // Iterate over each possible tuple that is formed with this hexagon piece and see if that tuple is a match for (int i = 0; i < 6; i++) { HexagonTuple tuple = GetTupleAtCorner(piece.X, piece.Y, (HexagonPiece.Corner)i); if (!tuple.IsEmpty && tuple.IsMatching) { if (!isPieceAddedToMatch) { match.Add(piece); isPieceAddedToMatch = true; } if (matchesOnGridSet.Add(tuple.piece1)) { GetMatchingPiecesAt(tuple.piece1, match); } if (matchesOnGridSet.Add(tuple.piece2)) { GetMatchingPiecesAt(tuple.piece2, match); } if (matchesOnGridSet.Add(tuple.piece3)) { GetMatchingPiecesAt(tuple.piece3, match); } } } }
// Finds the match that have at least one hexagon piece from this tuple public HexagonMatch GetMatchingPiecesAt(HexagonTuple tuple) { matchesOnGridSet.Clear(); HexagonMatch result = GetMatchingPiecesAt(tuple.piece1); if (result == null) { result = GetMatchingPiecesAt(tuple.piece2); if (result == null) { result = GetMatchingPiecesAt(tuple.piece3); } } return(result); }
// If points resides inside the grid, locate the three adjacent hexagon pieces (tuple) that are closest to the point public bool TryGetTupleAt(Vector2 point, out HexagonTuple tuple) { if (point.x <= 0f || point.x >= gridSize.x || point.y <= 0f || point.y >= gridSize.y) { tuple = new HexagonTuple(); return(false); } // Calculate the row and column indices of the hexagon piece that the point resides inside int x, y; Utils.GetCoordinatesFrom(point, out x, out y); // Find the hexagon piece's corner that is closest to the point // This corner is guaranteed to have two adjacent hexagon pieces (i.e. GetPickableCorner) HexagonPiece.Corner corner = grid[x][y].GetPickableCorner(grid[x][y].GetClosestCorner(point - (Vector2)grid[x][y].transform.localPosition)); tuple = GetTupleAtCorner(x, y, corner); return(true); }
// Returns true if there are no possible moves that result in a match on the grid public bool CheckDeadlock() { matchesOnGridSet.Clear(); // We can skip odd columns, if there is a match, it will be found while iterating the even columns for (int x = 0; x < gridWidth; x += 2) { for (int y = 0; y < gridHeight; y++) { for (int i = 0; i < 6; i++) { // For each possible tuple on the grid HexagonTuple tuple = GetTupleAtCorner(x, y, (HexagonPiece.Corner)i); if (!tuple.IsEmpty) { // Check if rotating the tuple once or twice results in a match for (int j = 0; j < 2; j++) { tuple.RotateClockwise(); HexagonMatch match = GetMatchingPiecesAt(tuple); if (match != null) { PoolManager.Instance.Push(match); tuple.RotateClockwise(2 - j); return(false); } } // There is no match after 2 rotations, rotate the tuple one last time to restore its original state tuple.RotateClockwise(); } } } } return(true); }
public void Add(HexagonTuple tuple) { matchingPieces.Add(tuple.piece1); matchingPieces.Add(tuple.piece2); matchingPieces.Add(tuple.piece3); }