// Generate Level from beginning public void GenerateStage(BlockNumbers bNumbers) { bNumbers.Clear(); // Added GridLayoutGroup to making GameObjects to be seen more tidy. _gameArea.GetComponent <GridLayoutGroup>().enabled = true; // Generate Tiles 8x8 Array with Stage int[,] cubes = bNumbers.GenerateElementsWithStage(GameManager.Stage); // Match Block Numbers and TileCubes by using dynamically created Buttons for (int i = 0; i < cubes.GetLength(0); i++) { for (int j = 0; j < cubes.GetLength(1); j++) { if (cubes[i, j] != 0) { // Setting Properties of Number, Instantiate its GameObject and // Added to dictionary to be controlled easily. int number = cubes[i, j]; GameObject cube = Instantiate(_tileBlock, _gameArea.transform); bNumbers.SetCubeProperties(cube, number); bNumbers.AddElement(new Vector2Int(i, j), cube); } } } }
// Generate Level from Player's Data public void GenerateStageFromData(BlockNumbers bNumbers, Player p) { bNumbers.Clear(); // No need Grid Layout since GameObjects positions are known. _gameArea.GetComponent <GridLayoutGroup>().enabled = false; var cubes = bNumbers.getTileCube.GetCubes(); var positionOfNumbers = p.PositionOfNumbers; for (int i = 0; i < cubes.GetLength(0); i++) { for (int j = 0; j < cubes.GetLength(1); j++) { if (cubes[i, j] != 0) { foreach (var savedPositions in positionOfNumbers) { // x=0, y=1 indexes string[] vector2Position = savedPositions.Key.Split(','); if (int.Parse(vector2Position[0]) == i && int.Parse(vector2Position[1]) == j) { int number = cubes[i, j]; // Determine GameObject positions and Instantiate them according to data // Then added them to Dictionary GameObject cube = Instantiate(_tileBlock, _gameArea.transform); cube.GetComponent <RectTransform>().sizeDelta = new Vector2(93.5f, 93.5f); bNumbers.SetCubeProperties(cube, number); bNumbers.AddElement(new Vector2Int(i, j), cube); string[] vector3Position = savedPositions.Value.Split(','); int cubeX = int.Parse(vector3Position[0]); int cubeY = int.Parse(vector3Position[1]); cube.transform.localPosition = new Vector3(cubeX, (_gameArea.GetComponent <RectTransform>().rect.height / 2) - i * 20); } } } } } }