// Edge decetion along one axis void EdgeDetection(int offsetX, int offsetZ, bool initialCheck = true) { // Assign references to the cube gameobject and cubedata script GameObject cube = map.GridCubeArray()[(int)cubeCoord.x + offsetX, (int)cubeCoord.z + offsetZ]; CubeData cubeData = cube.GetComponent <CubeData>(); // If the cube exsists on the x axis and is not of type metal e.g. (border) if (cube.gameObject != null && cubeData.cubeType != CUBETYPE.BORDER) { // If the edge detection is during map generation if (initialCheck) { // Set the cube as an edge cube - update material cubeData.SetAsEdgeCube(); // If the edge cube is a cyrstal cube change it to a rock cube if (cubeData.cubeType == CUBETYPE.CRYSTAL) { cubeData.SetAsRockCube(); cubeData.SetAsEdgeCube(); } } // Else if the edge dectetion is in game else if (!initialCheck && cubeData.cubeType == CUBETYPE.ROCK && cube.GetComponent <MeshRenderer>() != null) { cubeData.SetAsEdgeCube(); } } }
// 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"); }