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); }