Esempio n. 1
0
    // 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;
                }
            }
        }
    }