/// <summary> /// Inits any manully placed tiles. This is required because some tiles may be placed manually in the level /// prefab at design time which wouldn't allow them to get initialized in the same order with other spawned tiles. /// So these have to be separatelly inititalized after the board determines its neighbors and links. /// </summary> public void InitBoardTiles() { // Ensure the board has at least one possible match (but not already made matches). int numTries = 0; // matchesUndoer.OnNewTileSpawned = (newTile) => { // newTile.enabled = false; // }; // // possibleMatchGenerator.OnNewTileSpawned = (newTile) => { // newTile.enabled = false; // }; MatchesUndoer matchesUndoer = new MatchesUndoer(Board); PossibleMatchesFinder possibleMatchesFinder = new PossibleMatchesFinder(Board); PossibleMatchGenerator possibleMatchGenerator = new PossibleMatchGenerator(Board); while ((numTries++) < 10 && (matchesUndoer.FindMatchesAndUndo(true) || numTries == 1)) { if (!possibleMatchesFinder.FindFirstPossibleMatch()) { Debug.LogWarning("Forcing a rematch! Last tiles init failed"); possibleMatchGenerator.GenerateMatch(true); } } // matchesUndoer.OnNewTileSpawned = null; // possibleMatchGenerator.OnNewTileSpawned = null; List <Match3Tile> tilesToInitAfterBoardAttach = new List <Match3Tile>(32); // Init all tiles that on the board that haven't been initialized. Board.ApplyActionToAll((boardPiece) => { if (boardPiece.TileRef != null && !(boardPiece.TileRef as Match3Tile).WasInitialized) { // Set the tile's board piece owner (because it might have been manually placed at design time). // Correctly attach the tile to the board piece as if it would have been spawned at run-time. AbstractTile tile = boardPiece.TileRef; boardPiece.TileRef = null; tile.BoardPieceRef = null; tile.InitComponent(); boardPiece.Tile = tile; tilesToInitAfterBoardAttach.Add(tile as Match3Tile); // (tile as Match3Tile).InitAfterAttachedToBoard(); } }); // Execute InitAfterAttachedToBoard for all above initialized tiles. (like Unity does first Awake, then Start for all game objects). for (int i = 0; i < tilesToInitAfterBoardAttach.Count; i++) { tilesToInitAfterBoardAttach[i].InitAfterAttachedToBoard(); } }
/// <summary> /// Defined to also give this component the ability to be enabled/disabled. /// </summary> void Start() { board = Match3BoardGameLogic.Instance.boardData; // Register to the board's OnStable event. Debug.Log("[BoardShuffleController] Registering to the board's OnStableBoard event..."); Match3BoardGameLogic.OnStableBoard += OnStableBoardEvent; matchesFinder = new MatchesFinder(board); possibleMatchesFinder = new PossibleMatchesFinder(board); matchesUndoer = new MatchesUndoer(board); possibleMatchGenerator = new PossibleMatchGenerator(board); }
/// <summary> /// Inits any manully placed tiles. This is required because some tiles may be placed manually in the level /// prefab at design time which wouldn't allow them to get initialized in the same order with other spawned tiles. /// So these have to be separatelly inititalized after the board determines its neighbors and links. /// </summary> public void InitBoardTiles() { // Ensure the board has at least one possible match (but not already made matches). int numTries = 0; // matchesUndoer.OnNewTileSpawned = (newTile) => { // newTile.enabled = false; // }; // // possibleMatchGenerator.OnNewTileSpawned = (newTile) => { // newTile.enabled = false; // }; MatchesUndoer matchesUndoer = new MatchesUndoer(Board); PossibleMatchesFinder possibleMatchesFinder = new PossibleMatchesFinder(Board); PossibleMatchGenerator possibleMatchGenerator = new PossibleMatchGenerator(Board); while ((numTries++) < 10 && (matchesUndoer.FindMatchesAndUndo(true) || numTries == 1)) { if ( !possibleMatchesFinder.FindFirstPossibleMatch() ) { Debug.LogWarning("Forcing a rematch! Last tiles init failed"); possibleMatchGenerator.GenerateMatch(true); } } // matchesUndoer.OnNewTileSpawned = null; // possibleMatchGenerator.OnNewTileSpawned = null; List<Match3Tile> tilesToInitAfterBoardAttach = new List<Match3Tile>(32); // Init all tiles that on the board that haven't been initialized. Board.ApplyActionToAll((boardPiece) => { if ( boardPiece.TileRef != null && !(boardPiece.TileRef as Match3Tile).WasInitialized ) { // Set the tile's board piece owner (because it might have been manually placed at design time). // Correctly attach the tile to the board piece as if it would have been spawned at run-time. AbstractTile tile = boardPiece.TileRef; boardPiece.TileRef = null; tile.BoardPieceRef = null; tile.InitComponent(); boardPiece.Tile = tile; tilesToInitAfterBoardAttach.Add(tile as Match3Tile); // (tile as Match3Tile).InitAfterAttachedToBoard(); } }); // Execute InitAfterAttachedToBoard for all above initialized tiles. (like Unity does first Awake, then Start for all game objects). for(int i = 0; i < tilesToInitAfterBoardAttach.Count; i++) { tilesToInitAfterBoardAttach[i].InitAfterAttachedToBoard(); } }