Skip to content

Commit

Permalink
Added method GetSourceRect and made method GetSourceVector obsolete
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBoneJarmer committed Apr 21, 2021
1 parent 004bb9d commit d8813ac
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/TiledMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public TiledTile GetTiledTile(TiledMapTileset mapTileset, TiledTileset tileset,
/// <param name="gid">An element within a TiledLayer.data array</param>
/// <returns>An int array of length 2 containing the x and y position of the source rect of the tileset image. Multiply the values by the tile width and height in pixels to get the actual x and y position. Returns null if the gid was not found</returns>
/// <remarks>This method currently doesn't take margin into account</remarks>
[Obsolete("Please use GetSourceRect instead because with future versions of Tiled this method may no longer be sufficient")]
public int[] GetSourceVector(TiledMapTileset mapTileset, TiledTileset tileset, int gid)
{
var tileHor = 0;
Expand All @@ -387,6 +388,44 @@ public int[] GetSourceVector(TiledMapTileset mapTileset, TiledTileset tileset, i

return null;
}

/// <summary>
/// This method can be used to figure out the source rect on a Tileset image for rendering tiles.
/// </summary>
/// <param name="mapTileset"></param>
/// <param name="tileset"></param>
/// <param name="gid"></param>
/// <returns>An instance of the class TiledSourceRect that represents a rectangle. Returns null if the provided gid was not found within the tileset.</returns>
public TiledSourceRect GetSourceRect(TiledMapTileset mapTileset, TiledTileset tileset, int gid)
{
var tileHor = 0;
var tileVert = 0;

for (var i = 0; i < tileset.TileCount; i++)
{
if (i == gid - mapTileset.firstgid)
{
var result = new TiledSourceRect();
result.x = tileHor * tileset.TileWidth;
result.y = tileVert * tileset.TileHeight;
result.width = tileset.TileWidth;
result.height = tileset.TileHeight;

return result;
}

// Update x and y position
tileHor++;

if (tileHor == tileset.ImageWidth / tileset.TileWidth)
{
tileHor = 0;
tileVert++;
}
}

return null;
}

/// <summary>
/// Checks is a tile is flipped horizontally
Expand Down
23 changes: 23 additions & 0 deletions src/TiledModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,27 @@ public class TiledTerrain
/// </summary>
public int tile;
}

/// <summary>
/// Used as data type for the GetSourceRect method. Represents basically a rectangle.
/// </summary>
public class TiledSourceRect
{
/// <summary>
/// The x position in pixels from the tile location in the source image
/// </summary>
public int x;
/// <summary>
/// The y position in pixels from the tile location in the source image
/// </summary>
public int y;
/// <summary>
/// The width in pixels from the tile in the source image
/// </summary>
public int width;
/// <summary>
/// The height in pixels from the tile in the source image
/// </summary>
public int height;
}
}

0 comments on commit d8813ac

Please sign in to comment.