コード例 #1
0
        public void Apply(GeneratorParameter param, Tile[,] tiles)
        {
            if (param.forestSize <= 0f)
            {
                return;
            }

            float paramValue = 1 - param.forestSize;
            int   perc       = (int)(BLOCKING_PERC_MIN + (BLOCKING_PERC_MAX - BLOCKING_PERC_MIN) * paramValue);

            bool[,] automata = CellularAutomata.Generate(param.size, SMOOTH_ITERATIONS, perc, true, random.Next());

            for (int x = 0; x < param.size.X; x++)
            {
                for (int y = 0; y < param.size.Y; y++)
                {
                    Tile t = tiles[x, y];
                    if (!automata[x, y] && t.type == TileType.Nothing)
                    {
                        t.type       = TileType.Forest;
                        t.onTopIndex = param.tileset.GetRandomIndex(TileType.Forest, random);
                    }
                }
            }
        }
コード例 #2
0
        private void CreateStones(GeneratorParameter param, Tile[,] tiles)
        {
            float[,] heatMap = CreateStoneHeatMap(param.size, tiles);

            List <Room> rooms = null;

            while (rooms == null || rooms.Count < MIN_RES_ROOMS)
            {
                int block = (int)(BLOCKING_PERC - BLOCKING_PERC_DELTA / 2 + ((1 - param.resourceSize) * BLOCKING_PERC_DELTA));

                bool[,] automata = CellularAutomata.Generate(param.size, SMOOTH_ITERATIONS, block, false, random.Next());

                for (int y = 0; y < param.size.Y; y++)
                {
                    for (int x = 0; x < param.size.X; x++)
                    {
                        if (automata[x, y] || heatMap[x, y] < 0.5f || !tiles[x, y].AllHeightsAreSame())
                        {
                            automata[x, y] = true;
                        }
                    }
                }

                rooms = GeneratorHelper.GetCellularAutomataAsRooms(automata);
            }

            rooms.Shuffle(random);

            int coalIndex = (int)(STONE_PERCENTAGE * rooms.Count);
            int oreIndex  = (int)((STONE_PERCENTAGE + COAL_PERCENTAGE) * rooms.Count);
            int oilIndex  = (int)((STONE_PERCENTAGE + COAL_PERCENTAGE + ORE_PERCENTAGE) * rooms.Count);

            for (int i = 0; i < rooms.Count; ++i)
            {
                TileType res = TileType.Stone;
                if (i >= oilIndex)
                {
                    res = TileType.Oil;
                }
                else if (i >= oreIndex)
                {
                    res = TileType.Ore;
                }
                else if (i >= coalIndex)
                {
                    res = TileType.Coal;
                }

                foreach (Point p in rooms[i].Tiles)
                {
                    if (tiles[p.X, p.Y].type == TileType.Nothing)
                    {
                        tiles[p.X, p.Y].type       = res;
                        tiles[p.X, p.Y].onTopIndex = param.tileset.GetRandomIndex(res);
                    }
                }
            }
        }
コード例 #3
0
    /// <summary>
    /// Generates the DungeonLevel and writes it to the map 2d array
    /// </summary>
    private void GenerateDungeon()
    {
        DungeonMst    dungeonMst    = new DungeonMst();
        RoomGenerator roomGenerator = new RoomGenerator();

        rooms = roomGenerator.GenerateRooms(
            DungeonLevel.InitialRoomDensity,
            DungeonLevel.Width,
            DungeonLevel.Height,
            new Vector2Int(DungeonLevel.MinRoomHeight, DungeonLevel.MaxRoomHeight),
            new Vector2Int(DungeonLevel.MinRoomWidth, DungeonLevel.MaxRoomWidth)
            );
        links = dungeonMst.GetDungeonMap(rooms.Keys.ToList(), DungeonLevel.RoomConnectedness);
        LinksIntoHallways();
        dungeonExits = dungeonMst.dungeonExits;

        // Adds hubs
        foreach (var room in rooms.Values)
        {
            WriteRoomToMap(room);
        }

        foreach (var edge in hallways)
        {
            WriteEdgeToMap(edge);
        }

        CellularAutomata          caveGen   = new CellularAutomata(DungeonLevel);
        List <List <Vector2Int> > caveRooms = caveGen.Generate();

        foreach (var caveRoom in caveRooms)
        {
            foreach (var tile in caveRoom)
            {
                map[tile.x, tile.y] = Tiles.caveTile;
            }
        }

        // Place the physical objects into the world
        PlaceExits();
        AddFloorsAndWalls();
        PlaceDoors();
        foreach (WeightedGeneratedStructure type in DungeonLevel.generatedStructures)
        {
            PlaceGeneratedStructures(type.structure, type.amountPerLevel);
        }
        PlaceObjects(PlaceChest, DungeonLevel.ChestsPerLevel);
        PlaceObjects(PlaceDecorObjects, DungeonLevel.FreeStandingDecorationCount);
        PlaceObjects(PlaceEnemy, DungeonLevel.EnemiesPerLevel);
        PlaceObjects(PlaceDestrucibleObjects, DungeonLevel.DestructibleObjectCount);
    }
コード例 #4
0
    void Start()
    {
        bool[,] map = CellularAutomata.Generate(cellularAutomataArgs);
        Tilemap tilemap = FindObjectOfType <Tilemap>();

        for (int x = 0; x < map.GetLength(0); x++)
        {
            for (int y = 0; y < map.GetLength(1); y++)
            {
                if (map[x, y])
                {
                    Vector2Int pos = new Vector2Int(
                        x - cellularAutomataArgs.width / 2,
                        y - cellularAutomataArgs.height / 2);
                    tilemap.Instantiate(floorTile, pos);
                }
            }
        }
    }
コード例 #5
0
        public static List <Room> GetCellularAutomataAsRooms(int smoothIterations, int blockingTilePercentage, bool openCave)
        {
            bool[,] automata = CellularAutomata.Generate(Size, smoothIterations, blockingTilePercentage, openCave);

            return(GetCellularAutomataAsRooms(automata));
        }