/// <summary> /// Get a selection of tiles from the selected layer. /// </summary> /// <param name="grid">The grid to use for selection data.</param> /// <returns>An array of tiles.</returns> private GMareTile[,] GetTiles(GMareBrush grid) { // A new array of tile ids. Rectangle rect = grid.ToRectangle(); Size tileSize = ProjectManager.Room.TileSize; GMareTile[,] tiles = new GMareTile[rect.Width / tileSize.Width, rect.Height / tileSize.Height]; // Iterate through columns. for (int col = 0; col < tiles.GetLength(0); col++) { // Iterate through rows. for (int row = 0; row < tiles.GetLength(1); row++) { // Calculate source position. int x = ((col * tileSize.Width) + rect.X) / tileSize.Width; int y = ((row * tileSize.Height) + rect.Y) / tileSize.Height; // Set tile id. tiles[col, row] = ProjectManager.Room.Layers[_layerIndex].Tiles2[x, y].Clone(); } } // Return selected tiles. return tiles; }
/// <summary> /// Fills in tile sections with the desired tile. /// </summary> /// <param name="origin">The starting point for the fill, in tiles.</param> /// <param name="tileId">The tile id to fill with.</param> public void Fill(Point origin, int tileId) { // Create a new array of tiles. GMareTile[,] tiles = new GMareTile[1, 1]; tiles[0, 0] = new GMareTile(); tiles[0, 0].TileId = tileId; // Fill with tile array. Fill(origin, tiles); }
/// <summary> /// Fills in tile sections with the desired brush. /// </summary> /// <param name="origin">The starting point for the fill, in tiles.</param> /// <param name="tiles">The array of tiles to fill with.</param> public void Fill(Point origin, GMareTile[,] tiles) { // If the origin is out of bounds, return. if (origin.X < 0 || origin.X >= _tiles2.GetLength(0) || origin.Y < 0 || origin.Y >= _tiles2.GetLength(1)) return; // Get target tile. int target = _tiles2[origin.X, origin.Y].TileId; // Get the valid fill array. bool[,] mask = this.GetFillMask(origin, target); // Calculate fill start offsets. int offsetX = origin.X % tiles.GetLength(0); int offsetY = origin.Y % tiles.GetLength(1); // If the offsetX is not zero, adjust offsetX origin. if (offsetX != 0) offsetX = tiles.GetLength(0) - offsetX; // If the offsetY is not zero, adjust offsetY origin. if (offsetY != 0) offsetY = tiles.GetLength(1) - offsetY; // Selection indexers, start them at the offsets. int x = offsetX; int y = offsetY; // Iterate through layer columns. for (int col = 0; col < _tiles2.GetLength(0); col++) { // Iterate through layer rows. for (int row = 0; row < _tiles2.GetLength(1); row++) { // If the coordinate values are allowed by the mask and equal the target, replace the tile. if (mask[col, row] == true && _tiles2[col, row].TileId == target) _tiles2[col, row] = tiles[x, y].Clone(); // Increment selection's row index. y++; // If at the max height of the tile selection, reset row index. if (y == tiles.GetLength(1)) y = 0; // If at the max height of the layer, reset row index to offsetY. if (row == _tiles2.GetLength(1) - 1) y = offsetY; } // Increment selection's column index. x++; // If at the max width of the tile selection, reset column index. if (x == tiles.GetLength(0)) x = 0; // If at the max width of the layer, reset column index to offsetX. if (col == _tiles2.GetLength(0) - 1) x = offsetX; } }
/// <summary> /// Clears all the tiles from the layer. /// </summary> public void Clear() { // If no tiles exist, return. if (_tiles2 == null) return; // Get column and row amounts. int cols = _tiles2.GetLength(0); int rows = _tiles2.GetLength(1); // Iterate through columns. for (int col = 0; col < cols; col++) { // Iterate through rows. for (int row = 0; row < rows; row++) { // Set the tile to new empty tile. _tiles2[col, row] = new GMareTile(); } } }
/// <summary> /// Sets all tile values to -1, which means empty. /// </summary> /// <param name="array">The array to empty.</param> /// <returns>An empty array.</returns> public static GMareTile[,] GetEmptyLayer(int cols, int rows) { // Create a new tile 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++) { // Set the tile to empty. tiles[col, row] = new GMareTile(); } } // Return an empty array of tiles. return tiles; }
/// <summary> /// Constructs a new GMare Layer, with name, depth and tile data. /// </summary> /// <param name="name">The name of the layer.</param> /// <param name="depth">The depth of the layer.</param> /// <param name="tiles">The tiles for the layer.</param> public GMareLayer(string name, int depth, GMareTile[,] tiles) { // Set fields. _name = name; _depth = depth; _tiles2 = new GMareTile[tiles.GetLength(0), tiles.GetLength(1)]; // Iterate through tiles. for (int y = 0; y < _tiles2.GetLength(1); y++) for (int x = 0; x < _tiles2.GetLength(0); x++) _tiles2[x, y] = tiles[x, y].Clone(); }
/// <summary> /// Makes a shallow copy of this tile. /// </summary> /// <returns>A shallow copy of this tile.</returns> public GMareTile Clone() { // Create a new tile. GMareTile tile = new GMareTile(); // Set properties. tile.TileId = _tileId; tile.FlipMode = _flip; tile.Blend = _blend; // Return cloned tile. return tile; }
/// <summary> /// Converts Ore tile array to GMare tile array. /// </summary> /// <param name="oreTiles">The Ore tile array to convert.</param> /// <param name="cols">The amount of tiles horizontally.</param> /// <param name="rows">The amount of tiles vertically.</param> /// <returns>A converted tile array.</returns> private static GMareTile[,] OreTilesToGMareTiles(int[] oreTiles, int cols, int rows) { // Ore tile indexer. int index = 0; // Create a new 2d array of tiles. GMareTile[,] tiles = new GMareTile[cols, rows]; // Iterate through rows. for (int row = 0; row < rows; row++) { // Iterate through columns. for (int col = 0; col < cols; col++) { // Create new tile. tiles[col, row] = new GMareTile(); // Set tile id. tiles[col, row].TileId = oreTiles[index]; // Increament indexer. index++; } } // Return converted tiles. return tiles; }