Пример #1
0
    public void CreateDenseMaze(csDungeon dungeon)
    {
        Vector2 currentLocation = dungeon.PickRandomCellAndFlagItAsVisited();

        csDungeonCell.DirectionType previousDirection = csDungeonCell.DirectionType.North;

        while (!dungeon.AllCellsAreVisited)
        {
            csDirectionPicker directionPicker = new csDirectionPicker();
            directionPicker.Constructor(previousDirection, changeDirectionModifier);
            csDungeonCell.DirectionType direction = directionPicker.GetNextDirection();

            while (!dungeon.HasAdjacentCellInDirection(currentLocation, direction) || dungeon.AdjacentCellInDirectionIsVisited(currentLocation, direction))
            {
                if (directionPicker.HasNextDirection)
                {
                    direction = directionPicker.GetNextDirection();
                }
                else
                {
                    currentLocation = dungeon.GetRandomVisitedCell(currentLocation);         // Get a new previously visited location
                    directionPicker = new csDirectionPicker();
                    directionPicker.Constructor(previousDirection, changeDirectionModifier); // Reset the direction picker
                    direction = directionPicker.GetNextDirection();                          // Get a new direction
                }
            }

            currentLocation = dungeon.CreateCorridor(currentLocation, direction);
            dungeon.FlagCellAsVisited(currentLocation);
            previousDirection = direction;
        }
    }
Пример #2
0
    public void RemoveDeadEnds(csDungeon dungeon)
    {
        foreach (Vector2 deadEndLocation in dungeon.DeadEndCellLocations)
        {
            if (ShouldRemoveDeadend())
            {
                Vector2 currentLocation = deadEndLocation;

                do
                {
                    // Initialize the direction picker not to select the dead-end corridor direction
                    csDirectionPicker directionPicker = new csDirectionPicker();
                    directionPicker.Constructor(dungeon[currentLocation].CalculateDeadEndCorridorDirection(), 100);
                    csDungeonCell.DirectionType direction = directionPicker.GetNextDirection();

                    while (!dungeon.HasAdjacentCellInDirection(currentLocation, direction))
                    {
                        if (directionPicker.HasNextDirection)
                        {
                            direction = directionPicker.GetNextDirection();
                        }
                        else
                        {
                            throw new InvalidOperationException("This should not happen");
                        }
                    }
                    // Create a corridor in the selected direction
                    currentLocation = dungeon.CreateCorridor(currentLocation, direction);
                } while (dungeon[currentLocation].IsDeadEnd);         // Stop when you intersect an existing corridor.
            }
        }
    }
Пример #3
0
    public void PlaceDoors(csDungeon dungeon)
    {
        foreach (csDungeonRoom room in dungeon.Rooms)
        {
            bool hasNorthDoor = false;
            bool hasSouthDoor = false;
            bool hasWestDoor  = false;
            bool hasEastDoor  = false;

            foreach (Vector2 cellLocation in room.CellLocations)
            {
                // Translate the room cell location to its location in the dungeon
                Vector2 dungeonLocation = new Vector2(room.Bounds.x + cellLocation.x, room.Bounds.y + cellLocation.y);

                // Check if we are on the west boundary of our room
                // and if there is a corridor to the west
                if ((cellLocation.x == 0) &&
                    (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.West)) &&
                    (!hasWestDoor))
                {
                    dungeon.CreateDoor(dungeonLocation, csDungeonCell.DirectionType.West);
                    hasWestDoor = true;
                }

                // Check if we are on the east boundary of our room
                // and if there is a corridor to the east
                if ((cellLocation.x == room.Width - 1) &&
                    (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.East)) &&
                    (!hasEastDoor))
                {
                    dungeon.CreateDoor(dungeonLocation, csDungeonCell.DirectionType.East);
                    hasEastDoor = true;
                }

                // Check if we are on the north boundary of our room
                // and if there is a corridor to the north
                if ((cellLocation.y == 0) &&
                    (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.North)) &&
                    (!hasNorthDoor))
                {
                    dungeon.CreateDoor(dungeonLocation, csDungeonCell.DirectionType.North);
                    hasNorthDoor = true;
                }


                // Check if we are on the south boundary of our room
                // and if there is a corridor to the south
                if ((cellLocation.y == room.Height - 1) &&
                    (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.South)) &&
                    (!hasSouthDoor))
                {
                    dungeon.CreateDoor(dungeonLocation, csDungeonCell.DirectionType.South);
                    hasSouthDoor = true;
                }
            }
        }
    }
