Exemple #1
0
 /// <summary>
 /// Returns a grid position of a given tile position
 /// </summary>
 /// <param name="row">Tile row index</param>
 /// <param name="column">Tile column index</param>
 /// <param name="gridRow">Returns the grid row index</param>
 /// <param name="gridColumn">Returns the grid column index</param>
 public void ConvertTileToGridPosition(int row, int column, out int gridRow, out int gridColumn)
 {
     gridRow    = (int)Math.Floor((double)row / (double)TileRowCount);
     gridColumn = (int)Math.Floor((double)column / (double)TileColumnCount);
     if (TileMathHelper.IsOutOfRange(gridRow, gridColumn, GridRowCount, GridColumnCount))
     {
         gridRow    = TileMathHelper.FixTileIndex(gridRow, GridRowCount);
         gridColumn = TileMathHelper.FixTileIndex(gridColumn, GridColumnCount);
     }
 }
Exemple #2
0
        /// <summary>
        /// Determine all tiles on screen based on the given viewport
        /// </summary>
        /// <param name="viewportWidth">Viewport width in pixel</param>
        /// <param name="viewportHeight">Viewport height in pixel</param>
        /// <returns>Returns a flatten byte array which represants the tiles</returns>
        public Tile[] GetTileMapInScreen(int viewportWidth, int viewportHeight)
        {
            ScreenWidth  = viewportWidth;
            ScreenHeight = viewportHeight;

            int screenColumnCount = (viewportWidth / tileSize);
            int screenRowCount    = (viewportHeight / tileSize);

            Tile[] tilesInScreen = new Tile[screenRowCount * screenColumnCount];

            int posRow    = tileRow + (screenRowCount / 2);
            int negRow    = tileRow - (screenRowCount / 2);
            int posColumn = tileColumn + (screenColumnCount / 2);
            int negColumn = tileColumn - (screenColumnCount / 2);

            int rowIndex = 0;

            for (int r = negRow; r < posRow; r++)
            {
                int columnIndex = 0;
                for (int c = negColumn; c < posColumn; c++)
                {
                    int newMapIndex = CurrentMapIndex;
                    int realRow     = r;
                    int realColumn  = c;
                    if (TileMathHelper.IsOutOfRange(r, c, TileRowCount, TileColumnCount))
                    {
                        newMapIndex = TileMathHelper.GetMapIndex(r, c, TileRowCount, TileColumnCount, newMapIndex);
                        realRow     = TileMathHelper.FixTileIndex(r, TileRowCount);
                        realColumn  = TileMathHelper.FixTileIndex(c, TileColumnCount);
                    }

                    //GetValues and merge
                    if (newMapIndex < 0)
                    {
                        tilesInScreen[TileMathHelper.ToIndex(rowIndex, columnIndex++, screenColumnCount)] = new Tile(ushort.MaxValue, 255);
                        int newGridRow    = (int)Math.Floor((double)r / (double)TileRowCount);
                        int newGridColumn = (int)Math.Floor((double)c / (double)TileColumnCount);
                        GridGenerationIsSlow?.Invoke(this, new GridEventArgs(newGridRow, newGridColumn, GridRow, GridColumn, false));
                    }
                    else
                    {
                        tilesInScreen[TileMathHelper.ToIndex(rowIndex, columnIndex++, screenColumnCount)] = maps[newMapIndex].MapSurface[TileMathHelper.ToIndex(realRow, realColumn, TileColumnCount)];
                    }
                }
                rowIndex++;
            }

            return(tilesInScreen);
        }
