コード例 #1
0
    private static void InstantiateDungeonPieces(ref GridMapFromPrefabsSettings settings)
    {
        bool[,] visitedCells = new bool[mapFromPrefabs.mapRows, mapFromPrefabs.mapColumns];
        int visitedCellsCount = 0;

        while (visitedCellsCount < Mathf.FloorToInt((mapFromPrefabs.mapRows - 2) * (mapFromPrefabs.mapColumns - 2) * 0.1f))
        {
            mapFromPrefabs.InitializeMap(ref settings);
            visitedCells      = mapFromPrefabs.WalkThroughMap();
            visitedCellsCount = GetVisitedCellsCount(ref visitedCells);
        }

        mapFromPrefabs.LogOutputMap();

        for (int r = 1; r < mapFromPrefabs.mapRows - 1; r++)
        {
            for (int c = 1; c < mapFromPrefabs.mapColumns - 1; c++)
            {
                char ch = mapFromPrefabs.map[r, c];

                if (!dungeonGameObjects.ContainsKey(ch) || !visitedCells[r, c])
                {
                    continue;
                }
                else
                {
                    GameObject dungeonGameObject = dungeonGameObjects[ch];
                    //Instantiate(dungeonGameObject, new Vector3(r * 3, 0, c * 3), dungeonGameObject.transform.rotation);
                }
            }
        }
    }
コード例 #2
0
    private void TryToSpawnRooms(ref GridMapFromPrefabsSettings settings)
    {
        rooms = new List <Room>(settings.roomsCount);
        int roomPlacementCount = 0;
        int roomsPlaced        = 0;

        while (true)
        {
            bool isPlaced = CreateRoom(ref settings);
            if (!isPlaced)
            {
                roomPlacementCount++;
            }
            else
            {
                roomsPlaced++;
                roomPlacementCount = 0;
            }

            if (roomPlacementCount >= settings.roomPlacementTriesCount || roomsPlaced >= settings.roomsCount)
            {
                break;
            }
        }
    }
コード例 #3
0
    private static void ConvertPrefabMapToCellMap(ref Cell[,] map, ref GridMapFromPrefabsSettings settings, CellType roomWallTile = CellType.Wall, CellType roomFloorTile = CellType.Floor)
    {
        for (int y = 1; y < mapFromPrefabs.mapColumns - 1; y++)
        {
            for (int x = 1; x < mapFromPrefabs.mapRows - 1; x++)
            //for (int x = 1; x < mapFromPrefabs.mapRows - 1; x++)
            {
                int mapX = (x - 1) * settings.prefabWidthInTiles;
                int mapY = (y - 1) * settings.prefabHeightInTiles;

                Cell[,] prefabCell = PrefabToCell(ref mapFromPrefabs.map[x, y], roomWallTile, roomFloorTile);

                string str = "";

                for (int yy = mapY; yy < mapY + settings.prefabWidthInTiles; yy++)
                {
                    for (int xx = mapX; xx < mapX + settings.prefabHeightInTiles; xx++)
                    {
                        map[xx, yy] = prefabCell[xx - mapX, yy - mapY];
                        str        += map[xx, yy].cellType;
                    }
                    str += "\n";
                }
                Debug.Log(str);
            }
        }
    }
コード例 #4
0
    public void InitializeMap(ref GridMapFromPrefabsSettings settings)
    {
        InitializeCharacters();
        settings.prefabCharacters = prefabCharacters;

        mapRows    = settings.gridWidth + 2;
        mapColumns = settings.gridHeight + 2;

        map = new char[mapRows, mapColumns];

        // Put 'X' (walls) in top and bottom rows.
        for (int c = 0; c < mapColumns; c++)
        {
            map[0, c]           = 'X';
            map[mapRows - 1, c] = 'X';
        }

        // Put 'X' (walls) in the left and right columns.
        for (int r = 0; r < mapRows; r++)
        {
            map[r, 0] = 'X';
            map[r, mapColumns - 1] = 'X';
        }

        // Set 'O' (empty) for the other map cells.
        for (int r = 1; r < mapRows - 1; r++)
        {
            for (int c = 1; c < mapColumns - 1; c++)
            {
                map[r, c] = 'O';
            }
        }

        TryToSpawnRooms(ref settings);

        for (int c = 1; c < mapColumns - 1; c++)
        {
            for (int r = 1; r < mapRows - 1; r++)
            {
                if (
                    map[r, c] == '╒' || map[r, c] == '╕' || map[r, c] == '╘' || map[r, c] == '╛' ||
                    map[r, c] == '╔' || map[r, c] == '╗' || map[r, c] == '╚' || map[r, c] == '╝' ||
                    map[r, c] == '╓' || map[r, c] == '╖' || map[r, c] == '╙' || map[r, c] == '╜' ||
                    map[r, c] == '╧' || map[r, c] == '╤' || map[r, c] == '╟' || map[r, c] == '╢' ||
                    map[r, c] == '╩' || map[r, c] == '╦' || map[r, c] == '╠' || map[r, c] == '╣' || map[r, c] == '▓'
                    )
                {
                    continue;
                }

                string validCharacters = GetValidBoxCharacters(r, c);
                map[r, c] = validCharacters[Random.Range(0, validCharacters.Length)];
            }
        }

        PlaceDeadEnds();
    }