Пример #4
0
    public int CalculateRoomPlacementScore(Vector2 location, csDungeonRoom room, csDungeon dungeon)
    {
        // Check if the room at the given location will fit inside the bounds of the map

        //if (dungeon.Bounds.Contains(new Rect(location.x, location.y, (float)room.Width + 1, (float)room.Height + 1)))
        if (dungeon.Bounds.Contains(location) &&
            dungeon.Bounds.Contains(new Vector2(location.x + room.Width + 1, location.y + room.Height + 1)))
        {
            int roomPlacementScore = 0;

            // Loop for each cell in the room
            foreach (Vector2 roomLocation in room.CellLocations)
            {
                // Translate the room cell location to its location in the dungeon
                Vector2 dungeonLocation = new Vector2(location.x + roomLocation.x, location.y + roomLocation.y);

                // Add 1 point for each adjacent corridor to the cell
                if (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.North))
                {
                    roomPlacementScore++;
                }
                if (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.South))
                {
                    roomPlacementScore++;
                }
                if (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.West))
                {
                    roomPlacementScore++;
                }
                if (dungeon.AdjacentCellInDirectionIsCorridor(dungeonLocation, csDungeonCell.DirectionType.East))
                {
                    roomPlacementScore++;
                }

                // Add 3 points if the cell overlaps an existing corridor
                if (dungeon[dungeonLocation].IsCorridor)
                {
                    roomPlacementScore += 3;
                }

                // Do not allow rooms to overlap!
                foreach (csDungeonRoom dungeonRoom in dungeon.Rooms)
                {
                    if (dungeonRoom.Bounds.Contains(dungeonLocation))
                    {
                        return(0);
                    }
                }
            }

            return(roomPlacementScore);
        }
        else
        {
            return(0);
        }
    }
Пример #5
0
    public csDungeon Generate()
    {
        csDungeon dungeon = new csDungeon();

        dungeon.Constructor(width, height);
        dungeon.FlagAllCellsAsUnvisited();

        CreateDenseMaze(dungeon);
        SparsifyMaze(dungeon);
        RemoveDeadEnds(dungeon);
        roomGenerator.PlaceRooms(dungeon);
        roomGenerator.PlaceDoors(dungeon);

        return(dungeon);
    }
Пример #6
0
    public void PlaceRooms(csDungeon dungeon)
    {
        int roomsPlaced = 0;

        for (int roomCounter = 0; roomCounter < noOfRoomsToPlace; roomCounter++)
        {
            csDungeonRoom room = null;

            // Ensure that at least half the rooms placed are small rooms to avoid "empty" dungeons
            if (maxRoomWidth > 1 || maxRoomHeight > 1)
            {
                if (roomCounter < noOfRoomsToPlace / 2)
                {
                    room = CreateRoom(true);
                }
                else
                {
                    room = CreateRoom(false);
                }
            }
            else
            {
                room = CreateRoom(false);
            }

            int     bestRoomPlacementScore    = 0;              // int.MaxValue;
            Vector2?bestRoomPlacementLocation = null;

            foreach (Vector2 currentRoomPlacementLocation in dungeon.CorridorCellLocations)
            {
                int currentRoomPlacementScore = CalculateRoomPlacementScore(currentRoomPlacementLocation, room, dungeon);

                if (currentRoomPlacementScore > bestRoomPlacementScore)
                {
                    bestRoomPlacementScore    = currentRoomPlacementScore;
                    bestRoomPlacementLocation = currentRoomPlacementLocation;
                }
            }

            // Create room at best room placement cell
            if (bestRoomPlacementLocation != null)
            {
                PlaceRoom(bestRoomPlacementLocation.Value, room, dungeon);
                roomsPlaced++;
            }
        }
    }