Exemple #3
0
 /// <summary>
 /// Initzializes a new streamed tile map
 /// </summary>
 /// <param name="spawnTileRow">Tile row index</param>
 /// <param name="spawnTileColumn">Tile column index</param>
 /// <param name="gridColumnCount">Number of grid columns</param>
 /// <param name="gridRowCount">Number of grid rows</param>
 /// <param name="tileRowCount">Number of tile rows per grid</param>
 /// <param name="tileColumnCount">Number of tile columns per grid</param>
 /// <param name="tileSize">Tile size in pixel</param>
 /// <param name="maps">Loaded grids</param>
 public StreamedTileMap(int spawnTileRow, int spawnTileColumn, int gridColumnCount, int gridRowCount, int tileRowCount, int tileColumnCount, int tileSize, List <TileMapPart> maps)
 {
     this.maps            = maps;
     this.gridColumnCount = gridColumnCount;
     this.gridRowCount    = gridRowCount;
     this.tileSize        = tileSize;
     this.tileRowCount    = tileRowCount;
     this.tileColumnCount = tileColumnCount;
     currentMapIndex      = 4;
     tileRow    = spawnTileRow;
     tileColumn = spawnTileColumn;
     gridRow    = (int)Math.Floor((double)spawnTileRow / (double)tileRowCount);
     gridColumn = (int)Math.Floor((double)spawnTileColumn / (double)tileColumnCount);
     gridRow    = TileMathHelper.FixTileIndex(gridRow, gridRowCount);
     gridColumn = TileMathHelper.FixTileIndex(gridColumn, gridColumnCount);
 }
        /// <summary>
        /// Create the StreamedTileMap from the generated grids
        /// </summary>
        /// <param name="maps">Current grid with all it's surrounding grids</param>
        /// <param name="suroundingGrids">Ids of all surrounding grids</param>
        /// <param name="gridsPerRow">Number grids per row</param>
        /// <param name="streamedTileMap">A reference to a streamedTileMap</param>
        private void CreateTileMapParts(Tile[][] maps, List <ObjectTile>[] objectTiles, int[] suroundingGrids, int gridsPerRow, StreamedTileMap streamedTileMap)
        {
            //create TileMapPart
            for (int i = 0; i < maps.Length; i++)
            {
                int gridId = suroundingGrids[i];

                int gridRow;
                int gridColumn;
                TileMathHelper.ToPosition(gridId, gridsPerRow, out gridRow, out gridColumn);

                gridRow    = TileMathHelper.FixTileIndex(gridRow, gridsPerRow);
                gridColumn = TileMathHelper.FixTileIndex(gridColumn, gridsPerRow);

                TileMapPart part = new TileMapPart(gridId, maps[i], null, objectTiles[i].ToArray(), gridColumn, gridRow);
                streamedTileMap.Add(part);
            }
        }
