///////////////////////////Grid Cubes/////////////////////////// // Record the grid cubes (start and end of game) public void RecordGridCubes(GameObject[,] gridCubes, bool start) { // Loop through all the cubes foreach (GameObject go in gridCubes) { // Add cube to the cube positions dictionary CubeData cubeData = go.GetComponent <CubeData>(); Vector3 cubePos = cubeData.GetCubeCoord(); // Start of the game if (start) { cubePositionsStart.Add(new CubeStat { position = cubePos, type = cubeData.GetCubeType() }); } // End of the game else { cubePositionsEnd.Add(new CubeStat { position = cubePos, type = cubeData.GetCubeType() }); } } }
// Generate tyhe grid cubes void GenerateGridCubes() { // Initialise the grid cube array gridCubeArray = new GameObject[gridWidth, gridHeight]; // Build the map grid - loop through height of grid for (int z = 0; z < gridHeight; z++) { // Loop through width of grid for (int x = 0; x < gridWidth; x++) { // Build the grid - instantiate grid cube calculated position - assign the cube as a child of the gridCubes object GameObject cube = Instantiate(gridCube, new Vector3(x * 1.0f, 0.0f, z * 1.0f), Quaternion.identity, gridCubes.transform) as GameObject; cube.transform.localScale = new Vector3(cubeScale, cubeScale, cubeScale); // Assign the grid cube to the array gridCubeArray[x, z] = cube; // Reference to the cube data CubeData cubeData = gridCubeArray[x, z].GetComponent <CubeData>(); // Set the cubes coord in the grid and set all cubes to closed (initially) cubeData.SetCubeCoord(new Vector3(x, 0.0f, z)); cubeData.SetCubeOpen(false); // Random between 0 and 1 - used to select rock type float crystalChance = Random.Range(0.0f, 1.0f); // Set the grid borders as border cubes - Set the gird cube indestructible and the cube type as border if (x == 0 || z == 0 || x == 1 || z == 1 || x == gridWidth - 1 || z == gridHeight - 1 || x == gridWidth - 2 || z == gridHeight - 2) { cubeData.SetAsBorderCube(); } // Set the cube as crystal cubes - Set the gird cube destructible and the cube type as rock else if (crystalChance < crystalCubeChance) { cubeData.SetAsCystalCube(); } // Set the rest of the cubes as rock cubes -- Set the gird cube destructible and the cube type as rock else { cubeData.SetAsRockCube(); } // If the cube is a rock/crystal or border cube if (cubeData.GetCubeType() == CUBETYPE.BORDER || cubeData.GetCubeType() == CUBETYPE.ROCK || cubeData.GetCubeType() == CUBETYPE.CRYSTAL) { // Set the scale and rotation of the grid cubes cube.transform.localScale = new Vector3(cubeScale, cubeScale, cubeScale); cube.transform.rotation = Random.rotation; } } } Debug.Log("MAP - GEN GRID CUBES - COMPLETE"); }
// Position a gameobject in place of a rock cube public void PositionChestInRock(GameObject go) { // Set eth gameobject as not spawned bool spawned = false; // While the gameobject has not been spawned in while (!spawned) { // Counter for the open grid cubes int allCubesClosed = 0; // Choose a random grid x and y coord Vector3 randomPos = RandomPosition(); // Assign the cubedata CubeData cubeData = gridCubeArray[(int)randomPos.x, (int)randomPos.z].GetComponent <CubeData>(); // If the cube type is rock if (cubeData.GetCubeType() == CUBETYPE.ROCK) { // Loop through the adjacent grid cubes - z axis for (int z = -1; z < 2; z++) { // Loop through the adjacent grid cubes - x axis for (int x = -1; x < 2; x++) { // Assign the cubedata CubeData cubeDataToCheck = gridCubeArray[(int)randomPos.x + x, (int)randomPos.z + z].GetComponent <CubeData>(); // If the grid cube at the random coord is not open if (!cubeDataToCheck.GetCubeOpen()) { // Count the adjacent closed cube (plus 1) allCubesClosed++; } } } // If all the adjacent grid cubes are closed if (allCubesClosed == 9) { // Open the grid cube and set as chest cube cubeData.OpenGridCube(); cubeData.SetAsChestCube(); // Rotate gameobject to face out from walls if (randomPos.x <= gridWidth / 2 && randomPos.z <= gridHeight / 2) { go.transform.Rotate(Vector3.up, 90.0f, Space.Self); } if (randomPos.x <= gridWidth / 2 && randomPos.z > gridHeight / 2) { go.transform.Rotate(Vector3.up, 180.0f, Space.Self); } if (randomPos.x > gridWidth / 2 && randomPos.z > gridHeight / 2) { go.transform.Rotate(Vector3.up, -90.0f, Space.Self); } // Set the gamobjects position go.transform.position = new Vector3(randomPos.x, go.transform.position.y, randomPos.z); // Break out of the loop and spawn the gameobject spawned = true; } } } }