Пример #7
0
    public void SparsifyMaze(csDungeon dungeon)
    {
        // Calculate the number of cells to remove as a percentage of the total number of cells in the dungeon
        int noOfDeadEndCellsToRemove = (int)Math.Ceiling(((double)sparsenessModifier / 100) * (dungeon.Width * dungeon.Height));

        IEnumerator <Vector2> enumerator = dungeon.DeadEndCellLocations.GetEnumerator();

        for (int i = 0; i < noOfDeadEndCellsToRemove; i++)
        {
            if (!enumerator.MoveNext())                                    // Check if there is another item in our enumerator
            {
                enumerator = dungeon.DeadEndCellLocations.GetEnumerator(); // Get a new enumerator
                if (!enumerator.MoveNext())
                {
                    break;                                 // No new items exist so break out of loop
                }
            }

            Vector2 point = enumerator.Current;
            dungeon.CreateWall(point, dungeon[point].CalculateDeadEndCorridorDirection());
            dungeon[point].IsCorridor = false;
        }
    }
Пример #8
0
    public void PlaceRoom(Vector2 location, csDungeonRoom room, csDungeon dungeon)
    {
        // Offset the room origin to the new location
        room.SetLocation(location);

        // Loop for each cell in the room
        foreach (Vector2 roomLocation in room.CellLocations)
        {
            // Translate the room cell location to its location in the dungeon
            Vector2 dungeonLocation = new Vector2(location.x + roomLocation.x, location.y + roomLocation.y);
            dungeon[dungeonLocation].NorthSide = room[roomLocation].NorthSide;
            dungeon[dungeonLocation].SouthSide = room[roomLocation].SouthSide;
            dungeon[dungeonLocation].WestSide  = room[roomLocation].WestSide;
            dungeon[dungeonLocation].EastSide  = room[roomLocation].EastSide;

            // Create room walls on map (either side of the wall)
            if ((roomLocation.x == 0) && (dungeon.HasAdjacentCellInDirection(dungeonLocation, csDungeonCell.DirectionType.West)))
            {
                dungeon.CreateWall(dungeonLocation, csDungeonCell.DirectionType.West);
            }
            if ((roomLocation.x == room.Width - 1) && (dungeon.HasAdjacentCellInDirection(dungeonLocation, csDungeonCell.DirectionType.East)))
            {
                dungeon.CreateWall(dungeonLocation, csDungeonCell.DirectionType.East);
            }
            if ((roomLocation.y == 0) && (dungeon.HasAdjacentCellInDirection(dungeonLocation, csDungeonCell.DirectionType.North)))
            {
                dungeon.CreateWall(dungeonLocation, csDungeonCell.DirectionType.North);
            }
            if ((roomLocation.y == room.Height - 1) && (dungeon.HasAdjacentCellInDirection(dungeonLocation, csDungeonCell.DirectionType.South)))
            {
                dungeon.CreateWall(dungeonLocation, csDungeonCell.DirectionType.South);
            }
        }

        dungeon.AddRoom(room);
    }
