Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- public class GameBoard : MonoBehaviour
- {
- public Sprite cellCover;
- public Sprite cellBG;
- public Sprite mineSprite;
- public Sprite flagSprite;
- GameObject flagContainer;
- GameObject cellContainer;
- GameObject mineContainer;
- Canvas gameCanvas;
- Image[] mines;
- Vector2Int[] mineLocations;
- public MineSweeperCell[,] cells;
- Vector2Int boardDimensions=new Vector2Int(20,15);
- Vector2Int offSet=Vector2Int.zero;
- int cellSize=40;
- int numberOfMines;
- //public Text debugtext;
- public RectTransform rectTransform;
- public GameState currentGameState;
- //int dontcrash = 0;
- bool flagMode;
- List<FlagPlacement> flags;
- public Image flagModeSprite;
- public TextMeshProUGUI timerText;
- float timer = 0;
- const string audioSaveKey = "mute";
- // Start is called before the first frame update
- void Start()
- {
- mainMenuGO.SetActive(true);
- inGameMenuGO.SetActive(false);
- pauseMenuGO.SetActive(false);
- gameOverScreenGO.SetActive(false);
- winScreenGO.SetActive(false);
- gameCanvas = FindObjectOfType<Canvas>();
- flagContainer = new GameObject();
- flagContainer.transform.parent = gameCanvas.transform;
- flagContainer.transform.localPosition = Vector3.zero;
- flagContainer.transform.localScale = new Vector3(1f, 1f, 1f);
- cellContainer = new GameObject();
- cellContainer.transform.parent = gameCanvas.transform;
- cellContainer.transform.localPosition = Vector3.zero;
- cellContainer.transform.localScale = new Vector3(1f,1f,1f);
- rectTransform = cellContainer.AddComponent<RectTransform>();
- mineContainer = new GameObject();
- mineContainer.transform.parent = gameCanvas.transform;
- mineContainer.transform.localPosition = Vector3.zero;
- mineContainer.transform.localScale = new Vector3(1f, 1f, 1f);
- flagContainer.transform.SetAsFirstSibling();
- cellContainer.transform.SetAsFirstSibling();
- mineContainer.transform.SetAsFirstSibling();
- flagContainer.name="flagContainer";
- cellContainer.name = "cellContainer";
- mineContainer.name = "mineContainer";
- flagMode = false;
- flagModeSprite.color = Color.black;
- if (PlayerPrefs.HasKey(audioSaveKey))
- {
- if (PlayerPrefs.GetInt(audioSaveKey) == 1)
- {
- muted = false;
- audioButtonImage.color = Color.white;
- }
- else
- {
- muted = true;
- audioButtonImage.color = Color.black;
- }
- }
- else
- {
- muted = true;
- audioButtonImage.color = Color.black;
- }
- }
- void Cheat()
- {
- cellContainer.transform.SetAsFirstSibling();
- }
- // Update is called once per frame
- void Update()
- {
- if (!CaptureMouseClick())
- {
- HandleInteraction();
- }
- if (currentGameState == GameState.INPROGRESS || currentGameState == GameState.FIRSTCLICK)
- {
- UpdateTimer();
- }
- if (CheckForWinCondition())
- {
- WinGame();
- }
- if (Input.GetKeyDown(KeyCode.B))
- {
- //Cheat();
- }
- }
- void WinGame()
- {
- currentGameState = GameState.ENDED;
- winScreenGO.SetActive(true);
- string minSec = string.Format("{0}:{1:00}", (int)timer / 60, (int)timer % 60);
- winScreenTimerText.text ="You Win!! In "+ minSec+".";
- }
- bool CaptureMouseClick()
- {
- if (pauseMenuGO.activeSelf)
- {
- return (true);
- }
- if (winScreenGO.activeSelf)
- {
- return (true);
- }
- if (gameOverScreenGO.activeSelf)
- {
- return (true);
- }
- return (false);
- }
- void HandleInteraction()
- {
- if (Input.GetMouseButtonDown(0))
- {
- if (currentGameState == GameState.INPROGRESS)
- {
- if (Input.GetMouseButtonDown(0))
- {
- if (!flagMode)
- {
- Vector2Int temp = HighlightCell(offSet, cellSize);
- if (temp.x != -1)
- {
- UncoverCell(temp);
- }
- }
- else
- {
- Vector2Int temp = HighlightCell(offSet, cellSize);
- if (temp.x != -1)
- {
- PlaceFlag(temp);
- }
- }
- }
- }
- else if (currentGameState == GameState.FIRSTCLICK)//spawn bombs after click
- {
- if (Input.GetMouseButtonDown(0))
- {
- if (!flagMode)
- {
- Vector2Int temp = HighlightCell(offSet, cellSize);
- if (temp.x != -1)
- {
- SpawnMines(numberOfMines, temp);
- UncoverCell(temp);
- }
- }
- else
- {
- Vector2Int temp = HighlightCell(offSet, cellSize);
- if (temp.x != -1)
- {
- PlaceFlag(temp);
- }
- }
- }
- }
- }
- }
- private void HandleMouseClick()
- {
- if (currentGameState == GameState.INPROGRESS)
- {
- Debug.Log("uncover cell");
- if (!flagMode)
- {
- Vector2Int temp = HighlightCell(offSet, cellSize);
- if (temp.x != -1)
- {
- UncoverCell(temp);
- }
- }
- else
- {
- Vector2Int temp = HighlightCell(offSet, cellSize);
- if (temp.x != -1)
- {
- PlaceFlag(temp);
- }
- }
- }
- else if (currentGameState == GameState.FIRSTCLICK)
- {
- Debug.Log("uncover cell");
- if (!flagMode)
- {
- //int temp = HighlightCell(offSet,cellSize);
- Vector2Int temp = HighlightCell(offSet, cellSize);
- if (temp.x != -1)
- {
- SpawnMines(numberOfMines, temp);
- UncoverCell(temp);
- }
- }
- else
- {
- Vector2Int temp = HighlightCell(offSet, cellSize);
- if (temp.x != -1)
- {
- PlaceFlag(temp);
- }
- }
- }
- }
- void InitializeGameBoard(Vector2Int bDimensions, int cSize,Vector2Int bOffset,int nOfMines)
- {
- boardDimensions = bDimensions;
- cellSize = cSize;
- offSet = bOffset;
- numberOfMines = nOfMines;
- if (ShouldSpawnCells())
- {
- ClearCells();
- SpawnBoard();
- }
- else
- {
- RestartGame();
- }
- flagContainer.transform.SetAsFirstSibling();
- cellContainer.transform.SetAsFirstSibling();
- mineContainer.transform.SetAsFirstSibling();
- currentGameState = GameState.FIRSTCLICK;
- flagMode = false;
- flagModeSprite.color = Color.black;
- }
- void PlaceFlag(Vector2Int location)
- {
- if (flags == null)
- {
- flags = new List<FlagPlacement>();
- }
- if (cells[location.x, location.y].isCovered)
- {
- //first check for flag already there
- int index=-1;
- for (int i = 0; i < flags.Count; i++)
- {
- if (flags[i].flagLocation.x == location.x && flags[i].flagLocation.y == location.y)
- {
- index = i;
- }
- }
- if (index != -1)
- {
- Destroy(flags[index].flagImage);
- flags.RemoveAt(index);
- }
- else
- {
- flags.Add(InitializeFlag(new Vector2Int(location.x, location.y)));
- }
- }
- }
- FlagPlacement InitializeFlag(Vector2Int location)
- {
- GameObject temp = new GameObject();
- temp.transform.parent =flagContainer.transform;
- temp.transform.localScale =new Vector3(1f,1f,1f);
- temp.gameObject.SetActive(true);
- temp.transform.position = cells[location.x, location.y].cellImage.transform.position;
- temp.AddComponent<Image>().sprite = flagSprite;
- temp.GetComponent<Image>().rectTransform.sizeDelta = new Vector2(cellSize-20f,cellSize-20f);
- FlagPlacement flpl = new FlagPlacement(temp, location);
- return (flpl);
- }
- public void FlagButton()
- {
- if (flagMode = !flagMode)
- {
- flagModeSprite.color = Color.white;
- }
- else
- {
- flagModeSprite.color = Color.black;
- }
- }
- void SpawnBoard(Vector2Int boardWidthAndHeight, Vector2Int boardOffSet, int boardCellSize)
- {
- Vector2 startingPos = new Vector2(
- ((-boardWidthAndHeight.x * boardCellSize) /2f)+boardCellSize/2f,
- ((-boardWidthAndHeight.y * boardCellSize) / 2f) + boardCellSize / 2f);
- cells = new MineSweeperCell[boardWidthAndHeight.x, boardWidthAndHeight.y];
- //cellCovered=new bool[boardWidthAndHeight.x, boardWidthAndHeight.y];
- for (int y = 0; y < boardWidthAndHeight.y; y++)
- {
- for (int x = 0; x < boardWidthAndHeight.x; x++)
- {
- cells[x, y] = InitiateCell();
- cells[x, y].cellImage.transform.localPosition = new Vector3(
- (x* boardCellSize) +startingPos.x+boardOffSet.x,
- ((boardWidthAndHeight.y-(y+1))*boardCellSize)+startingPos.y+boardOffSet.y,
- 0);
- }
- }
- ClearBombs();
- ClearFlags();
- }
- void SpawnBoard()
- {
- Vector2 startingPos = new Vector2(
- ((-boardDimensions.x * cellSize) /2f)+cellSize/2f,
- ((-boardDimensions.y * cellSize) / 2f) + cellSize / 2f);
- cells = new MineSweeperCell[boardDimensions.x, boardDimensions.y];
- //cellCovered=new bool[boardWidthAndHeight.x, boardWidthAndHeight.y];
- for (int y = 0; y < boardDimensions.y; y++)
- {
- for (int x = 0; x < boardDimensions.x; x++)
- {
- cells[x, y] = InitiateCell();
- cells[x, y].cellImage.transform.localPosition = new Vector3(
- (x* cellSize) +startingPos.x+offSet.x,
- ((boardDimensions.y-(y+1))* cellSize) +startingPos.y+offSet.y,
- 0);
- }
- }
- ClearBombs();
- ClearFlags();
- }
- Vector2Int HighlightCell(Vector2Int boardOffSet, int boardCellSize)
- {
- if (cells == null)
- {
- return (Vector2Int.zero);
- }
- Vector2 uiPos = ConvertScreenSpaceToGridSpace();
- Vector2 startingPos = new Vector2(
- ((-cells.GetLength(0) * boardCellSize) / 2f) + boardCellSize / 2f,
- ((-cells.GetLength(1) * boardCellSize) / 2f) + boardCellSize / 2f);
- for (int y = 0; y < cells.GetLength(1); y++)
- {
- for (int x = 0; x < cells.GetLength(0); x++)
- {
- Vector2 cellPos= new Vector3(
- (x * boardCellSize) + startingPos.x + boardOffSet.x,
- ((cells.GetLength(1) - (y + 1)) * boardCellSize) + startingPos.y + boardOffSet.y
- );
- if (CellContainsPoint(cellPos, uiPos, boardCellSize))
- {
- //debugtext.text = x+", "+y;
- return (new Vector2Int(x,y));
- }
- }
- }
- return (new Vector2Int(-1,-1));
- }
- Vector2 ConvertScreenSpaceToGridSpace()
- {
- Vector2 temp;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, null, out temp);
- return (new Vector2(temp.x,temp.y));
- }
- bool CellContainsPoint(Vector2 cellPos, Vector2 uiPos, int cellBounds)
- {
- if (uiPos.x < cellPos.x - (cellBounds / 2f) || uiPos.x > cellPos.x + (cellBounds / 2f))
- {
- return (false);
- }
- else if (uiPos.y < cellPos.y - (cellBounds / 2f) || uiPos.y > cellPos.y + (cellBounds / 2f))
- {
- return (false);
- }
- return (true);
- }
- MineSweeperCell InitiateCell()
- {
- GameObject tempGO = new GameObject();
- GameObject tempTextGO = new GameObject();
- Text tempText = tempTextGO.AddComponent<Text>();
- tempText.rectTransform.sizeDelta = new Vector2(cellSize - 10, cellSize - 10);
- tempText.alignment = TextAnchor.MiddleCenter;
- tempText.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
- tempText.fontSize = cellSize - 20;
- tempGO.transform.parent = cellContainer.transform;
- tempGO.transform.localScale = new Vector3(1f,1f,1f);
- tempTextGO.transform.parent = tempGO.transform;
- tempTextGO.transform.localScale = new Vector3(1f, 1f, 1f);
- tempTextGO.transform.localPosition = Vector3.zero;
- Image tempCell=tempGO.AddComponent<Image>();
- tempCell.rectTransform.sizeDelta = new Vector2(cellSize, cellSize);
- tempCell.sprite = cellCover;
- tempCell.type = Image.Type.Sliced;
- MineSweeperCell tempMineCell = new MineSweeperCell(tempCell,tempText);
- return (tempMineCell);
- }
- void SpawnMines(int n,Vector2Int excludedCell)
- {
- currentGameState = GameState.INPROGRESS;
- List<Vector2Int> tempList = new List<Vector2Int>();
- for (int y = 0; y < cells.GetLength(1); y++)
- {
- for (int x = 0; x < cells.GetLength(0); x++)
- {
- if (excludedCell.x==x && excludedCell.y==y)
- {
- }
- else if (excludedCell.x-1 == x && excludedCell.y == y)
- {
- }
- else if (excludedCell.x + 1 == x && excludedCell.y == y)
- {
- }
- else if (excludedCell.x == x && excludedCell.y == y+1)
- {
- }
- else if (excludedCell.x - 1 == x && excludedCell.y == y+1)
- {
- }
- else if (excludedCell.x + 1 == x && excludedCell.y == y+1)
- {
- }
- else if (excludedCell.x == x && excludedCell.y == y - 1)
- {
- }
- else if (excludedCell.x - 1 == x && excludedCell.y == y - 1)
- {
- }
- else if (excludedCell.x + 1 == x && excludedCell.y == y - 1)
- {
- }
- else
- {
- tempList.Add(new Vector2Int(x,y));
- }
- }
- }
- int length = tempList.Count - n;
- for (int i = 0; i < length; i++)
- {
- int tempR = Random.Range(0, tempList.Count);
- tempList.RemoveAt(tempR);
- }
- mineLocations = tempList.ToArray();
- mines = new Image[tempList.Count];
- for (int i = 0; i < tempList.Count; i++)
- {
- mines[i] = InitiateMine();
- mines[i].transform.position = cells[mineLocations[i].x, mineLocations[i].y].cellImage.transform.position;
- }
- }
- void SetImagePosition(Transform t, Vector2Int location)
- {
- }
- void ClearBombs()
- {
- currentGameState = GameState.ENDED;
- if (mines != null)
- {
- for (int i = 0; i < mines.Length; i++)
- {
- Destroy(mines[i].gameObject);
- }
- mines = null;
- mineLocations = null;
- }
- timer = 0;
- UpdateTimer();
- }
- Image InitiateMine()
- {
- GameObject tempGO = new GameObject();
- tempGO.transform.parent = mineContainer.transform;
- tempGO.transform.localScale = new Vector3(1f, 1f, 1f);
- Image tempMine = tempGO.AddComponent<Image>();
- tempMine.sprite = mineSprite;
- tempMine.rectTransform.sizeDelta = new Vector2(cellSize,cellSize);
- return (tempMine);
- }
- //int dontcrash = 0;
- void UncoveredMine()
- {
- currentGameState = GameState.ENDED;
- cellContainer.transform.SetAsFirstSibling();
- ShowGameOver();
- ClearFlags();
- }
- void ShowGameOver()
- {
- gameOverScreenGO.SetActive(true);
- }
- void UncoverCell(Vector2Int location)
- {
- if (CellContainsMine(location))//lose game
- {
- Debug.Log("bomb: "+location);
- UncoveredMine();
- }
- if (cells[location.x, location.y].isCovered)
- {
- cells[location.x, location.y].cellImage.sprite = cellBG;
- cells[location.x, location.y].isCovered = false;
- //check nearby cells
- RecursivelyUncoverCells(new Vector2Int(location.x, location.y));
- }
- else
- {
- //uncover all nearby cells if bombs are labeled
- UncoverNearbyCells(location);
- }
- }
- void UncoverNearbyCells(Vector2Int location)
- {
- int bombCount = 0;
- int flagCount = 0;
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- if (location.x - 1 + x >= 0 && location.x - 1 + x < cells.GetLength(0))
- {
- if (location.y - 1 + y >= 0 && location.y - 1 + y < cells.GetLength(1))
- {
- if (CellContainsMine(new Vector2Int(location.x - 1 + x, location.y - 1 + y)))
- {
- bombCount++;
- }
- if (CellContainsFlag(new Vector2Int(location.x - 1 + x, location.y - 1 + y)))
- {
- flagCount++;
- }
- }
- }
- }
- }
- if (flagCount == bombCount)
- {
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- if (location.x - 1 + x >= 0 && location.x - 1 + x < cells.GetLength(0))
- {
- if (location.y - 1 + y >= 0 && location.y - 1 + y < cells.GetLength(1))
- {
- if (cells[location.x - 1 + x, location.y - 1 + y].isCovered)
- {
- if (!CellContainsFlag(new Vector2Int(location.x - 1 + x, location.y - 1 + y)))
- {
- UncoverOneCell(new Vector2Int(location.x - 1 + x, location.y - 1 + y));
- }
- }
- }
- }
- }
- }
- }
- }
- void UncoverOneCell(Vector2Int location)
- {
- if (CellContainsMine(location))//lose game
- {
- UncoveredMine();
- }
- for (int y = 0; y < cells.GetLength(1); y++)
- {
- for (int x = 0; x < cells.GetLength(0); x++)
- {
- if (location.x == x && location.y == y)
- {
- if (cells[x, y].isCovered)
- {
- cells[x, y].cellImage.sprite = cellBG;
- cells[x, y].isCovered = false;
- int bombCount = GetMineCountNumber(location);
- if (bombCount > 0)
- {
- cells[location.x, location.y].cellText.text = bombCount.ToString();
- cells[location.x, location.y].cellText.gameObject.SetActive(true);
- }
- }
- break;
- }
- }
- }
- }
- int GetMineCountNumber(Vector2Int location)
- {
- int mineCount = 0;
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- if (location.x - 1 + x >= 0 && location.x - 1 + x < cells.GetLength(0))
- {
- if (location.y - 1 + y >= 0 && location.y - 1 + y < cells.GetLength(1))
- {
- if (CellContainsMine(new Vector2Int(location.x - 1 + x, location.y - 1 + y)))
- {
- mineCount++;
- }
- }
- }
- }
- }
- return (mineCount);
- }
- bool CellContainsFlag(Vector2Int location)
- {
- if (flags == null)
- {
- return (false);
- }
- for (int i = 0; i < flags.Count; i++)
- {
- if (flags[i].flagLocation.x == location.x && flags[i].flagLocation.y == location.y)
- {
- return (true);
- }
- }
- return (false);
- }
- bool CellContainsMine(Vector2Int location)
- {
- for (int i = 0; i < mineLocations.Length; i++)
- {
- if (mineLocations[i].x==location.x && mineLocations[i].y==location.y)
- {
- Debug.Log(i+": "+mineLocations[i]+" "+location);
- return (true);
- }
- }
- return (false);
- }
- void CheckForBombs(int x, int y)
- {
- int nx = x - 1;
- int ny = y - 1;
- if (nx >= 0 && nx < cells.GetLength(0))
- {
- if (ny >= 0 && ny < cells.GetLength(1))
- {
- }
- }
- }
- int GetCellLocation(Vector2Int location)
- {
- int count = 1;
- for (int y = 0; y < cells.GetLength(1); y++)
- {
- for (int x = 0; x < cells.GetLength(0); x++)
- {
- if (x==location.x && y==location.y)
- {
- return (count);
- }
- count++;
- }
- }
- return (-1);
- }
- void RecursivelyUncoverCells(Vector2Int location)
- {
- int mineCount = 0;
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- if (location.x - 1 + x >= 0 && location.x - 1 + x < cells.GetLength(0))
- {
- if (location.y - 1 + y >= 0 && location.y - 1 + y < cells.GetLength(1))
- {
- if (CellContainsMine(new Vector2Int(location.x - 1 + x, location.y - 1 + y)))
- {
- mineCount++;
- }
- }
- }
- }
- }
- if (mineCount > 0)
- {
- cells[location.x,location.y].cellText.text = mineCount.ToString();
- cells[location.x, location.y].cellText.gameObject.SetActive(true);
- }
- else//uncover all neighboring squares
- {
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- if (location.x - 1 + x >= 0 && location.x - 1 + x < cells.GetLength(0))
- {
- if (location.y - 1 + y >= 0 && location.y - 1 + y < cells.GetLength(1))
- {
- if (cells[location.x - 1 + x, location.y - 1 + y].isCovered)
- {
- cells[location.x - 1 + x, location.y - 1 + y].cellImage.sprite = cellBG;
- cells[location.x - 1 + x, location.y - 1 + y].isCovered = false;
- RecursivelyUncoverCells(new Vector2Int(location.x - 1 + x, location.y - 1 + y));
- }
- }
- }
- }
- }
- }
- }
- public void RestartGame()
- {
- //SpawnBoard(boardDimensions, offSet, cellSize);
- for (int y = 0; y < cells.GetLength(1); y++)
- {
- for (int x = 0; x < cells.GetLength(0); x++)
- {
- cells[x, y].cellImage.sprite = cellCover;
- cells[x, y].isCovered = true;
- cells[x, y].cellText.gameObject.SetActive(false);
- cells[x, y].cellText.text = "";
- }
- }
- ClearBombs();
- ClearFlags();
- currentGameState = GameState.FIRSTCLICK;
- //hide menu
- mainMenuGO.SetActive(false);
- inGameMenuGO.SetActive(true);
- pauseMenuGO.SetActive(false);
- gameOverScreenGO.SetActive(false);
- winScreenGO.SetActive(false);
- //resort sprite containers
- flagContainer.transform.SetAsFirstSibling();
- cellContainer.transform.SetAsFirstSibling();
- mineContainer.transform.SetAsFirstSibling();
- flagMode = false;
- flagModeSprite.color = Color.black;
- }
- bool CheckForWinCondition()//either all empty cells uncovered or all bombs flagged
- {
- if (currentGameState == GameState.ENDED)
- {
- return (false);
- }
- bool winCondition = true;
- if (flags != null && mines!=null)
- {
- if (mines.Length == flags.Count)
- {
- for (int y = 0; y < cells.GetLength(1); y++)
- {
- for (int x = 0; x < cells.GetLength(0); x++)
- {
- if (!CellContainsFlag(new Vector2Int(x, y)) && CellContainsMine(new Vector2Int(x, y)))
- {
- Debug.Log("did not win");
- winCondition = false;
- }
- }
- }
- }
- else
- {
- winCondition = false;
- }
- }
- else
- {
- winCondition = false;
- }
- if (!winCondition && currentGameState==GameState.INPROGRESS)//check if only cells with bombs are covered
- {
- Debug.Log("check other");
- winCondition = true;
- for (int y = 0; y < cells.GetLength(1); y++)
- {
- for (int x = 0; x < cells.GetLength(0); x++)
- {
- if (cells[x,y].isCovered && !CellContainsMine(new Vector2Int(x, y)))
- {
- winCondition = false;
- }
- }
- }
- }
- return (winCondition);
- }
- void ClearFlags()
- {
- if (flags != null)
- {
- for (int i = 0; i < flags.Count; i++)
- {
- Destroy(flags[i].flagImage);
- }
- flags = null;
- }
- }
- //menu stuff
- public GameObject mainMenuGO;
- public GameObject inGameMenuGO;
- public GameObject pauseMenuGO;
- public GameObject gameOverScreenGO;
- public GameObject winScreenGO;
- public TextMeshProUGUI winScreenTimerText;
- public Image audioButtonImage;
- public AudioSource musicSource;
- float volumeLevel = .3f;
- bool muted = false;
- public void NewGame(int i)
- {
- //hide menu
- mainMenuGO.SetActive(false);
- inGameMenuGO.SetActive(true);
- pauseMenuGO.SetActive(false);
- gameOverScreenGO.SetActive(false);
- winScreenGO.SetActive(false);
- if (i == 1)//medium 20 by 12 grid with 30 bombs
- {
- InitializeGameBoard(new Vector2Int(20,12),80, new Vector2Int(0, -120), 30);
- }
- else if (i == 2)//hard 40 by 24 grid with 140 bombs
- {
- InitializeGameBoard(new Vector2Int(40, 24), 40, new Vector2Int(0, -116), 140);
- }
- else//easy 20 by 12 grid with 20 bombs
- {
- InitializeGameBoard(new Vector2Int(20, 12), 80,new Vector2Int(0,-120), 20);
- }
- if (!muted)
- {
- musicSource.Play();
- }
- timer = 0;
- }
- void UpdateTimer()
- {
- timer += Time.deltaTime;
- string minSec = string.Format("{0}:{1:00}", (int)timer / 60, (int)timer % 60);
- timerText.text = minSec;
- }
- public void AudioButton()
- {
- if (muted = !muted)
- {
- audioButtonImage.color = Color.black;
- musicSource.volume = 0;
- PlayerPrefs.SetInt(audioSaveKey, 0);
- }
- else
- {
- audioButtonImage.color = Color.white;
- musicSource.volume = volumeLevel;
- if (!musicSource.isPlaying)
- {
- musicSource.Play();
- }
- PlayerPrefs.SetInt(audioSaveKey, 1);
- }
- }
- public void ExitGame()
- {
- mainMenuGO.SetActive(true);
- inGameMenuGO.SetActive(false);
- pauseMenuGO.SetActive(false);
- gameOverScreenGO.SetActive(false);
- winScreenGO.SetActive(false);
- }
- public void ResumeGame()
- {
- mainMenuGO.SetActive(false);
- inGameMenuGO.SetActive(true);
- pauseMenuGO.SetActive(false);
- }
- public void PauseGame()
- {
- mainMenuGO.SetActive(false);
- inGameMenuGO.SetActive(false);
- pauseMenuGO.SetActive(true);
- }
- bool ShouldSpawnCells()
- {
- if (cells == null)
- {
- return (true);
- }
- if (boardDimensions.x != cells.GetLength(0) || boardDimensions.y != cells.GetLength(1))
- {
- return (true);
- }
- return (false);
- }
- void ClearCells()
- {
- Debug.Log("ClearCells");
- if (cells == null)
- {
- return;
- }
- for (int x = 0; x < cells.GetLength(0); x++)
- {
- for (int y = 0; y < cells.GetLength(1); y++)
- {
- Destroy(cells[x, y].cellImage.gameObject);
- }
- }
- cells = null;
- }
- }
- class FlagPlacement
- {
- public GameObject flagImage;
- public Vector2Int flagLocation;
- public FlagPlacement(GameObject f,Vector2Int l)
- {
- flagImage = f;
- flagLocation = l;
- }
- }
- public class MineSweeperCell
- {
- public Image cellImage;
- public Text cellText;
- public bool isCovered;
- public MineSweeperCell(Image i, Text t)
- {
- cellImage = i;
- cellText = t;
- isCovered = true;
- }
- }
- public enum GameState
- {
- ENDED,INPROGRESS,FIRSTCLICK
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement