From dd3c3cfc30dc49eb45af0ef7fd6ec8730b4d06e6 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 6 Aug 2019 22:31:57 +0200 Subject: [PATCH] added simple auto tiling --- MLEM/MLEM.csproj | 2 +- MLEM/Misc/AutoTiling.cs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 MLEM/Misc/AutoTiling.cs diff --git a/MLEM/MLEM.csproj b/MLEM/MLEM.csproj index 85e0002..b32cf5a 100644 --- a/MLEM/MLEM.csproj +++ b/MLEM/MLEM.csproj @@ -10,7 +10,7 @@ https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM/blob/master/LICENSE - 1.0.5 + 1.0.6 diff --git a/MLEM/Misc/AutoTiling.cs b/MLEM/Misc/AutoTiling.cs new file mode 100644 index 0000000..ff4de86 --- /dev/null +++ b/MLEM/Misc/AutoTiling.cs @@ -0,0 +1,30 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace MLEM.Misc { + public class AutoTiling { + + public static void DrawAutoTile(SpriteBatch batch, Vector2 pos, Texture2D texture, Rectangle textureRegion, ConnectsTo connectsTo, Color color) { + var up = connectsTo(0, -1); + var down = connectsTo(0, 1); + var left = connectsTo(-1, 0); + var right = connectsTo(1, 0); + + var xUl = up && left ? connectsTo(-1, -1) ? 0 : 4 : left ? 1 : up ? 3 : 2; + var xUr = up && right ? connectsTo(1, -1) ? 0 : 4 : right ? 1 : up ? 3 : 2; + var xDl = down && left ? connectsTo(-1, 1) ? 0 : 4 : left ? 1 : down ? 3 : 2; + var xDr = down && right ? connectsTo(1, 1) ? 0 : 4 : right ? 1 : down ? 3 : 2; + + var (x, y) = pos; + var (sizeX, sizeY) = textureRegion.Size; + var (halfSizeX, halfSizeY) = new Point(sizeX / 2, sizeY / 2); + batch.Draw(texture, new Vector2(x, y), new Rectangle(textureRegion.X + 0 + xUl * sizeX, textureRegion.Y + 0, sizeX, sizeY), color); + batch.Draw(texture, new Vector2(x + 0.5F * sizeX, y), new Rectangle(textureRegion.X + halfSizeX + xUr * sizeX, textureRegion.Y + 0, sizeX, sizeY), color); + batch.Draw(texture, new Vector2(x, y + 0.5F * sizeY), new Rectangle(textureRegion.X + xDl * sizeX, textureRegion.Y + halfSizeY, sizeX, sizeY), color); + batch.Draw(texture, new Vector2(x + 0.5F * sizeX, y + 0.5F * sizeY), new Rectangle(textureRegion.X + halfSizeX + xDr * sizeX, textureRegion.Y + halfSizeY, sizeX, sizeY), color); + } + + public delegate bool ConnectsTo(int xOff, int yOff); + + } +} \ No newline at end of file