コード例 #5
0
    // Use this for initialization
    private static void InitializeAndGenerateMap(ref GridMapFromPrefabsSettings settings)
    {
        mapFromPrefabs = new MapFromPrefabs();
        mapFromPrefabs.InitializeMap(ref settings);
        LoadDungeonCharacterMappings(ref settings);

        int visitedCellsCount = 0;

        while (visitedCellsCount > Mathf.FloorToInt((mapFromPrefabs.mapRows - 2) * (mapFromPrefabs.mapColumns - 2) * settings.procentOfTilesThatAreWalkable))
        {
            mapFromPrefabs.InitializeMap(ref settings);
            var visitedCells = mapFromPrefabs.WalkThroughMap();
            visitedCellsCount = GetVisitedCellsCount(ref visitedCells);
        }

        mapFromPrefabs.LogOutputMap();
        //InstantiateDungeonPieces(ref settings);
    }
コード例 #6
0
    public static TileMapSettings PreprocessMap(TileMapSettings mapSettings, GridMapFromPrefabsSettings settings)
    {
        int maxRoomsCount = settings.gridWidth * settings.gridHeight;

        if (settings.gridWidth < 2)
        {
            settings.gridWidth = 2;
        }
        if (settings.gridHeight < 2)
        {
            settings.gridHeight = 2;
        }

        mapSettings.mapWidth  = settings.gridWidth * settings.prefabWidthInTiles;
        mapSettings.mapHeight = settings.gridHeight * settings.prefabHeightInTiles;

        return(mapSettings);
    }
コード例 #7
0
    private static void LoadDungeonCharacterMappings(ref GridMapFromPrefabsSettings settings)
    {
        //int i = 0;
        //if (settings.dungeonShapes.Count != settings.prefabCharacters.Length)
        //{
        //    Debug.LogError("Prefab shape must be set for each character if prefab characters. And wise versa.");
        //}

        for (int j = 0; j < settings.prefabCharacters.Length; j++)
        {
            char character = settings.prefabCharacters[j];
            int  count     = 0;

            for (int k = 0; k < settings.prefabCharacters.Length; k++)
            {
                if (character == settings.prefabCharacters[k])
                {
                    count++;
                }
                if (count > 1)
                {
                    break;
                }
            }
            if (count > 1)
            {
                Debug.LogError("Characters can not appear twice in prefab characters string.");
            }
        }

        /// TODO
        /// Check if prefabs are in dungeonShapes twice. Maybe it is possible to check only inside Editor (using PrefabUtility)

        //dungeonGameObjects = new Dictionary<char, GameObject>();
        //// Create dictionary
        //foreach (GameObject dungeonShape in settings.dungeonShapes)
        //{
        //    dungeonGameObjects.Add(settings.prefabCharacters[i], dungeonShape);
        //    i++;
        //}
    }
コード例 #8
0
    public static EmptyGrid ProcessMap(EmptyGrid map, GridMapFromPrefabsSettings settings)
    {
        //int mapSeed = 925;
        //int mapSeed = 573;
        Random.State initialState = Random.state;
        if (settings.useFixedSeed)
        {
            Random.InitState(settings.seed.GetHashCode());
        }
        else
        {
            Random.InitState(Time.time.ToString().GetHashCode());
        }

        InitializeAndGenerateMap(ref settings);

        ConvertPrefabMapToCellMap(ref map.values, ref settings);

        Random.state = initialState;

        return(map);
    }