Пример #9
0
    public static int[, ] ExpandToTiles(csDungeon dungeon)
    {
        // Instantiate our tile array
        int[, ] tiles = new int[dungeon.Width * 2 + 2, dungeon.Height * 2 + 2];

        // Initialize the tile array to void (empty)
        for (int x = 0; x < dungeon.Width * 2 + 2; x++)
        {
            for (int y = 0; y < dungeon.Height * 2 + 2; y++)
            {
                tiles[x, y] = (int)csDungeonCell.TileType.Rock;
            }
        }

        // Loop for each corridor cell and expand it
        foreach (Vector2 cellLocation in dungeon.CorridorCellLocations)
        {
            Vector2 tileLocation = new Vector2(cellLocation.x * 2 + 1, cellLocation.y * 2 + 1);
            tiles[(int)tileLocation.x, (int)tileLocation.y] = (int)csDungeonCell.TileType.Corridor;

            if (dungeon[cellLocation].NorthSide == csDungeonCell.SideType.Empty)
            {
                tiles[(int)tileLocation.x, (int)tileLocation.y - 1] = (int)csDungeonCell.TileType.Corridor;
            }
            if (dungeon[cellLocation].NorthSide == csDungeonCell.SideType.Door)
            {
                tiles[(int)tileLocation.x, (int)tileLocation.y - 1] = (int)csDungeonCell.TileType.DoorNS;
            }
            if (dungeon[cellLocation].NorthSide == csDungeonCell.SideType.Wall)
            {
                tiles[(int)tileLocation.x, (int)tileLocation.y - 1] = (int)csDungeonCell.TileType.Rock;
            }

            if (dungeon[cellLocation].SouthSide == csDungeonCell.SideType.Empty)
            {
                tiles[(int)tileLocation.x, (int)tileLocation.y + 1] = (int)csDungeonCell.TileType.Corridor;
            }
            if (dungeon[cellLocation].SouthSide == csDungeonCell.SideType.Door)
            {
                tiles[(int)tileLocation.x, (int)tileLocation.y + 1] = (int)csDungeonCell.TileType.DoorNS;
            }
            if (dungeon[cellLocation].SouthSide == csDungeonCell.SideType.Wall)
            {
                tiles[(int)tileLocation.x, (int)tileLocation.y + 1] = (int)csDungeonCell.TileType.Rock;
            }

            if (dungeon[cellLocation].WestSide == csDungeonCell.SideType.Empty)
            {
                tiles[(int)tileLocation.x - 1, (int)tileLocation.y] = (int)csDungeonCell.TileType.Corridor;
            }
            if (dungeon[cellLocation].WestSide == csDungeonCell.SideType.Door)
            {
                tiles[(int)tileLocation.x - 1, (int)tileLocation.y] = (int)csDungeonCell.TileType.DoorEW;
            }
            if (dungeon[cellLocation].WestSide == csDungeonCell.SideType.Wall)
            {
                tiles[(int)tileLocation.x - 1, (int)tileLocation.y] = (int)csDungeonCell.TileType.Rock;
            }

            if (dungeon[cellLocation].EastSide == csDungeonCell.SideType.Empty)
            {
                tiles[(int)tileLocation.x + 1, (int)tileLocation.y] = (int)csDungeonCell.TileType.Corridor;
            }
            if (dungeon[cellLocation].EastSide == csDungeonCell.SideType.Door)
            {
                tiles[(int)tileLocation.x + 1, (int)tileLocation.y] = (int)csDungeonCell.TileType.DoorEW;
            }
            if (dungeon[cellLocation].EastSide == csDungeonCell.SideType.Wall)
            {
                tiles[(int)tileLocation.x + 1, (int)tileLocation.y] = (int)csDungeonCell.TileType.Rock;
            }
        }

        // Fill tiles with corridor values for each room in dungeon
        foreach (csDungeonRoom room in dungeon.Rooms)
        {
            // Get the room min and max location in tile coordinates
            Vector2 minPoint = new Vector2(room.Bounds.xMin * 2 + 1, room.Bounds.yMin * 2 + 1);
            Vector2 maxPoint = new Vector2(room.Bounds.xMax * 2, room.Bounds.yMax * 2);

            // Fill the room in tile space with an empty value
            for (int i = (int)minPoint.x; i < (int)maxPoint.x; i++)
            {
                for (int j = (int)minPoint.y; j < (int)maxPoint.y; j++)
                {
                    tiles[i, j] = (int)csDungeonCell.TileType.Room;
                }
            }
        }

        // Remove unnecessary rock
        List <Vector2> rocksToRemove = new List <Vector2>();

        for (int x = 2; x < dungeon.Width * 2; x++)
        {
            for (int y = 2; y < dungeon.Height * 2; y++)
            {
                if (tiles[x, y] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x - 1, y - 1] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x, y - 1] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x + 1, y - 1] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x - 1, y] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x + 1, y] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x - 1, y + 1] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x, y + 1] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x + 1, y + 1] == (int)csDungeonCell.TileType.Rock &&

                    // tiles[x-2, y-2] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x - 1, y - 2] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x, y - 2] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x + 1, y - 2] == (int)csDungeonCell.TileType.Rock &&
                    // tiles[x+2, y-2] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x + 2, y - 1] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x + 2, y] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x + 2, y + 1] == (int)csDungeonCell.TileType.Rock &&
                    // tiles[x+2, y+2] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x + 1, y + 2] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x, y + 2] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x - 1, y + 2] == (int)csDungeonCell.TileType.Rock &&
                    // tiles[x-2, y+2] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x - 2, y + 1] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x - 2, y] == (int)csDungeonCell.TileType.Rock &&
                    tiles[x - 2, y - 1] == (int)csDungeonCell.TileType.Rock)

                {
                    rocksToRemove.Add(new Vector2(x, y));
                }
            }
        }

        foreach (Vector2 v in rocksToRemove)
        {
            tiles[(int)v.x, (int)v.y] = (int)csDungeonCell.TileType.Void;
        }

        for (int x = 0; x < dungeon.Width * 2 + 1; x++)
        {
            if (tiles[x, 0] == (int)csDungeonCell.TileType.Rock && tiles[x, 1] == (int)csDungeonCell.TileType.Rock)
            {
                tiles[x, 0] = (int)csDungeonCell.TileType.Void;
            }
            if (tiles[x, 1] == (int)csDungeonCell.TileType.Rock && tiles[x, 2] == (int)csDungeonCell.TileType.Void)
            {
                tiles[x, 1] = (int)csDungeonCell.TileType.Void;
            }
            if (tiles[x, dungeon.Height * 2] == (int)csDungeonCell.TileType.Rock && tiles[x, dungeon.Height * 2 - 1] == (int)csDungeonCell.TileType.Void)
            {
                tiles[x, dungeon.Height * 2] = (int)csDungeonCell.TileType.Void;
            }
        }

        for (int y = 0; y < dungeon.Height * 2 + 1; y++)
        {
            if (tiles[0, y] == (int)csDungeonCell.TileType.Rock && tiles[1, y] == (int)csDungeonCell.TileType.Rock)
            {
                tiles[0, y] = (int)csDungeonCell.TileType.Void;
            }
            if (tiles[1, y] == (int)csDungeonCell.TileType.Rock && tiles[2, y] == (int)csDungeonCell.TileType.Void)
            {
                tiles[1, y] = (int)csDungeonCell.TileType.Void;
            }
            if (tiles[dungeon.Width * 2, y] == (int)csDungeonCell.TileType.Rock && tiles[dungeon.Height * 2 - 1, y] == (int)csDungeonCell.TileType.Void)
            {
                tiles[dungeon.Height * 2, y] = (int)csDungeonCell.TileType.Void;
            }
        }

        return(tiles);
    }