Exemple #5
0
        /// <summary>
        /// Initzializes a new streamed tile map
        /// </summary>
        /// <param name="generator">Tile map generator</param>
        /// <param name="spawnTileRow">Tile row index</param>
        /// <param name="spawnTileColumn">Tile column index</param>
        /// <param name="gridColumnCount">Number of grid columns</param>
        /// <param name="gridRowCount">Number of grid rows</param>
        /// <param name="tileRowCount">Number of tile rows per grid</param>
        /// <param name="tileColumnCount">Number of tile columns per grid</param>
        /// <param name="tileSize">Tile size in pixel</param>
        public StreamedTileMap(TileMapGenerator generator, int spawnTileRow, int spawnTileColumn, int gridColumnCount, int gridRowCount, int tileRowCount, int tileColumnCount, int tileSize)
        {
            maps = new List <TileMapPart>();
            this.gridColumnCount = gridColumnCount;
            this.gridRowCount    = gridRowCount;
            this.tileSize        = tileSize;
            this.tileRowCount    = tileRowCount;
            this.tileColumnCount = tileColumnCount;
            this.generator       = generator;
            currentMapIndex      = 4;
            tileRow    = spawnTileRow;
            tileColumn = spawnTileColumn;
            gridRow    = (int)Math.Floor((double)spawnTileRow / (double)tileRowCount);
            gridColumn = (int)Math.Floor((double)spawnTileColumn / (double)tileColumnCount);

            gridRow    = TileMathHelper.FixTileIndex(gridRow, gridRowCount);
            gridColumn = TileMathHelper.FixTileIndex(gridColumn, gridColumnCount);
        }
        /// <summary>
        /// Create a circular area to a given map
        /// </summary>
        /// <param name="map">The map where the area should be placed on</param>
        /// <param name="rowIndex">Tile row index</param>
        /// <param name="columnIndex">Tile column index</param>
        /// <param name="rowCount">Number of rows per grid</param>
        /// <param name="columnCount">Number of columns per grid</param>
        /// <param name="radius">Radius of the circle</param>
        /// <param name="tileSize">Size of a tile in pixel</param>
        /// <param name="metersPerTile">Size of a tile in meter</param>
        /// <param name="tileIdValue">Tile id value for the circular area</param>
        /// <param name="tileFlag">Tile flag</param>
        /// <param name="allowEdgeManipulation">Allows areas to overlap tiles from other maps</param>
        /// <returns>Returns the number of changed tiles</returns>
        private int CreateCircleArea(Tile[] map, int rowIndex, int columnIndex, int rowCount, int columnCount, int radius, int tileSize, float metersPerTile, ushort tileIdValue, byte tileFlag, bool allowEdgeManipulation)
        {
            int modified      = 0;
            int minRadiusTile = (int)Math.Ceiling(radius / metersPerTile);
            int minRadiusPx   = minRadiusTile * tileSize;
            int x             = columnIndex * tileSize;
            int y             = rowIndex * tileSize;

            for (int r = rowIndex - minRadiusTile; r < rowIndex + minRadiusTile; r++)
            {
                for (int c = columnIndex - minRadiusTile; c < columnIndex + minRadiusTile; c++)
                {
                    int realRow    = r;
                    int realColumn = c;

                    if (TileMathHelper.IsOutOfRange(r, c, rowCount, columnCount))
                    {
                        if (allowEdgeManipulation)
                        {
                            realRow    = TileMathHelper.FixTileIndex(r, rowCount);
                            realColumn = TileMathHelper.FixTileIndex(c, columnCount);
                        }
                        else
                        {
                            continue;
                        }
                    }


                    int distance = TileMathHelper.GetDistance(x, y, c * tileSize, r * tileSize);
                    if (distance <= minRadiusPx)
                    {
                        int id = TileMathHelper.ToIndex(realRow, realColumn, columnCount);
                        if (map[id].Id == 0)
                        {
                            map[id] = new Tile(tileIdValue, tileFlag);
                            modified++;
                        }
                    }
                }
            }

            return(modified);
        }
Exemple #7
0
        /// <summary>
        /// Sets a tile value
        /// </summary>
        /// <param name="row">Tile row index</param>
        /// <param name="column">Tile column index</param>
        /// <param name="value">New tile value</param>
        public bool SetTileValue(int row, int column, Tile value)
        {
            int newGridRow;
            int newGridColumn;

            ConvertTileToGridPosition(row, column, out newGridRow, out newGridColumn);

            int tileMapId       = TileMathHelper.ToIndex(newGridRow, newGridColumn, GridColumnCount);
            int tileRow         = TileMathHelper.FixTileIndex(row, TileRowCount);
            int tileColumn      = TileMathHelper.FixTileIndex(column, TileColumnCount);
            int currentMapIndex = maps.FindIndex(m => m.Id == tileMapId);

            if (currentMapIndex != -1)
            {
                maps[currentMapIndex].MapSurface[TileMathHelper.ToIndex(tileRow, tileColumn, TileColumnCount)] = value;
                return(true);
            }

            return(false);
        }
