예제 #1
0
 /// <summary>
 /// initzilzes a new tile map manager
 /// </summary>
 /// <param name="settings">Generator settings for the tile map generator</param>
 /// <param name="spreads">Areas that should be placed during the map generation</param>
 public TileMapManager(GeneratorSettings settings, AreaSpread[] spreads)
 {
     this.settings = settings;
     this.spreads  = spreads;
     maps          = new List <StreamedTileMap>();
     generator     = new TileMapGenerator();
 }
예제 #2
0
        /// <summary>
        /// Calculates the map size in tiles
        /// </summary>
        /// <param name="settings">Settings which descibes the map</param>
        /// <returns>Returns the map size</returns>
        private int GetMapSize(GeneratorSettings settings)
        {
            int radiusInMeter = random.Next(settings.MinSizeInMeter, settings.MaxSizeInMeter);

            float u = 2f * Convert.ToSingle(Math.PI) * radiusInMeter;

            return((int)Math.Round(u / settings.MeterPerTile));
        }
예제 #3
0
        /// <summary>
        /// Changes to level to map that will be generated based on the current position
        /// </summary>
        /// <param name="settings">Setting to generate the new map</param>
        /// <param name="tileColumn">Tile column index</param>
        /// <param name="tileRow">Tile row index</param>
        /// <param name="disposeCurrentMap">Disposes the current level when true</param>
        public void Changelevel(GeneratorSettings settings, int tileColumn, int tileRow, bool disposeCurrentMap)
        {
            this.settings = settings;
            if (!disposeCurrentMap && currentLevel != null)
            {
                maps.Add(CurrentLevel);
            }
            generator = new TileMapGenerator();
            UnSub();
            currentLevel = generator.GenerateMap(settings, spreads, tileColumn, tileRow);
            Sub();

            GridChanged?.Invoke(this, new GridEventArgs(currentLevel.GridRow, currentLevel.GridColumn, -1, -1, false));
        }
예제 #4
0
        /// <summary>
        /// Generates an empty map based on the generator settings
        /// </summary>
        /// <param name="settings">Settings which descibes the map</param>
        /// <returns>Returns an empty map</returns>
        private Tile[] PopulateMap(GeneratorSettings settings)
        {
            if (settings.RadiusOfCylinder)
            {
                int radiusInMeter = random.Next(settings.MinSizeInMeter, settings.MaxSizeInMeter);

                float u    = 2f * Convert.ToSingle(Math.PI) * radiusInMeter;
                int   size = (int)Math.Round(u / settings.MeterPerTile);
                return(new Tile[size * size]);
            }
            else
            {
                int height = random.Next(settings.MinSizeInMeter, settings.MaxSizeInMeter + 1);
                int width  = random.Next(settings.MinSizeInMeter, settings.MaxSizeInMeter + 1);
                return(new Tile[height * width]);
            }
        }
예제 #5
0
        /// <summary>
        /// Generates a map based on the generator settings and area spreads
        /// </summary>
        /// <param name="settings">Generator settings to modify the generation process</param>
        /// <param name="area">Area that should be placed on the map</param>
        /// <returns>Returns the maps as a flatten byte array</returns>
        public Tile[] GenerateMap(GeneratorSettings settings, AreaSpread area)
        {
            if (settings.Seed >= 0)
            {
                random = new Random(settings.Seed);
            }
            else if (random == null)
            {
                throw new ArgumentOutOfRangeException();
            }

            Tile[] map         = PopulateMap(settings);
            int    rowCount    = map.GetUpperBound(0);
            int    columnCount = map.GetUpperBound(1);

            CreateLayerOne(map, rowCount, columnCount, settings.TileSize, settings.MeterPerTile, area, settings.RadiusOfCylinder);

            return(map);
        }