Пример #10
0
    public void CreateDungeon()
    {
        if (floor == 4 && !generateBossLevel)
        {
            return;
        }

        loadingScreen.SetActive(true);

        DestroyDungeon();

        if (generateBossLevel)
        {
            audioPlayer.GetComponent <AudioPlayer>().StopPlayingMusic();

            bossLevel.SetActive(true);
            if (!playerObject)
            {
                playerObject = Instantiate(playerPrefab, bossLevel.transform.FindChild("PlayerSpawn").position, Quaternion.identity) as GameObject;
            }
            playerObject.transform.position             = bossLevel.transform.FindChild("PlayerSpawn").position;
            playerObject.GetComponent <PlayerAI>().dest = playerObject.transform.position;

            GameObject boss = Instantiate(bossPrefab, bossLevel.transform.FindChild("BossSpawn").position, Quaternion.identity) as GameObject;

            if (InitializeGrid)
            {
                // Disable in case we have already made a grid.
                gameWorld.GetComponent <GridComponent>().Disable(100);

                // Initialize grid.
                gameWorld.GetComponent <GridComponent>().Initialize(100, (ignored) => OnGridWasInitialized());
            }

            generateBossLevel = false;
            return;
        }

        if (floor >= 1)
        {
            foreach (GameObject monster in hardMonsterObjects)
            {
                if (!monsterObjects.Contains(monster))
                {
                    monsterObjects.Add(monster);
                }
            }
        }

        csDungeonRoomGenerator rG
            = new csDungeonRoomGenerator();

        int roomSizeMin = 2;
        int roomSizeMax = 2;

        switch (m_dungeonRoomSize)
        {
        case enDungeonRoomSize.Small:
            roomSizeMin = 2;
            roomSizeMax = 2;
            break;

        case enDungeonRoomSize.Medium:
            roomSizeMin = 2;
            roomSizeMax = 3;
            break;

        case enDungeonRoomSize.Large:
            roomSizeMin = 2;
            roomSizeMax = 4;
            break;
        }

        rG.Constructor
            (m_maxRoomCount,
            roomSizeMin,
            roomSizeMax,
            roomSizeMin,
            roomSizeMax);

        csDungeonGenerator dG
            = new csDungeonGenerator();

        dG.Constructor
            ((int)m_dungeonSize,
            (int)m_dungeonSize,
            (int)m_twists,
            (int)m_corridors,
            (int)m_deadEnds,
            rG);

        csDungeon dungeon
            = dG.Generate();

        int[,] tiles
            = csDungeonGenerator.ExpandToTiles
                  (dungeon);

        if (this.floorPrefab && this.wallsPrefab && this.doorsPrefab)
        {
            for (int k = 0; k < tiles.GetUpperBound(0); k++)
            {
                for (int l = 0; l < tiles.GetUpperBound(1); l++)
                {
                    float x = k * cellSpacingFactor;
                    float y = l * cellSpacingFactor;

                    if (tiles[k, l] != (int)csDungeonCell.TileType.Rock &&
                        tiles[k, l] != (int)csDungeonCell.TileType.Void &&
                        tiles[k, l] != (int)csDungeonCell.TileType.OuterRock)
                    {
                        GameObject tile
                            = (GameObject)Instantiate
                                  (this.floorPrefab, new Vector3(x, this.floorHeightOffset, y), Quaternion.identity);

                        tile.transform.parent
                            = this.transform;

                        if (this.GenerateCeiling)
                        {
                            tile = (GameObject)Instantiate
                                       (this.floorPrefab, new Vector3(x, this.roofsHeightOffset, y), Quaternion.identity);

                            tile.transform.parent
                                = this.transform;
                        }
                    }

                    if (tiles[k, l] == (int)csDungeonCell.TileType.Rock)
                    {
                        GameObject tile
                            = (GameObject)Instantiate
                                  (this.wallsPrefab, new Vector3(x, this.wallsHeightOffset, y), Quaternion.identity);

                        tile.transform.parent
                            = this.transform;
                    }

                    if (tiles[k, l] == (int)csDungeonCell.TileType.OuterRock)
                    {
                        GameObject tile
                            = (GameObject)Instantiate
                                  (this.wallsPrefab, new Vector3(x, this.wallsHeightOffset, y), Quaternion.identity);

                        tile.transform.parent
                            = this.transform;

                        Destroy(tile.GetComponent <BoxCollider>());
                    }

                    if (tiles[k, l] == (int)csDungeonCell.TileType.DoorNS)
                    {
                        GameObject tile
                            = (GameObject)Instantiate
                                  (this.doorsPrefab, new Vector3(x, this.doorsHeightOffset, y), Quaternion.identity);

                        tile.transform.parent
                            = this.transform;
                    }

                    if (tiles[k, l] == (int)csDungeonCell.TileType.DoorEW)
                    {
                        GameObject tile
                            = (GameObject)Instantiate
                                  (this.doorsPrefab, new Vector3(x, this.doorsHeightOffset, y), Quaternion.AngleAxis(90.0f, Vector3.up));

                        tile.transform.parent
                            = this.transform;
                    }
                }
            }

            // Spawn rare chest loot.
            SpawnLoot(tiles);

            if (InitializeGrid)
            {
                // Disable in case we have already made a grid.
                gameWorld.GetComponent <GridComponent>().Disable(100);

                // Initialize grid.
                gameWorld.GetComponent <GridComponent>().Initialize(100, (ignored) => OnGridWasInitialized(tiles));
            }
        }
    }
