/// <summary>
        /// Delete the current level from the scene
        /// </summary>
        public override void DeleteLevel()
        {
            // Check if there is something to delete first or else there will be an error
            GeneratedDungeon outputtedTerrain = GameObject.FindObjectOfType <GeneratedDungeon>();

            if (outputtedTerrain != null)
            {
                MonoBehaviour.DestroyImmediate(outputtedTerrain.gameObject);
            }
        }
Exemple #2
0
        /// <summary>
        /// Spawn the dungeon onto the scene
        /// </summary>
        /// <param name="reUseGameObject">If the user wants to create a new dungeon using an exciting game object</param>
        public void OutputDungeon(bool reUseGameObject)
        {
            // Make sure all tile sizes are the same and are inputted
            // If not exit the output since they will spawn incorrectly
            if (CheckTile() == false)
            {
                return;
            }

            // If the user deletes the previous dungeon so the  dungeonParent is now null
            // Check if there is any other dungeons to recreate on
            if (m_dungeonParent == null)
            {
                GeneratedDungeon otherDungeon = GameObject.FindObjectOfType <GeneratedDungeon>();

                if (otherDungeon != null)
                {
                    m_dungeonParent = otherDungeon.gameObject;
                }
            }

            // If the user wants to re-use the current dungeon game object
            // Go through the parent of the whole dungeon and remove the children
            // To get rid of the current dungeon tiles
            if (reUseGameObject && m_dungeonParent != null)
            {
                // Need to get the value before the loop since it changes each time a child is destroyed
                int numberOfChildren = m_dungeonParent.transform.childCount;

                for (int i = 0; i < numberOfChildren; i++)
                {
                    // Destroy at index 0 since every time a child is destroyed it moves the locations down
                    MonoBehaviour.DestroyImmediate(m_dungeonParent.transform.GetChild(0).gameObject);
                }
            }
            // If the user wants to create a new dungeon or if there is no current dungeon
            else if (reUseGameObject == false || m_dungeonParent == null)
            {
                m_dungeonParent = CreateParent("Dungeon", null);

                // Used to find the dungeon to delete when the delete button is pressed by the user
                m_dungeonParent.AddComponent <GeneratedDungeon>();
            }

            // Create the parent game objects to use in hierarchy to organise it
            m_dungeonRoomParent     = CreateParent("Rooms", m_dungeonParent.transform);
            m_dungeonCorridorParent = CreateParent("Corridors", m_dungeonParent.transform);
            m_dungeonWallParent     = CreateParent("Walls", m_dungeonParent.transform);
            m_dungeonRoofParent     = CreateParent("Roof", m_dungeonParent.transform);

            // Go through the gird and based on the number spawn the correct tile
            for (int x = 0; x < m_spawnGrid.GetLength(xAxis); x++)
            {
                for (int z = 0; z < m_spawnGrid.GetLength(yAxis); z++)
                {
                    switch (m_spawnGrid[x, z])
                    {
                    case roomGridNum:
                        SpawnFloorTile(m_floorTile, x, z, m_dungeonRoomParent.transform, m_dungeonRoofParent.transform);
                        break;

                    case corridorGridNum:
                        SpawnFloorTile(m_corridorTile, x, z, m_dungeonCorridorParent.transform, m_dungeonRoofParent.transform);
                        break;

                    case wallGridNum:
                        // Stack up the walls to make the rooms 3D
                        // Starts spawning at ySpawningPos + 1 so it is 1 tile above the floor
                        // Without the +1 it will spawn on the same level and the bottom title won't be seen
                        // Have to +1 the y dungeon size or else it will spawn 1 less block because of the +1 on the starting value
                        for (int y = ySpawningPos + spawnAboveTheFloor; y <= m_dungeonSizeYInt + spawnAboveTheFloor; y++)
                        {
                            GameObject.Instantiate(m_wallTile, new Vector3(x, y, z), m_wallTile.transform.rotation, m_dungeonWallParent.transform);
                        }
                        break;

                    default:
                        // For the empty tile number (0)
                        break;
                    }
                }
            }
        }