예제 #6
0
        /// <summary>
        /// Generates a map based on the given tile location, generator settings and area spreads
        /// </summary>
        /// <param name="settings">Generator settings to modify the generation process</param>
        /// <param name="areas">Areas that should be placed on the map</param>
        /// <param name="tileColumn">Tile column index</param>
        /// <param name="tileRow">Tile row index</param>
        /// <returns>Returns a StreamedTileMap</returns>
        public StreamedTileMap GenerateMap(GeneratorSettings settings, AreaSpread[] areas, int tileColumn, int tileRow)
        {
            if (settings.Seed > 0)
            {
                random = new Random(settings.Seed);
            }
            else if (random == null)
            {
                throw new ArgumentOutOfRangeException();
            }

            this.settings = settings;
            spreads       = areas;

            double tilesPerGrid   = Math.Round(settings.MeterPerGrid / settings.MeterPerTile);
            int    tilesPerColumn = (int)tilesPerGrid;
            int    size           = tilesPerColumn * tilesPerColumn;
            int    gridsPerRow    = (int)Math.Ceiling(GetMapSize(settings) / tilesPerGrid);

            int gridColumn = (int)Math.Floor(tileColumn / tilesPerGrid);
            int gridRow    = (int)Math.Floor(tileRow / tilesPerGrid);

            Tile[][]            edgeOverrides = new Tile[9][];
            Tile[][]            maps          = new Tile[9][];
            List <ObjectTile>[] objectTiles   = new List <ObjectTile> [9];
            for (int i = 0; i < maps.Length; i++)
            {
                edgeOverrides[i] = new Tile[size];
                maps[i]          = new Tile[size];
                objectTiles[i]   = new List <ObjectTile>();
            }

            int[] suroundingGrids = CreateSuroundings(maps.Length, gridColumn, gridRow, gridsPerRow);

            StreamedTileMap streamedTileMap = new StreamedTileMap(this, tileRow, tileColumn, gridsPerRow, gridsPerRow, tilesPerColumn, tilesPerColumn, settings.TileSize);

            //Create Map
            foreach (LayerType currentLayerType in Enum.GetValues(typeof(LayerType)).Cast <LayerType>().OrderBy(k => (int)k))
            {
                for (int i = 0; i < suroundingGrids.Length; i++)
                {
                    AreaSpread[] layerAreas = areas.Where(a => a.Layer == currentLayerType).ToArray();
                    random = new Random(suroundingGrids[i] * settings.Seed);
                    for (int j = 0; j < layerAreas.Length; j++)
                    {
                        switch (layerAreas[j].Layer)
                        {
                        case LayerType.Height:
                        {
                            if (layerAreas[j].MaxSizeInMeter <= settings.MeterPerGrid / 2f)
                            {
                                CreateLayerOne(i, maps, edgeOverrides, (int)tilesPerGrid, (int)tilesPerGrid, settings.TileSize, settings.MeterPerTile, layerAreas[j], settings.RadiusOfCylinder);
                            }
                            else
                            {
                                throw new Exception("AreaSpread MaxSize must be smaller then (MetersPerGrid / 2)!");
                            }
                            break;
                        }

                        case LayerType.Biome:
                            CreateBiomeLayer(i, maps, layerAreas[j]);
                            break;

                        case LayerType.Paths:
                            CreateLayerFour();
                            break;

                        case LayerType.PointsOfInterest:
                        {
                            objectTiles[i].AddRange(CreateObjectLayer(maps[i], layerAreas[j], tilesPerColumn, tilesPerColumn));
                        }
                        break;
                        }
                    }
                }

                if (currentLayerType == LayerType.Height)
                {
                    //DefragementMap and add edgenoise
                    DefragmentMaps(maps, areas, suroundingGrids, (int)tilesPerGrid, (int)tilesPerGrid);

                    //Merge edgeOverrides
                    for (int k = 0; k < maps.Length; k++)
                    {
                        maps[k] = TileMathHelper.MergeMaps(maps[k], edgeOverrides[k]);
                    }
                }
            }

            CreateTileMapParts(maps, objectTiles, suroundingGrids, gridsPerRow, streamedTileMap);

            return(streamedTileMap);
        }