Пример #11
0
    public void CreateDungeon(int seed)
    {
        csRandom.Instance.Seed(seed);
        csDungeonRoomGenerator rG
            = new csDungeonRoomGenerator();

        int roomSizeMin = 2;
        int roomSizeMax = 2;

        switch (m_dungeonRoomSize)
        {
        case enDungeonRoomSize.Small:
            roomSizeMin = 2;
            roomSizeMax = 2;
            break;

        case enDungeonRoomSize.Medium:
            roomSizeMin = 2;
            roomSizeMax = 3;
            break;

        case enDungeonRoomSize.Large:
            roomSizeMin = 2;
            roomSizeMax = 4;
            break;
        }

        rG.Constructor
            (m_maxRoomCount,
            roomSizeMin,
            roomSizeMax,
            roomSizeMin,
            roomSizeMax);

        csDungeonGenerator dG
            = new csDungeonGenerator();

        dG.Constructor
            ((int)m_dungeonSize,
            (int)m_dungeonSize,
            (int)m_twists,
            (int)m_corridors,
            (int)m_deadEnds,
            rG);

        csDungeon dungeon
            = dG.Generate();

        int[,] tiles
            = csDungeonGenerator.ExpandToTiles
                  (dungeon);

        if (this.floorPrefab && this.wallsPrefab && this.doorsPrefab)
        {
            for (int k = 0; k < tiles.GetUpperBound(0); k++)
            {
                for (int l = 0; l < tiles.GetUpperBound(1); l++)
                {
                    float x = k * cellSpacingFactor;
                    float y = l * cellSpacingFactor;

                    if (tiles[k, l] != (int)csDungeonCell.TileType.Rock &&
                        tiles[k, l] != (int)csDungeonCell.TileType.Void)
                    {
                        GameObject tile
                            = (GameObject)Instantiate
                                  (this.floorPrefab, new Vector3(x, this.floorHeightOffset, y), Quaternion.identity);

                        tile.transform.parent
                            = this.transform;

                        if (this.GenerateCeiling)
                        {
                            tile = (GameObject)Instantiate
                                       (this.floorPrefab, new Vector3(x, this.roofsHeightOffset, y), Quaternion.identity);

                            tile.transform.parent
                                = this.transform;
                        }
                    }

                    if (tiles[k, l] == (int)csDungeonCell.TileType.Rock)
                    {
                        GameObject tile
                            = (GameObject)Instantiate
                                  (this.wallsPrefab, new Vector3(x, this.wallsHeightOffset, y), Quaternion.identity);

                        tile.transform.parent
                            = this.transform;
                    }

                    if (tiles[k, l] == (int)csDungeonCell.TileType.DoorNS)
                    {
                        GameObject tile
                            = (GameObject)Instantiate
                                  (this.doorsPrefab, new Vector3(x, this.doorsHeightOffset, y), Quaternion.identity);

                        tile.transform.parent
                            = this.transform;
                    }

                    if (tiles[k, l] == (int)csDungeonCell.TileType.DoorEW)
                    {
                        GameObject tile
                            = (GameObject)Instantiate
                                  (this.doorsPrefab, new Vector3(x, this.doorsHeightOffset, y), Quaternion.AngleAxis(90.0f, Vector3.up));

                        tile.transform.parent
                            = this.transform;
                    }
                }
            }

            //Find a dead end to place the player in
            if (this.playerObject)
            {
                for (int k = 1; k < tiles.GetUpperBound(0) - 1; k++)
                {
                    for (int l = 1; l < tiles.GetUpperBound(1) - 1; l++)
                    {
                        if (tiles[k, l] == (int)csDungeonCell.TileType.Corridor)
                        {
                            int deadEndWeight = 0;

                            if (tiles[k - 1, l - 1] == (int)csDungeonCell.TileType.Rock)
                            {
                                deadEndWeight++;
                            }
                            if (tiles[k, l - 1] == (int)csDungeonCell.TileType.Rock)
                            {
                                deadEndWeight++;
                            }
                            if (tiles[k + 1, l - 1] == (int)csDungeonCell.TileType.Rock)
                            {
                                deadEndWeight++;
                            }
                            if (tiles[k - 1, l] == (int)csDungeonCell.TileType.Rock)
                            {
                                deadEndWeight++;
                            }
                            if (tiles[k + 1, l] == (int)csDungeonCell.TileType.Rock)
                            {
                                deadEndWeight++;
                            }
                            if (tiles[k - 1, l + 1] == (int)csDungeonCell.TileType.Rock)
                            {
                                deadEndWeight++;
                            }
                            if (tiles[k, l + 1] == (int)csDungeonCell.TileType.Rock)
                            {
                                deadEndWeight++;
                            }
                            if (tiles[k + 1, l + 1] == (int)csDungeonCell.TileType.Rock)
                            {
                                deadEndWeight++;
                            }

                            if (deadEndWeight == 7)
                            {
                                playerObject.transform.position = new Vector3(k * cellSpacingFactor, 1.0f, l * cellSpacingFactor);
                            }
                        }
                    }
                }
            }

            if (this.monsterObject)
            {
                bool itemGiven = false;

                for (int k = 1; k < tiles.GetUpperBound(0) - 1; k++)
                {
                    for (int l = 1; l < tiles.GetUpperBound(1) - 1; l++)
                    {
                        if (tiles[k, l] == (int)csDungeonCell.TileType.Room && monstersInRooms == true)
                        {
                            if (Random.Range(0, 100) < monsterAppearChance)
                            {
                                GameObject monster
                                    = (GameObject)
                                      Instantiate(monsterObject, new Vector3(k * cellSpacingFactor, 1.05f, l * cellSpacingFactor), Quaternion.identity);

                                monster.transform.parent = this.transform;

                                if (!itemGiven)
                                {
                                    monster.GetComponentInChildren <EnemyProps>().hasItem = true;  // Give this monster the item!!!
                                    itemGiven = true;
                                }
                            }
                        }

                        if (tiles[k, l] == (int)csDungeonCell.TileType.Corridor && monstersInCorridors == true)
                        {
                            if (Random.Range(0, 100) < monsterAppearChance)
                            {
                                GameObject monster
                                    = (GameObject)
                                      Instantiate(monsterObject, new Vector3(k * cellSpacingFactor, 1.05f, l * cellSpacingFactor), Quaternion.identity);

                                monster.transform.parent = this.transform;

                                if (!itemGiven)
                                {
                                    monster.GetComponentInChildren <EnemyProps>().hasItem = true;  // Give this monster the item!!!
                                    itemGiven = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }