diff --git a/Source/MonoGame.Extended/MonoGame.Extended.csproj b/Source/MonoGame.Extended/MonoGame.Extended.csproj index 2479522c..d8a55a1b 100644 --- a/Source/MonoGame.Extended/MonoGame.Extended.csproj +++ b/Source/MonoGame.Extended/MonoGame.Extended.csproj @@ -54,6 +54,7 @@ + diff --git a/Source/MonoGame.Extended/Tiled/TiledLayer.cs b/Source/MonoGame.Extended/Tiled/TiledLayer.cs index b02c22d2..ad705026 100644 --- a/Source/MonoGame.Extended/Tiled/TiledLayer.cs +++ b/Source/MonoGame.Extended/Tiled/TiledLayer.cs @@ -1,4 +1,6 @@ -using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended.Graphics; @@ -11,10 +13,10 @@ namespace MonoGame.Extended.Tiled Name = name; Width = width; Height = height; - + _tiledMap = tiledMap; - _data = data; _spriteBatch = new SpriteBatch(graphicsDevice); + _tiles = CreateTiles(data); } public string Name { get; private set; } @@ -22,33 +24,106 @@ namespace MonoGame.Extended.Tiled public int Height { get; private set; } private readonly TiledMap _tiledMap; - private readonly int[] _data; + private readonly TiledTile[] _tiles; private readonly SpriteBatch _spriteBatch; - public void Draw(Camera2D camera) + private TiledTile[] CreateTiles(int[] data) { - _spriteBatch.Begin(sortMode: SpriteSortMode.Immediate, blendState: BlendState.AlphaBlend, - samplerState: SamplerState.PointClamp, transformMatrix: camera.GetViewMatrix()); + var tiles = new TiledTile[data.Length]; + var index = 0; for (var y = 0; y < Height; y++) { for (var x = 0; x < Width; x++) { - var id = _data[x + y * Width]; - var region = _tiledMap.GetTileRegion(id); + tiles[x + y * Width] = new TiledTile(data[index], x, y); + index++; + } + } - if (region != null) - { - // not exactly sure why we need to compensate 1 pixel here. Could be a bug in MonoGame? - var tx = x * (_tiledMap.TileWidth - 1); - var ty = y * (_tiledMap.TileHeight - 1); + return tiles; + } + + public void Draw(Camera2D camera) + { + var renderOrderFunction = GetRenderOrderFunction(); + + _spriteBatch.Begin(sortMode: SpriteSortMode.Immediate, blendState: BlendState.AlphaBlend, + samplerState: SamplerState.PointClamp, transformMatrix: camera.GetViewMatrix()); + + foreach (var tile in renderOrderFunction()) + { + var region = _tiledMap.GetTileRegion(tile.Id); + + if (region != null) + { + // not exactly sure why we need to compensate 1 pixel here. Could be a bug in MonoGame? + var tx = tile.X * (_tiledMap.TileWidth - 1); + var ty = tile.Y * (_tiledMap.TileHeight - 1); - _spriteBatch.Draw(region, new Rectangle(tx, ty, region.Width, region.Height), Color.White); - } + _spriteBatch.Draw(region, new Rectangle(tx, ty, region.Width, region.Height), Color.White); } } _spriteBatch.End(); } + + public TiledTile GetTile(int x, int y) + { + return _tiles[x + y * Width]; + } + + private Func> GetRenderOrderFunction() + { + switch (_tiledMap.RenderOrder) + { + case TiledMapRenderOrder.LeftDown: + return GetTilesLeftDown; + case TiledMapRenderOrder.LeftUp: + return GetTilesLeftUp; + case TiledMapRenderOrder.RightDown: + return GetTilesRightDown; + case TiledMapRenderOrder.RightUp: + return GetTilesRightUp; + } + + throw new NotSupportedException(string.Format("{0} is not supported", _tiledMap.RenderOrder)); + } + + private IEnumerable GetTilesRightDown() + { + for (var y = 0; y < Height; y++) + { + for (var x = 0; x < Width; x++) + yield return GetTile(x, y); + } + } + + private IEnumerable GetTilesRightUp() + { + for (var y = Height - 1; y >= 0; y--) + { + for (var x = 0; x < Width; x++) + yield return GetTile(x, y); + } + } + + private IEnumerable GetTilesLeftDown() + { + for (var y = 0; y < Height; y++) + { + for (var x = Width - 1; x >= 0; x--) + yield return GetTile(x, y); + } + } + + private IEnumerable GetTilesLeftUp() + { + for (var y = Height - 1; y >= 0; y--) + { + for (var x = Width - 1; x >= 0; x--) + yield return GetTile(x, y); + } + } } } \ No newline at end of file diff --git a/Source/MonoGame.Extended/Tiled/TiledTile.cs b/Source/MonoGame.Extended/Tiled/TiledTile.cs new file mode 100644 index 00000000..8106fb2e --- /dev/null +++ b/Source/MonoGame.Extended/Tiled/TiledTile.cs @@ -0,0 +1,16 @@ +namespace MonoGame.Extended.Tiled +{ + public class TiledTile + { + public TiledTile (int id, int x, int y) + { + Id = id; + X = x; + Y = y; + } + + public int Id { get; private set; } + public int X { get; private set; } + public int Y { get; private set; } + } +} \ No newline at end of file diff --git a/Source/Sandbox/Content/test-tileset-left-down.tmx b/Source/Sandbox/Content/test-tileset-left-down.tmx index 429929fd..620cf08e 100644 --- a/Source/Sandbox/Content/test-tileset-left-down.tmx +++ b/Source/Sandbox/Content/test-tileset-left-down.tmx @@ -1,7 +1,7 @@ - + diff --git a/Source/Sandbox/Content/test-tileset-left-up.tmx b/Source/Sandbox/Content/test-tileset-left-up.tmx index 4da6b029..58099f2a 100644 --- a/Source/Sandbox/Content/test-tileset-left-up.tmx +++ b/Source/Sandbox/Content/test-tileset-left-up.tmx @@ -1,7 +1,7 @@ - + diff --git a/Source/Sandbox/Content/test-tileset-right-down.tmx b/Source/Sandbox/Content/test-tileset-right-down.tmx index 32a50cfc..b1a7f197 100644 --- a/Source/Sandbox/Content/test-tileset-right-down.tmx +++ b/Source/Sandbox/Content/test-tileset-right-down.tmx @@ -1,7 +1,7 @@ - + diff --git a/Source/Sandbox/Content/test-tileset-right-up.tmx b/Source/Sandbox/Content/test-tileset-right-up.tmx index 4e11118a..21f2f52b 100644 --- a/Source/Sandbox/Content/test-tileset-right-up.tmx +++ b/Source/Sandbox/Content/test-tileset-right-up.tmx @@ -1,7 +1,7 @@ - + diff --git a/Source/Sandbox/SandboxGame.cs b/Source/Sandbox/SandboxGame.cs index 38d18a71..3986de1b 100644 --- a/Source/Sandbox/SandboxGame.cs +++ b/Source/Sandbox/SandboxGame.cs @@ -44,7 +44,7 @@ namespace Sandbox _camera = new Camera2D(_viewportAdapter) { Zoom = 1.5f, - //Position = new Vector2(900, 650) + Position = new Vector2(-200, -200) }; Window.AllowUserResizing = true; @@ -58,7 +58,7 @@ namespace Sandbox _spriteBatch = new SpriteBatch(GraphicsDevice); _backgroundTexture = Content.Load("hills"); _bitmapFont = Content.Load("courier-new-32"); - _tiledMap = Content.Load("test-tileset-left-down"); + _tiledMap = Content.Load("test-tileset-right-down"); } protected override void UnloadContent()