summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Tiled/TiledMapTileset.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Tiled/TiledMapTileset.cs')
-rw-r--r--Plugins/MonoGame.Extended/source/MonoGame.Extended.Tiled/TiledMapTileset.cs70
1 files changed, 70 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Tiled/TiledMapTileset.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Tiled/TiledMapTileset.cs
new file mode 100644
index 0000000..ed14e15
--- /dev/null
+++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Tiled/TiledMapTileset.cs
@@ -0,0 +1,70 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using MonoGame.Extended.TextureAtlases;
+
+namespace MonoGame.Extended.Tiled
+{
+ public interface ITileset
+ {
+ int ActualWidth { get; }
+ int Columns { get; }
+ int ActualHeight { get; }
+ int Rows { get; }
+ int TileWidth { get; }
+ int TileHeight { get; }
+ Texture2D Texture { get; }
+ TextureRegion2D GetRegion(int column, int row);
+ }
+
+ public class TiledMapTileset : ITileset
+ {
+ public TiledMapTileset(Texture2D texture, string type, int tileWidth, int tileHeight, int tileCount, int spacing, int margin, int columns)
+ {
+ Texture = texture;
+ Type = type;
+ TileWidth = tileWidth;
+ TileHeight = tileHeight;
+ TileCount = tileCount;
+ Spacing = spacing;
+ Margin = margin;
+ Columns = columns;
+ Properties = new TiledMapProperties();
+ Tiles = new List<TiledMapTilesetTile>();
+ }
+
+ public string Name => Texture.Name;
+ public Texture2D Texture { get; }
+
+ public TextureRegion2D GetRegion(int column, int row)
+ {
+ var x = Margin + column * (TileWidth + Spacing);
+ var y = Margin + row * (TileHeight + Spacing);
+ return new TextureRegion2D(Texture, x, y, TileWidth, TileHeight);
+ }
+
+ public string Type { get; }
+ public int TileWidth { get; }
+ public int TileHeight { get; }
+ public int Spacing { get; }
+ public int Margin { get; }
+ public int TileCount { get; }
+ public int Columns { get; }
+ public List<TiledMapTilesetTile> Tiles { get; }
+ public TiledMapProperties Properties { get; }
+
+ public int Rows => (int)Math.Ceiling((double) TileCount / Columns);
+ public int ActualWidth => TileWidth * Columns;
+ public int ActualHeight => TileHeight * Rows;
+
+ public Rectangle GetTileRegion(int localTileIdentifier)
+ {
+ return Texture is not null
+ ? TiledMapHelper.GetTileSourceRectangle(localTileIdentifier, TileWidth, TileHeight, Columns, Margin,
+ Spacing)
+ : Tiles.FirstOrDefault(x => x.LocalTileIdentifier == localTileIdentifier).Texture.Bounds;
+ }
+ }
+}