Exemple #1
0
        private static GameObject generateIndicator(int height)
        {
            GameObject indicator = GridElementBuilder.generateWallElement();

            indicator.transform.position = new Vector3(0, height + .01f, 0);
            indicator.gameObject.name    = "indicator";
            return(indicator);
        }
Exemple #2
0
        // Start is called before the first frame update
        public static GameObject buildElement(int height)
        {
            GameObject terrainElement = new GameObject();

            GridElementBuilder.generateCube(height).transform.parent      = terrainElement.transform;
            GridElementBuilder.generateIndicator(height).transform.parent = terrainElement.transform;
            GridElementBuilder.generateMark(height).transform.parent      = terrainElement.transform;

            terrainElement.AddComponent <GridElement>();

            return(terrainElement);
        }
Exemple #3
0
        private static GameObject generateCube(int height)
        {
            GameObject cubeParent = new GameObject();

            cubeParent.gameObject.name = "cube";
            GameObject bottom = GridElementBuilder.generateWallElement();

            bottom.gameObject.name    = "bottom";
            bottom.transform.parent   = cubeParent.transform;
            bottom.transform.position = new Vector3(0, 0, 0);

            GameObject northWallContainer = new GameObject();

            northWallContainer.gameObject.name  = "north";
            northWallContainer.transform.parent = cubeParent.transform;
            GameObject eastWallContainer = new GameObject();

            eastWallContainer.gameObject.name  = "east";
            eastWallContainer.transform.parent = cubeParent.transform;
            GameObject southWallContainer = new GameObject();

            southWallContainer.gameObject.name  = "south";
            southWallContainer.transform.parent = cubeParent.transform;
            GameObject westWallContainer = new GameObject();

            westWallContainer.gameObject.name  = "west";
            westWallContainer.transform.parent = cubeParent.transform;

            for (int i = 0; i < height; i++)
            {
                GameObject[] walls = GridElementBuilder.generateCubeWalls(i);
                walls[0].transform.parent = northWallContainer.transform;
                walls[1].transform.parent = eastWallContainer.transform;
                walls[2].transform.parent = southWallContainer.transform;
                walls[3].transform.parent = westWallContainer.transform;
            }

            GameObject top = GridElementBuilder.generateWallElement();

            top.gameObject.name    = "top";
            top.transform.parent   = cubeParent.transform;
            top.transform.position = new Vector3(0, height, 0);

            return(cubeParent);
        }
Exemple #4
0
        private static GameObject[] generateCubeWalls(int iteration)
        {
            float height = iteration + 0.5f;

            GameObject[] walls = new GameObject[4];

            GameObject northWall = GridElementBuilder.generateWallElement();

            northWall.gameObject.name       = "northWall" + iteration;
            northWall.transform.position    = new Vector3(0, height, -0.5f);
            northWall.transform.eulerAngles = new Vector3(90f, 0, 0);
            walls[0] = northWall;

            GameObject eastWall = GridElementBuilder.generateWallElement();

            eastWall.gameObject.name       = "eastWall" + iteration;
            eastWall.transform.position    = new Vector3(0.5f, height, 0);
            eastWall.transform.eulerAngles = new Vector3(90f, 0, 90f);
            walls[1] = eastWall;

            GameObject southWall = GridElementBuilder.generateWallElement();

            southWall.gameObject.name       = "southWall" + iteration;
            southWall.transform.position    = new Vector3(0, height, 0.5f);
            southWall.transform.eulerAngles = new Vector3(90f, 0, 0);
            walls[2] = southWall;

            GameObject westWall = GridElementBuilder.generateWallElement();

            westWall.gameObject.name       = "westWall" + iteration;
            westWall.transform.position    = new Vector3(-0.5f, height, 0);
            westWall.transform.eulerAngles = new Vector3(90f, 0, 90f);
            walls[3] = westWall;

            return(walls);
        }
Exemple #5
0
        private void Awake()
        {
            string[] fileContent;
            float    positionX             = 0;
            float    positionZ             = 0;
            int      gridCounterHorizontal = 0;
            int      gridCounterVertical   = 0;

            if (GameObject.Find("terrainParent") != null)
            {
                Console.WriteLine("terrainParent already exists in scene - aborting.");
                return;
            }

            GameObject terrainParent = new GameObject("terrainParent");

            terrainParent.AddComponent <GridController>();

            try {
                fileContent = File.ReadAllLines("Assets/generate.txt");
            } catch (FileNotFoundException e) {
                Console.WriteLine(e);
                throw;
            }

            terrainParent.transform.position = new Vector3(0, 0, 0);

            foreach (string line in fileContent)
            {
                string[] map = line.Split('|');
                gridCounterVertical += 1;

                foreach (string tileInfo in map)
                {
                    string[] tileInfoSplit = tileInfo.Split(',');
                    int      height        = int.Parse(tileInfoSplit[0]);
                    string   terrain       = tileInfoSplit[1];

                    gridCounterHorizontal += 1;

                    GameObject gridElement = GridElementBuilder.buildElement(height);
                    gridElement.GetComponent <GridElement>().setTileType(this.getTileType(terrain));
                    gridElement.GetComponent <GridElement>().setHeight(height);
                    gridElement.transform.parent = terrainParent.transform;

                    if (gridElement == null)
                    {
                        throw new Exception("terrainElement could not be loaded from Prefabs. Aborting.");
                    }

                    gridElement.transform.name     = string.Concat(gridCounterVertical, "-", gridCounterHorizontal);
                    gridElement.transform.position = new Vector3(positionX, 1f, positionZ);

                    positionX += 1;
                }

                gridCounterHorizontal = 0;

                positionZ -= 1;
                positionX  = 0;
            }
        }