Exemple #8
0
        /// <summary>
        /// Updates the grids based one the given tile location
        /// </summary>
        /// <param name="currentTileRow">Tile row index</param>
        /// <param name="currentTileColumn">Tile column index</param>
        public void Update(int currentTileRow, int currentTileColumn)
        {
            int newGridRow;
            int newGridColumn;

            ConvertTileToGridPosition(currentTileRow, currentTileColumn, out newGridRow, out newGridColumn);

            currentMapId = TileMathHelper.ToIndex(newGridRow, newGridColumn, GridColumnCount);
            tileRow      = TileMathHelper.FixTileIndex(currentTileRow, TileRowCount);
            tileColumn   = TileMathHelper.FixTileIndex(currentTileColumn, TileColumnCount);

            TryMapUpdate(newGridRow, newGridColumn);

            CurrentMapIndex = maps.FindIndex(m => m.Id == currentMapId);

            if ((newGridRow != GridRow || newGridColumn != GridColumn) && !newMapRequested)
            {
                GridChangeRequested?.Invoke(this, new GridEventArgs(GridRow, GridColumn, maps[4].GridRow, maps[4].GridColumn, false));
                gridRow    = newGridRow;
                gridColumn = newGridColumn;
                Resize(currentTileRow, currentTileColumn);
            }
        }
        /// <summary>
        /// Create a circular area to a given map
        /// </summary>
        /// <param name="mapIndex">Index of the map</param>
        /// <param name="maps">Current grids</param>
        /// <param name="edgeOverrides">Current edge override maps</param>
        /// <param name="rowIndex">Tile row index</param>
        /// <param name="columnIndex">Tile column index</param>
        /// <param name="rowCount">Number of rows per grid</param>
        /// <param name="columnCount">Number of columns per grid</param>
        /// <param name="radius">Radius of the circle</param>
        /// <param name="tileSize">Size of a tile in pixel</param>
        /// <param name="metersPerTile">Size of a tile in meter</param>
        /// <param name="tileIdValue">Tile id value for the circular area</param>
        /// <param name="tileFlags">Tile flags</param>
        /// <param name="allowEdgeOverflow">Allows areas to overlap tiles from other maps</param>
        /// <param name="useEdgeNoise">Adds edge noise to the generated area</param>
        /// <returns>Returns the number of changed tiles</returns>
        private int CreateCircleArea(int mapIndex, Tile[][] maps, Tile[][] edgeOverrides, int rowIndex, int columnIndex, int rowCount, int columnCount, int radius, int tileSize, float metersPerTile, ushort tileIdValue, byte tileFlags, bool allowEdgeOverflow, bool useEdgeNoise)
        {
            int modified      = 0;
            int minRadiusTile = (int)Math.Ceiling(radius / metersPerTile);
            int minRadiusPx   = minRadiusTile * tileSize;
            int x             = columnIndex * tileSize;
            int y             = rowIndex * tileSize;

            for (int r = rowIndex - minRadiusTile; r < rowIndex + minRadiusTile; r++)
            {
                for (int c = columnIndex - minRadiusTile; c < columnIndex + minRadiusTile; c++)
                {
                    int currentMapIndex = mapIndex;
                    int realRow         = r;
                    int realColumn      = c;

                    if (TileMathHelper.IsOutOfRange(r, c, rowCount, columnCount))
                    {
                        if (allowEdgeOverflow)
                        {
                            currentMapIndex = TileMathHelper.GetMapIndex(r, c, rowCount, columnCount, currentMapIndex);
                            if (currentMapIndex >= 0)
                            {
                                realRow    = TileMathHelper.FixTileIndex(r, rowCount);
                                realColumn = TileMathHelper.FixTileIndex(c, columnCount);
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }

                    int distance = TileMathHelper.GetDistance(x, y, c * tileSize, r * tileSize);
                    if (distance <= minRadiusPx)
                    {
                        int id = TileMathHelper.ToIndex(realRow, realColumn, columnCount);
                        if (mapIndex == currentMapIndex)
                        {
                            if (maps[currentMapIndex][id].Id == 0)
                            {
                                maps[currentMapIndex][id] = new Tile(tileIdValue, tileFlags);
                                modified++;
                            }
                        }
                        else
                        {
                            if (edgeOverrides[currentMapIndex][id].Id == 0)
                            {
                                edgeOverrides[currentMapIndex][id] = new Tile(tileIdValue, tileFlags);
                            }
                        }
                    }
                }
            }

            return(modified);
        }