Files
MonoGame.Extended/Source/MonoGame.Extended.Tiled/TiledMapTileset.cs
T
2018-11-25 13:03:39 +10:00

64 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
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, int tileWidth, int tileHeight, int tileCount, int spacing, int margin, int columns)
{
Texture = texture;
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 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 TiledMapHelper.GetTileSourceRectangle(localTileIdentifier, TileWidth, TileHeight, Columns, Margin, Spacing);
}
}
}