コード例 #9
0
    private bool CreateRoom(ref GridMapFromPrefabsSettings settings)
    {
        int roomWidthSize = Random.Range(settings.roomMinWidth, settings.roomMaxWidth);
        //int roomWidthSize = Random.Range(2, 5);
        int roomHeightSize = Random.Range(settings.roomMinHeight, settings.roomMaxHeight);
        //int roomHeightSize = Random.Range(2, 5);

        int startRow    = Random.Range(2, mapRows - 1 - roomWidthSize);
        int startColumn = Random.Range(2, mapColumns - 1 - roomHeightSize);

        int roomWidth  = roomWidthSize - 1;
        int roomHeight = roomHeightSize - 1;

        Room room          = new Room(startRow, startColumn, roomWidth, roomHeight);
        bool isOverlapping = false;

        for (int i = 0; i < rooms.Count; i++)
        {
            if (rooms[i].IsOverlapping(room))
            {
                isOverlapping = true;
                break;
            }
        }
        if (isOverlapping)
        {
            return(false);
        }
        else
        {
            rooms.Add(room);
        }

        float randomEntranceCreationChance = settings.randomEntranceCreationChance;

        // Corners
        map[startRow, startColumn]                          = '╚';
        map[startRow + roomWidth, startColumn]              = '╝';
        map[startRow, startColumn + roomHeight]             = '╔';
        map[startRow + roomWidth, startColumn + roomHeight] = '╗';

        // Top and Bottom
        for (int i = 1; i < roomWidth; i++)
        {
            map[startRow + i, startColumn + roomHeight] = '╦';
            map[startRow + i, startColumn] = '╩';
        }

        // Left and Right
        for (int i = 1; i < roomHeight; i++)
        {
            map[startRow, startColumn + i]             = '╠';
            map[startRow + roomWidth, startColumn + i] = '╣';
        }

        // Set '▓' for the other spaces (which means 'floor').
        for (int i = 1; i < roomHeight; i++)
        {
            for (int j = 1; j < roomWidth; j++)
            {
                map[startRow + j, startColumn + i] = '▓';
            }
        }

        for (int i = 0; i < settings.maxRandomEntrances; i++)
        {
            if (Random.value < randomEntranceCreationChance)
            {
                randomEntranceCreationChance -= settings.randomEntranceCreationChanceSubtrahend;
                continue;
            }

            int entranceSide = Random.Range(0, 4);

            if (entranceSide % 2 == 0) //even = Top Bottom
            {
                int entranceColumn = Random.Range(0, roomWidthSize);
                if (entranceSide == 0) //Top
                {
                    if (entranceColumn == 0)
                    {
                        map[startRow + entranceColumn, startColumn + roomHeight] = '╓';
                    }
                    else if (entranceColumn == roomWidth)
                    {
                        map[startRow + entranceColumn, startColumn + roomHeight] = '╖';
                    }
                    else
                    {
                        map[startRow + entranceColumn, startColumn + roomHeight] = '╧';
                    }
                }
                else //2
                {
                    if (entranceColumn == 0)
                    {
                        map[startRow + entranceColumn, startColumn] = '╙';
                    }
                    else if (entranceColumn == roomWidth)
                    {
                        map[startRow + entranceColumn, startColumn] = '╜';
                    }
                    else
                    {
                        map[startRow + entranceColumn, startColumn] = '╤';
                    }
                }
            }
            else
            {
                int entranceRow = Random.Range(0, roomHeightSize);
                if (entranceSide == 1) //Right
                {
                    if (entranceRow == 0)
                    {
                        map[startRow + roomWidth, startColumn + entranceRow] = '╛';
                    }
                    else if (entranceRow == roomHeight)
                    {
                        map[startRow + roomWidth, startColumn + entranceRow] = '╕';
                    }
                    else
                    {
                        map[startRow + roomWidth, startColumn + entranceRow] = '╟';
                    }
                }
                else //3
                {
                    if (entranceRow == 0)
                    {
                        map[startRow, startColumn + entranceRow] = '╘';
                    }
                    else if (entranceRow == roomHeight)
                    {
                        map[startRow, startColumn + entranceRow] = '╒';
                    }
                    else
                    {
                        map[startRow, startColumn + entranceRow] = '╢';
                    }
                }
            }
        }

        return(true);
    }