IEnumerator instanciateCells() { float cellWidth = cellPrefab.transform.localScale.x; float cellHeight = cellPrefab.transform.localScale.y; float cellDepth = cellPrefab.transform.localScale.z; float startX = -(cellWidth * width / 2); float startY = -(cellHeight * height / 2); float startZ = -(cellDepth * depth / 2); int gridSize = getSize(); int countBeforeRender = revealBatchSize; for (int i = 0; i < gridSize; i++) { GridCoordinate coords = getCoords(i); GameObject cell = Instantiate(cellPrefab, transform.position, transform.rotation); cell.transform.parent = gameObject.transform; cell.transform.localPosition = new Vector3( startX + coords.x * (cellWidth + cellSpacing), startY + coords.y * (cellHeight + cellSpacing), startZ + coords.z * (cellDepth + cellSpacing) ); MineSweeperCell cellComponent = cell.GetComponent <MineSweeperCell>(); cellComponent.setCoordinates(coords.x, coords.y, coords.z); cells.Add(cellComponent); countBeforeRender--; if (countBeforeRender == 0) { countBeforeRender = revealBatchSize; yield return(null); } } active = true; yield return(null); }
public void revealNeighbours(MineSweeperRevealedCell cell) { int x = cell.getOriginalCell().getX(); int y = cell.getOriginalCell().getY(); int z = cell.getOriginalCell().getZ(); int countMarkedNeighbours = 0; List <MineSweeperCell> unmarkedNeighbours = new List <MineSweeperCell>(); foreach (GridCoordinate coords in neighbourCoordinates(x, y, z)) { MineSweeperCell neighbour = cells[getCellIndex(coords.x, coords.y, coords.z)]; if (neighbour.getState() == CellState.initial) { unmarkedNeighbours.Add(neighbour); } else if (neighbour.getState() == CellState.flagged) { countMarkedNeighbours++; } } if (countMarkedNeighbours == cell.getCountNeighbours()) { foreach (var unmarkedCell in unmarkedNeighbours) { revealCell(unmarkedCell); } } }
public void reveal(MineSweeperCell cell) { if (cell.getState() == CellState.initial) { grid.revealCell(cell); } }
private void showNeighboursCount(MineSweeperCell cell, int nbNeighbourMines) { GameObject obj = Instantiate(revealedCellPrefab, cell.gameObject.transform.position, Quaternion.identity); MineSweeperRevealedCell revealedCell = obj.GetComponent <MineSweeperRevealedCell>(); revealedCell.setOriginalCell(cell); revealedCell.setValue(nbNeighbourMines); obj.transform.parent = gameObject.transform; }
void showMines(MineSweeperCell clickedMine) { foreach (var mineIndex in mines) { MineSweeperCell cell = cells[mineIndex]; if (cell.getState() != CellState.flagged) { cell.showMine(cell == clickedMine); } } }
private void mark(MineSweeperCell cell) { if (cell.getState() == CellState.flagged) { grid.removeFoundMine(); } cell.mark(); if (cell.getState() == CellState.flagged) { grid.addFoundMine(); } mineCounter.text = grid.getCountUnfoundMines().ToString(); }
IEnumerator replaceCellWithRevealed(MineSweeperCell cell) { List <MineSweeperCell> cellsToReveal = new List <MineSweeperCell>(); cellsToReveal.Add(cell); int countBeforeRender = revealBatchSize; while (cellsToReveal.Count > 0) { MineSweeperCell currentCell = cellsToReveal[0]; cellsToReveal.RemoveAt(0); if (currentCell.getState() != CellState.initial) { continue; } currentCell.setState(CellState.revealed); int currX = currentCell.getX(); int currY = currentCell.getY(); int currZ = currentCell.getZ(); int nbNeighbourMines = countNeighbourMines(currX, currY, currZ); if (nbNeighbourMines > 0) { showNeighboursCount(currentCell, nbNeighbourMines); } else { foreach (GridCoordinate coords in neighbourCoordinates(currX, currY, currZ)) { MineSweeperCell neighbourCell = cells[getCellIndex(coords.x, coords.y, coords.z)]; if (coords.x == currX && coords.y == currY && coords.z == currZ || neighbourCell.getState() != CellState.initial ) { continue; } cellsToReveal.Add(neighbourCell); } } countBeforeRender--; if (countBeforeRender == 0) { countBeforeRender = revealBatchSize; yield return(null); } currentCell.gameObject.SetActive(false); } setWin(); yield return(null); }
public void revealCell(MineSweeperCell cell) { int x = cell.getX(); int y = cell.getY(); int z = cell.getZ(); if (!acted) { acted = true; setMines(x, y, z); context.startTimer(); } if (cell.hasMine()) { deactivate(); showMines(cell); context.setLost(); } else { StartCoroutine("replaceCellWithRevealed", cells[getCellIndex(x, y, z)]); } }
void removeCellFromHovered(MineSweeperCell cell) { cell.setHovered(false); hoveredCells.Remove(cell); control.vibrate(VibrationType.Gentle); }
void addCellToHovered(MineSweeperCell cell) { cell.setHovered(true); hoveredCells.Add(cell); control.vibrate(VibrationType.Gentle); }
public void setOriginalCell(MineSweeperCell cell) { originalCell = cell; }