Exemple #1
0
 //Recursive Method to Create Rooms in all the leaf SubLevels
 public void CreateRoom()
 {
     if (child1 != null)
     {
         child1.CreateRoom();
     }
     if (child2 != null)
     {
         child2.CreateRoom();
     }
     if (IsLeaf())
     {
         roomWidth  = (int)Random.Range(minRoomSize, width - 4);
         roomHeight = (int)Random.Range(minRoomSize, height - 4);
         if (roomWidth % 2 != 0)
         {
             roomWidth -= 1;
         }
         if (roomHeight % 2 != 0)
         {
             roomHeight -= 1;
         }
         room = new Room((int)xCentre, (int)zCentre, roomWidth, roomHeight, depth);
         if (pair != null && pair.room != null)
         {
             room.setPair(pair.room);
             pair.room.setPair(room);
         }
     }
 }
    private bool spawnFailed = true;                                        //if true when generation is 'finished', the level will be generated again

    //main method to generate the map
    void Start()
    {
        while (spawnFailed)           //while loop used until a generation meets all criteria (main one being that every room is actually connected!)
        {
            try{
                spawnFailed = false;    //will be set to true in fatal circumstances (no direct line between rooms, coridoors unable to generate etc)
                SubLevel rootLevel = new SubLevel(levelWidth / 2f, levelHeight / 2f, levelWidth, levelHeight, minRoomSize, true, 0);
                SplitLevel(rootLevel);  //recursive method to split the map into appropriately sized sections
                rootLevel.CreateRoom(); //recursive method to create rooms in the sections
                rootLevel.FindPairs();  //recursive method to find pairs of rooms
                rooms  = new List <Room> ();
                groups = new List <RoomGroup> ();
                coridoorControllers = new List <CoridoorController> ();
                GetRooms(rootLevel);           //returns all rooms
                SpawnRooms();                  //spawns in rooms (including generating stats)
                GroupRooms();                  //groups rooms together
                SpawnCoridoorsWithinGroups();  //spawns coridoors within each group
                SpawnCoridoorsBetweenGroups(); //connects each group with a coridoor
            } catch {
                spawnFailed = true;
            }
            if (spawnFailed)               //Resetting, before trying again

            {
                for (int i = 0; i < roomList.childCount; i++)
                {
                    Destroy(roomList.GetChild(i).gameObject);
                }
                for (int i = 0; i < coridoorList.childCount; i++)
                {
                    Destroy(coridoorList.GetChild(i).gameObject);
                }
                while (roomList.childCount > 0)
                {
                    roomList.GetChild(0).parent = null;
                }
                while (coridoorList.childCount > 0)
                {
                    coridoorList.GetChild(0).parent = null;
                }
                tileController.ResetTiles(levelWidth, levelHeight);
            }
        }

        BuildCoridoors(); //builds the physical aspect of the coridoors;
        BuildRooms();     //builds the physical aspect of the rooms
        BuildNavMesh();   //builds a navmesh, used for AI movement
    }