From d3dc40600e19ff0065fb7906fc826d3088180c36 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 7 Dec 2019 15:17:40 +0100 Subject: [PATCH] added stub tileset tiles to make interaction with tiled easier --- MLEM.Extended/Tiled/TiledExtensions.cs | 20 ++++++++++++++++---- MLEM.Extended/Tiled/TiledMapCollisions.cs | 4 ---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/MLEM.Extended/Tiled/TiledExtensions.cs b/MLEM.Extended/Tiled/TiledExtensions.cs index d9b7188..1f502c9 100644 --- a/MLEM.Extended/Tiled/TiledExtensions.cs +++ b/MLEM.Extended/Tiled/TiledExtensions.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using MonoGame.Extended; @@ -6,6 +8,8 @@ using MonoGame.Extended.Tiled; namespace MLEM.Extended.Tiled { public static class TiledExtensions { + private static readonly Dictionary StubTilesetTiles = new Dictionary(); + public static string Get(this TiledMapProperties properties, string key) { properties.TryGetValue(key, out var val); return val; @@ -38,18 +42,26 @@ namespace MLEM.Extended.Tiled { return tile.GlobalIdentifier - map.GetTilesetFirstGlobalIdentifier(tileset); } - public static TiledMapTilesetTile GetTilesetTile(this TiledMapTileset tileset, TiledMapTile tile, TiledMap map) { + public static TiledMapTilesetTile GetTilesetTile(this TiledMapTileset tileset, TiledMapTile tile, TiledMap map, bool createStub = true) { if (tile.IsBlank) return null; var localId = tile.GetLocalIdentifier(tileset, map); - return tileset.Tiles.FirstOrDefault(t => t.LocalTileIdentifier == localId); + var tilesetTile = tileset.Tiles.FirstOrDefault(t => t.LocalTileIdentifier == localId); + if (tilesetTile == null && createStub) { + var id = tile.GetLocalIdentifier(tileset, map); + if (!StubTilesetTiles.TryGetValue(id, out tilesetTile)) { + tilesetTile = new TiledMapTilesetTile(id); + StubTilesetTiles.Add(id, tilesetTile); + } + } + return tilesetTile; } - public static TiledMapTilesetTile GetTilesetTile(this TiledMapTile tile, TiledMap map) { + public static TiledMapTilesetTile GetTilesetTile(this TiledMapTile tile, TiledMap map, bool createStub = true) { if (tile.IsBlank) return null; var tileset = tile.GetTileset(map); - return tileset?.GetTilesetTile(tile, map); + return tileset.GetTilesetTile(tile, map, createStub); } public static int GetTileLayerIndex(this TiledMap map, string layerName) { diff --git a/MLEM.Extended/Tiled/TiledMapCollisions.cs b/MLEM.Extended/Tiled/TiledMapCollisions.cs index 902d36b..a63ce4c 100644 --- a/MLEM.Extended/Tiled/TiledMapCollisions.cs +++ b/MLEM.Extended/Tiled/TiledMapCollisions.cs @@ -38,10 +38,6 @@ namespace MLEM.Extended.Tiled { return; } var tilesetTile = tile.GetTilesetTile(this.map); - if (tilesetTile == null) { - this.collisionInfos[layerIndex, x, y] = null; - return; - } this.collisionInfos[layerIndex, x, y] = new TileCollisionInfo(this.map, new Vector2(x, y), tile, tilesetTile); }