bool GetShape(out Shape shape, Cell firstBlock) { shape = new Shape(); Cell newShapeBlock = firstBlock; for (int i = 0; i < 3; i++) { // Add Cell shape.blocks.Add(newShapeBlock); grid[newShapeBlock.x, newShapeBlock.y] = true; // Get List of Neighbour Cells List <Cell> neighbourCells; if (neighbourCellsSearch == SearchType.AllBlocks) { neighbourCells = Neighbours.Shape(shape, gridSize, grid); } else { neighbourCells = Neighbours.Block(newShapeBlock, gridSize, grid); } // Choose a random Neighbour Cell if (neighbourCells.Count > 0) { newShapeBlock = neighbourCells[Random.Range(0, neighbourCells.Count)]; } else { return(false); } } // Add Cell shape.blocks.Add(newShapeBlock); grid[newShapeBlock.x, newShapeBlock.y] = true; return(true); }
IEnumerator GenerateGrid() { for (int x = 0; x < gridSize.x; x++) { for (int y = 0; y < gridSize.y; y++) { if (grid[x, y] == false) { Shape shape = new Shape(); Cell newShapeBlock = new Cell(x, y); bool success = true; int numberOfBlocks = 4; while (numberOfBlocks > 0 && success) { numberOfBlocks--; // Add Cell shape.blocks.Add(newShapeBlock); grid[newShapeBlock.x, newShapeBlock.y] = true; // Get List of Neighbour Cells List <Cell> neighbourCells; if (neighbourCellsSearch == SearchType.AllBlocks) { neighbourCells = Neighbours.Shape(shape, gridSize, grid); } else { neighbourCells = Neighbours.Block(newShapeBlock, gridSize, grid); } // Animation SetTempShapeBlock(shape, newShapeBlock.x, newShapeBlock.y); SetTempNeighbourBlocks(neighbourCells); yield return(new WaitForSeconds(animTime)); if (numberOfBlocks > 0) { // Choose a random Neighbour Cell if (neighbourCells.Count > 0) { newShapeBlock = neighbourCells[Random.Range(0, neighbourCells.Count)]; } else { success = false; } } } // Deactivate temp neighbour blocks foreach (Transform block in tempNeighbourBlocks) { block.gameObject.SetActive(false); } // Wait for last temp shape block if (success) { yield return(new WaitForSeconds(animTime)); } // Deactivate temp shape blocks foreach (Transform block in tempShapeBlocks) { block.gameObject.SetActive(false); } if (success) { shapes.Add(shape); // Color Color color = colors.colors[colorIndex]; colorIndex++; if (colorIndex >= colors.colors.Length) { colorIndex = 0; } ShapeObject.CreateShape(shape, blockPrefab, positionOffset, colors.colors[colorIndex]); yield return(new WaitForSeconds(animTime)); } else { foreach (Cell cell in shape.blocks) { grid[cell.x, cell.y] = false; } } } } } }