private Board(GameObject boardPanel, GameData gameData, MonoBehaviour mono) { _boardPanel = boardPanel; _mono = mono; _gameData = gameData; CellRatio = Options.Instance.CellRatio; Level = Options.Instance.SelectedLevel; loadingSlider = GameObject.FindGameObjectWithTag("LoadingSlider").GetComponent <Slider>(); //CellPrefab = Instantiate(prefabInstance); //FacingDownSprite = Instantiate(PrefabHelper.Instance.FacingDownSprite); //EmptySprite = Instantiate(PrefabHelper.Instance.EmptySprite); //FlagSprite = Instantiate(PrefabHelper.Instance.FlagSprite); //BombSprite = Instantiate(bombSprite); //ExplodedBombSprite = Instantiate(explodedBombSprite); //BadBombGuessSprite = Instantiate(badBombGuessSprite); //BombNumberSprite = bombNumberSprite; //ExplosionPrefab = explosionPrefab; //ResizeBoard(CellRatio); _mono.StartCoroutine(ResizeBoard(CellRatio)); //ResizeBoard(CellRatio); //ResetBoard(); }
public IEnumerator ResizeBoard(float newCellRatio, bool reset = true) { Debug.Log("RESIZEBOARD!"); loadingSlider.value = 0; initializing = true; int currentWidth = Width; int currentHeight = Height; bool cellRatioChanged = CellRatio != newCellRatio; CellRatio = newCellRatio; float startBoardPanelHeight = _boardPanel.GetComponent <RectTransform>().rect.height; float startBoardPanelWidth = _boardPanel.GetComponent <RectTransform>().rect.width; float cellHeight = PrefabHelper.Instance.CellPrefab.GetComponent <RectTransform>().rect.height *CellRatio; float cellWidth = PrefabHelper.Instance.CellPrefab.GetComponent <RectTransform>().rect.width *CellRatio; Debug.Log("Height: " + startBoardPanelHeight + " - Width: " + startBoardPanelWidth); Debug.Log("Cell dimension: " + cellHeight); float widthOffset = 0; System.Diagnostics.Stopwatch sw; if (reset) { nbGoodFlags = 0; nbFlags = 0; boardExploded = false; gameStarted = false; gameEnded = false; gamePaused = false; startTime = 0; sw = new System.Diagnostics.Stopwatch(); sw.Start(); if (currentWidth != Width || currentHeight != Height || cellRatioChanged) { DestroyAllCells(true); } else { DestroyAllCells(); } sw.Stop(); Debug.Log("DestroyAllCells() took " + sw.Elapsed); Width = (int)Mathf.Floor(startBoardPanelWidth / cellWidth); Height = (int)Mathf.Floor(startBoardPanelHeight / cellHeight); if (currentWidth != Width || currentHeight != Height || cellRatioChanged) { widthOffset = (startBoardPanelWidth - (Width * cellWidth)) / 2; Debug.Log("Nb width: " + Width + " - Nb height " + Height); Cells = new Cell[Height, Width]; } Level = Options.Instance.SelectedLevel; BombCount = GetBombNumber(); } else { Cell[,] tempCellArray = RotateBoardClockwise(_board.Cells); int oldWidth = Width; Width = Height; Height = oldWidth; widthOffset = (startBoardPanelHeight - (Width * cellWidth)) / 2; //DestroyAllCells(); _board.Cells = new Cell[Height, Width]; Array.Copy(tempCellArray, _board.Cells, tempCellArray.Length); } Debug.Log("Offset: " + widthOffset); sw = new System.Diagnostics.Stopwatch(); sw.Start(); int nbCells = Width * Height; loadingSlider.minValue = 0; loadingSlider.maxValue = nbCells; int loadingCounter = 1; for (int row = 0; row < Height; row++) { for (int col = 0; col < Width; col++) { if (reset) { //System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch(); //sw2.Start(); if (currentWidth != Width || currentHeight != Height || cellRatioChanged) { GameObject newCell = Instantiate(PrefabHelper.Instance.CellPrefab); newCell.transform.localScale = new Vector3(CellRatio, CellRatio, CellRatio); newCell.transform.position = new Vector3((cellWidth * col) + widthOffset, -cellHeight * row, newCell.transform.position.z); newCell.transform.SetParent(_boardPanel.transform, false); Button button = newCell.GetComponent <Button>(); newCell.AddComponent(typeof(CellComponent)); var cell = newCell.GetComponent <CellComponent>(); cell.row = row; cell.col = col; Cells[row, col] = new Cell(newCell, Width, row, col) { NbBomb = 0, Content = Cell.CONTENT.EMPTY, State = Cell.STATE.COVERED, Selected = false }; } else { Cells[row, col].NbBomb = 0; Cells[row, col].Content = Cell.CONTENT.EMPTY; Cells[row, col].State = Cell.STATE.COVERED; Cells[row, col].Selected = false; } //sw2.Stop(); //Debug.Log("Cell initialization took " + sw2.Elapsed); } else { //_board.Cells[row, col]._cell.transform.localScale = new Vector3(CellRatio, CellRatio, CellRatio); _board.Cells[row, col]._cell.transform.position = new Vector3((cellWidth * col) + widthOffset, -cellHeight * row, _board.Cells[row, col]._cell.transform.position.z); //_board.Cells[row, col]._cell.transform.SetParent(_boardPanel.transform, false); } loadingSlider.value = loadingCounter; loadingCounter++; } yield return(null); } sw.Stop(); Debug.Log("Init board " + sw.Elapsed); if (reset) { sw = new System.Diagnostics.Stopwatch(); sw.Start(); InitializeBoardBombs(); sw.Stop(); Debug.Log("InitializeBoardBombs() took " + sw.Elapsed); //if (GameObject.FindGameObjectWithTag("BackgroundBlackPanel").GetComponent<CanvasGroup>().alpha > 0) { GameObject.FindGameObjectWithTag("LoadingPanel").GetComponent <Animator>().Play("LoadingPanelClose"); GameObject.FindGameObjectWithTag("BackgroundBlackPanel").GetComponent <Animator>().Play("BackgroundBlackPanelDisable"); } } initializing = false; if (gamePaused) { gamePaused = false; } }