Beispiel #1
0
    //this Gameobject array is returned to tileGenerator with all the values for the cubes to be stored in the ChunkArray, aswell as tellingthe CubeGenerator script where to offset the cubes
    public GameObject[,,] CubeMake(int length, int depth, int height, float[,] heightMap, int chunkX, int chunkY)
    {
        //paramters are obtained from the tileGenrator script and they specift the exact size of the chunk, then the script runs through each value in the nested loops to check
        //with the heightmap values whether a cube should be spawned in that x,y,z coordinate or not

        cubeArray = new GameObject[length, height, depth];
        for (int xIndex = 0; xIndex < length; xIndex++)
        {
            for (int zIndex = 0; zIndex < depth; zIndex++)
            {
                for (int yIndex = 0; yIndex < height; yIndex++)
                {
                    int xOffset = chunkX * length;
                    int zOffset = chunkY * depth;

                    // height map is 0-1 also divided by 2.0 as a seondary map scale a heigher number will be smoother a lower number is more tall and rigid,
                    // y/height is in decimal form which will be 0-1.

                    //if the heighmap values at the zIndex and yIndex postions are greater then the yIndex devided by the total height specified example 4/10 will be 0.4
                    //then the perlin noise values will be in float form for example 0.5, then it will spawn a cube here
                    if (heightMap[xIndex + xOffset, zIndex + zOffset] / 2.0 > yIndex / (double)height)
                    {
                        //runs the funtion to infrom the cubeGenerator script if there are neihgbors to the sides or not
                        Sides neighbours = CalculateNeighbours(heightMap, new Vector3(xIndex + xOffset, yIndex / (float)height, zIndex + zOffset));

                        //each cube will no be made a gameobject with this line we creat a gameobject then later its assigned to a cube
                        GameObject go = new GameObject();

                        //gameobject that was just made is added to the index of the aray wherever the nested loops are currently at
                        //so if this statment doesnt run form the if statment it will be simply empty space with nothing in it but the array is intialized to be a certain size so thats how we can add blocks into the map and onto the array
                        cubeArray[xIndex, yIndex, zIndex] = go;

                        //sending all parameters to the CubeGeneator script norde rto initialize a cube in the world

                        CubeGenerator.CreateCube(go, new Vector3(0, 0, 0), textureAtlas,
                                                 ChooseTerrainType(heightMap[xIndex + xOffset, zIndex + zOffset]), neighbours);

                        //these sections allow each face to have a coordinate position and lets us set up a naming convention to indicate the chunk and x,y,z place of each cube within the unity hierachy
                        go.transform.position = new Vector3(xIndex + xOffset, yIndex, zIndex + zOffset);

                        go.name = string.Format("Chunk(x:{0:D2} y:{1:D2})Index(x:{2:D2}y:{3:D2}z:{4:D2})",
                                                chunkX, chunkY, xIndex, yIndex, zIndex);
                    }
                }
            }
        }

        return(cubeArray);
    }
    public void RayDetect()
    {
        //creates a ray based on the position of the player Camera
        Debug.DrawRay(playerCamera.transform.position, playerCamera.transform.forward * 10, Color.green);

        //adding a block on left click
        if (Input.GetMouseButtonDown(0))
        {   //sends out a ray in direction of player
            if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, 10))
            {
                Debug.Log(hit.collider.name);
                //hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = Color.red;


                //this bunch of TryParse's read off the postion of the long string name of each cube in the hierachy
                //we can access the number for the chunk its in as well as the x,y,z spot of each cube
                int xIn;
                int.TryParse(hit.transform.parent.name.Substring(24, 2), out xIn);
                int yIn;
                int.TryParse(hit.transform.parent.name.Substring(28, 2), out yIn);
                int zIn;
                int.TryParse(hit.transform.parent.name.Substring(32, 2), out zIn);

                int xChunk;
                int.TryParse(hit.transform.parent.name.Substring(8, 2), out xChunk);
                int yChunk;
                int.TryParse(hit.transform.parent.name.Substring(13, 2), out yChunk);

                Debug.Log($"x: {xIn} y: {yIn} z: {zIn}");
                Debug.Log("Chunk X = " + xChunk + "Chunk Y =" + yChunk);
                Debug.Log(hit.transform.position);

                //if we hit a ibject with the raycast we create a new gamobject which will have faces added to it
                GameObject go = new GameObject();
                go.transform.position = new Vector3(0, 0, 0);

                //we ge the chunkarray from tileGenerator script and add the gamobject to to the spot the collider hit
                tileGeneratorScript.chunkArray[xChunk, yChunk][xIn, yIn, zIn] = go;

                Debug.Log("this is before if: " + textureX + " " + textureY);

                //the if clusters identifys which face we hit with the ray and then we run the createcube function from cubeGenerator script to instatiate and entire new cube ajacent to the face we hit
                // on this first if statment if we hit the right face then we add a block on the xindex plus one or to the right of the hit object. we parse in all necesary paramters to make a cube and
                //we run a sperate neighbour check function that is on this script instead
                if (hit.collider.name == "rightFace")
                {
                    CubeGenerator.CreateCube(go, new Vector3((xIn + 1) + (xChunk * 10), yIn, zIn + (yChunk * 10)), hit.collider.gameObject.GetComponent <MeshRenderer>().material, new Vector2(textureX, textureY), CalculateNeighbours(go, cubeSpawnScript.cubeArray));

                    go.name = string.Format("Chunk(x:{0:D2} y:{1:D2})Index(x:{2:D2}y:{3:D2}z:{4:D2})", xChunk, yChunk, xIn + 1, yIn, zIn);
                    Debug.Log("this is after if: " + textureX + " " + textureY);
                }
                if (hit.collider.name == "leftFace")
                {
                    CubeGenerator.CreateCube(go, new Vector3((xIn - 1) + (xChunk * 10), yIn, zIn + (yChunk * 10)), hit.collider.gameObject.GetComponent <MeshRenderer>().material, new Vector2(textureX, textureY), CalculateNeighbours(go, cubeSpawnScript.cubeArray));
                    go.name = string.Format("Chunk(x:{0:D2} y:{1:D2})Index(x:{2:D2}y:{3:D2}z:{4:D2})", xChunk, yChunk, xIn - 1, yIn, zIn);
                }
                if (hit.collider.name == "topFace")
                {
                    CubeGenerator.CreateCube(go, new Vector3(xIn + (xChunk * 10), yIn + 1, zIn + (yChunk * 10)), hit.collider.gameObject.GetComponent <MeshRenderer>().material, new Vector2(textureX, textureY), CalculateNeighbours(go, cubeSpawnScript.cubeArray));
                    go.name = string.Format("Chunk(x:{0:D2} y:{1:D2})Index(x:{2:D2}y:{3:D2}z:{4:D2})", xChunk, yChunk, xIn, yIn + 1, zIn);
                    Debug.Log("this is after if: " + textureX + " " + textureY);
                }
                if (hit.collider.name == "frontFace")
                {
                    CubeGenerator.CreateCube(go, new Vector3(xIn + (xChunk * 10), yIn, (zIn + (yChunk * 10)) + 1), hit.collider.gameObject.GetComponent <MeshRenderer>().material, new Vector2(textureX, textureY), CalculateNeighbours(go, cubeSpawnScript.cubeArray));
                    go.name = string.Format("Chunk(x:{0:D2} y:{1:D2})Index(x:{2:D2}y:{3:D2}z:{4:D2})", xChunk, yChunk, xIn, yIn, zIn + 1);
                }
                if (hit.collider.name == "backFace")
                {
                    CubeGenerator.CreateCube(go, new Vector3(xIn + (xChunk * 10), yIn, (zIn - 1) + (yChunk * 10)), hit.collider.gameObject.GetComponent <MeshRenderer>().material, new Vector2(textureX, textureY), CalculateNeighbours(go, cubeSpawnScript.cubeArray));
                    go.name = string.Format("Chunk(x:{0:D2} y:{1:D2})Index(x:{2:D2}y:{3:D2}z:{4:D2})", xChunk, yChunk, xIn, yIn, zIn - 1);
                }

                if (hit.collider.name == "bottomFace")
                {
                    CubeGenerator.CreateCube(go, new Vector3(xIn + (xChunk * 10), yIn - 1, zIn + (yChunk * 10)), hit.collider.gameObject.GetComponent <MeshRenderer>().material, new Vector2(textureX, textureY), CalculateNeighbours(go, cubeSpawnScript.cubeArray));
                    go.name = string.Format("Chunk(x:{0:D2} y:{1:D2})Index(x:{2:D2}y:{3:D2}z:{4:D2})", xChunk, yChunk, xIn, yIn - 1, zIn);
                }
            }
        }
        //removing a block on right click
        else if (Input.GetMouseButtonDown(1))
        {
            if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, 10))
            {
                int xIn;
                int.TryParse(hit.transform.parent.name.Substring(24, 2), out xIn);
                int yIn;
                int.TryParse(hit.transform.parent.name.Substring(28, 2), out yIn);
                int zIn;
                int.TryParse(hit.transform.parent.name.Substring(32, 2), out zIn);

                int xChunk;
                int.TryParse(hit.transform.parent.name.Substring(8, 2), out xChunk);
                int yChunk;
                int.TryParse(hit.transform.parent.name.Substring(13, 2), out yChunk);


                CubeGenerator.CreateCube(tileGeneratorScript.chunkArray[xChunk, yChunk][(xIn - 1), yIn, zIn], new Vector3((xIn - 1) + (xChunk * 10), yIn, zIn + (yChunk * 10)), hit.collider.gameObject.GetComponent <MeshRenderer>().material
                                         , new Vector2(3, 16), CalculateNeighbours(tileGeneratorScript.chunkArray[xChunk, yChunk][(xIn - 1), yIn, zIn], cubeSpawnScript.cubeArray));


                tileGeneratorScript.chunkArray[xChunk, yChunk][xIn, yIn, zIn] = null;
                Destroy(hit.transform.parent.gameObject);
            }
        }
    }