/// <summary> /// Flag the given square. This doesn't reveal the square, just adds a mark saying there may be a bomb there /// </summary> /// <param name="x">The x coordinate of the square</param> /// <param name="y">The y coordinate of the squar</param> public void FlagSquare(int x, int y) { if (!IsOnMap(x, y)) { return; } RevealedState state = m_revealedState[x, y]; // Can't flag revealed squares if (state == RevealedState.Flag || state == RevealedState.Unexplored) { if (state == RevealedState.Flag) { state = RevealedState.Unexplored; m_bombsRemaining++; } else { state = RevealedState.Flag; m_bombsRemaining--; } // Update the tile m_revealedState[x, y] = state; m_tileMap.RefreshTile(new Vector3Int(x, y, 0)); } }
/// <summary> /// Reveals the current square. If the square does not touch any bombs, does a flood fil of the touching /// non bomb-touching squares /// </summary> /// <param name="x">The x coordinate of the square</param> /// <param name="y">The y coordinate of the square</param> public void RevealSquare(int x, int y) { if (m_gameOver || !IsOnMap(x, y)) { return; } Queue <Vector3Int> squaresToReveal = new Queue <Vector3Int>(); squaresToReveal.Enqueue(new Vector3Int(x, y, 0)); while (squaresToReveal.Count > 0) { Vector3Int square = squaresToReveal.Dequeue(); RevealedState state = m_revealedState[square.x, square.y]; if (state == RevealedState.Unexplored) { // Reveal the square and update the tile m_revealedState[square.x, square.y] = RevealedState.Guessed; m_tileMap.SetTile(new Vector3Int(square.x, square.y, 0), RevealedSquare); if (m_grid[square.x, square.y] == 1) { // Hit a bomb, end the game StopGame(false); break; } // If the square isn't touching bombs, add all the unexplored neighbors to the queue of squares we're revealing if (GetBombsTouchingSquare(square.x, square.y) == 0) { foreach (Vector3Int neighbor in GetNeighbors(square)) { if (IsOnMap(neighbor.x, neighbor.y) && m_revealedState[neighbor.x, neighbor.y] == RevealedState.Unexplored) { squaresToReveal.Enqueue(neighbor); } } } } } // Check if we've won the game if (CheckVictory()) { StopGame(true); } }
public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData) { // Query if we are flagged or not MapInformation mapInfo = tilemap.GetComponent <Transform>().gameObject.GetComponentInParent <MapInformation>(); RevealedState state = mapInfo.GetRevealedStateForSquare(position.x, position.y); if (state == RevealedState.Unexplored) { tileData.sprite = Unexplored; } else if (state == RevealedState.Flag) { tileData.sprite = Flag; } }