Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        /// <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);
        }