Exemplo n.º 1
0
        /// <summary>
        /// Converts a rectangle to an array of new tiles
        /// </summary>
        /// <param name="rectangle">The source rectangle to copy tiles from</param>
        /// <param name="width">The width of the source tileset</param>
        /// <param name="tileSize">The tile size of a single tile</param>
        /// <returns>An array of new tiles</returns>
        public static GMareTile[,] RectangleToTiles(Rectangle rectangle, int width, Size tileSize)
        {
            // Position variables
            int x = 0;
            int y = 0;

            // Calculate columns and rows
            int cols = rectangle.Width / tileSize.Width;
            int rows = rectangle.Height / tileSize.Height;

            // Create a new tile id array
            GMareTile[,] tiles = new GMareTile[cols, rows];

            // Iterate through columns
            for (int col = 0; col < cols; col++)
            {
                // Iterate through rows
                for (int row = 0; row < rows; row++)
                {
                    // Calculate tile position
                    x = (col * tileSize.Width) + rectangle.X;
                    y = (row * tileSize.Height) + rectangle.Y;

                    // Create new tile
                    GMareTile tile = new GMareTile();
                    tile.TileId = PositionToSourceTileId(x, y, width, tileSize);

                    // Set tile
                    tiles[col, row] = tile;
                }
            }

            // Return the array of tile ids
            return(tiles);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Flips the brush tiles
        /// </summary>
        /// <param name="direction">The direction to flip the tile array</param>
        public void Flip(FlipDirectionType direction)
        {
            // Get array dimensions
            int width  = _tiles.GetLength(0);
            int height = _tiles.GetLength(1);

            // Create new tile array
            GMareTile[,] tiles = new GMareTile[width, height];

            // Do action based on direction
            switch (direction)
            {
            // Flip horizontally
            case FlipDirectionType.Horizontal:
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        tiles[x, y]          = _tiles[(width - 1) - x, y].Clone();
                        tiles[x, y].FlipMode = tiles[x, y].FlipMode;
                        tiles[x, y].Flip(FlipDirectionType.Horizontal);
                    }
                }

                break;

            // Flip vertically
            case FlipDirectionType.Vertical:
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        tiles[x, y]          = _tiles[x, (height - 1) - y].Clone();
                        tiles[x, y].FlipMode = tiles[x, y].FlipMode;
                        tiles[x, y].Flip(FlipDirectionType.Vertical);
                    }
                }

                break;
            }

            // Dereference old tiles
            _tiles = null;

            // Reference new one
            _tiles = tiles;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts a rectangle to a array of new tiles
        /// </summary>
        /// <param name="rectangle">The source rectangle to copy tiles from</param>
        /// <param name="width">The width of the source tileset</param>
        /// <returns>A new tile brush</returns>
        public static GMareBrush RectangleToTileBrush(Rectangle rectangle, int width, Size tileSize)
        {
            GMareBrush brush = new GMareBrush();

            // Position variables
            int x = 0;
            int y = 0;

            // Calculate columns and rows
            int cols = rectangle.Width / tileSize.Width;
            int rows = rectangle.Height / tileSize.Height;

            // Create a new tile id array
            GMareTile[,] tiles = new GMareTile[cols, rows];

            // Iterate through columns
            for (int col = 0; col < cols; col++)
            {
                // Iterate through rows
                for (int row = 0; row < rows; row++)
                {
                    // Calculate tile position.
                    x = (col * tileSize.Width) + rectangle.X;
                    y = (row * tileSize.Height) + rectangle.Y;

                    // Create new tile
                    GMareTile tile = new GMareTile();
                    tile.TileId = PositionToSourceTileId(x, y, width, tileSize);

                    // Set tile
                    tiles[col, row] = tile;
                }
            }

            // Set brush properties
            brush.Tiles  = tiles;
            brush.StartX = rectangle.X;
            brush.StartY = rectangle.Y;
            brush.EndX   = rectangle.Right;
            brush.EndY   = rectangle.Bottom;

            return(brush);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new copy of this brush
        /// </summary>
        /// <returns>A new brush copy</returns>
        public GMareBrush Clone()
        {
            GMareBrush brush = (GMareBrush)this.MemberwiseClone();

            brush.Glyph        = _glyph == null ? null : (Bitmap)_glyph.Clone();
            GMareTile[,] tiles = new GMareTile[_tiles.GetLength(0), _tiles.GetLength(1)];

            // Deep clone
            for (int x = 0; x < _tiles.GetLength(0); x++)
            {
                for (int y = 0; y < _tiles.GetLength(1); y++)
                {
                    tiles[x, y] = _tiles[x, y].Clone();
                }
            }

            brush.Tiles = tiles;
            return(brush);
        }