コード例 #1
0
ファイル: HexGrid.cs プロジェクト: sagarpatel2570/HexMap-PCG
    void GenerateHexMap()
    {
        List <HexCell> hexCellsList = new List <HexCell>();

        hexCellsList.Add(cells[0]);
        HexRoom hexRoom = new HexRoom(RegionType.NONE, hexRegions);

        rooms.Add(hexRoom);
        cells[0].initialize = true;
        cells[0].room       = hexRoom;
        hexRoom.AddCell(cells[0]);

        while (hexCellsList.Count > 0)
        {
            ProcessNextStep(hexCellsList);
        }

        List <int> roomIndexToRemove = null;

        if (removeSmallRegion)
        {
            roomIndexToRemove = new List <int> ();
            roomIndexToRemove = RemoveSmallRegions();
        }

        GenerateMeshForRooms(roomIndexToRemove);

        if (OnMapReady != null)
        {
            OnMapReady();
        }
    }
コード例 #2
0
ファイル: HexGrid.cs プロジェクト: sagarpatel2570/HexMap-PCG
    /// Maze Generation
    /// if we visit  cell for the first time  we will add it to our list and remove it form the last index (same as stack) once's it is fully initialize
    /// if it is visited first time we either create passage or door's
    /// if visited second time we check if it has same region type if so we create a passage also if both have different room we merge them
    /// else it has differnet region so we add wall
    /// if it doesn't have neighbour we simmple put walls
    ///
    void ProcessNextStep(List <HexCell> hexCellsList)
    {
        HexCell currentCell = hexCellsList[hexCellsList.Count - 1];

        if (currentCell.IsFullyInitialize == true)
        {
            hexCellsList.RemoveAt(hexCellsList.Count - 1);
            return;
        }
        /// we should avoid the direction which is already initialize with some type wall, door or passage so that
        /// we can make sure that all of it direction's are visited
        HexDirection randomDirection = currentCell.GetRandomUnVisitedDirection();
        HexCell      neighbour       = currentCell.GetNeighbor(randomDirection);

        if (neighbour != null)
        {
            if (!neighbour.initialize)
            {
                if (Random.value <= passagePercent)
                {
                    currentCell.SetPassage(randomDirection, EdgeType.PASSAGE, currentCell.room);
                    neighbour.SetPassage(randomDirection.Opposite(), EdgeType.PASSAGE, currentCell.room);
                    currentCell.room.AddCell(currentCell.GetNeighbor(randomDirection));
                }
                else
                {
                    HexRoom nextRoom = new HexRoom(currentCell.room.region.type, hexRegions);
                    currentCell.SetPassage(randomDirection, EdgeType.DOOR, currentCell.room);
                    neighbour.SetPassage(randomDirection.Opposite(), EdgeType.DOOR, nextRoom);

                    currentCell.room.AddNeighbour(nextRoom);
                    nextRoom.AddNeighbour(currentCell.room);

                    rooms.Add(nextRoom);
                    nextRoom.AddCell(neighbour);
                }
                hexCellsList.Add(neighbour);
            }
            else if (currentCell.room.region.type == neighbour.room.region.type)
            {
                currentCell.SetPassage(randomDirection, EdgeType.PASSAGE, currentCell.room);
                neighbour.SetPassage(randomDirection.Opposite(), EdgeType.PASSAGE, currentCell.room);
                if (currentCell.room != neighbour.room)
                {
                    rooms.Remove(neighbour.room);
                    /// add all the cell's neighbour rooms to current cell room
                    currentCell.room.AddNeighbourRoomFrom(neighbour.room);
                    /// also add all of it's cell
                    currentCell.room.AddCellFromRoom(neighbour.room);
                }
            }
            else
            {
                currentCell.SetPassage(randomDirection, EdgeType.WALL, null);
                neighbour.SetPassage(randomDirection.Opposite(), EdgeType.WALL, null);
            }
        }
        else
        {
            currentCell.SetPassage(randomDirection, EdgeType.WALL, null);
        }
    }