From 14e97abf87e318b80df27fdd5386ac521a34764d Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 9 Mar 2021 18:56:55 +0100 Subject: [PATCH] some more tiled map extension utility methods --- MLEM.Extended/Tiled/TiledExtensions.cs | 41 ++++++++++++++++++++------ 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/MLEM.Extended/Tiled/TiledExtensions.cs b/MLEM.Extended/Tiled/TiledExtensions.cs index dff4289..77bffec 100644 --- a/MLEM.Extended/Tiled/TiledExtensions.cs +++ b/MLEM.Extended/Tiled/TiledExtensions.cs @@ -115,15 +115,7 @@ namespace MLEM.Extended.Tiled { if (tile.IsBlank) return null; var localId = tile.GetLocalIdentifier(tileset, map); - 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; + return tileset.GetTilesetTile(localId, createStub); } /// @@ -141,6 +133,24 @@ namespace MLEM.Extended.Tiled { return tileset.GetTilesetTile(tile, map, createStub); } + /// + /// Gets the tileset tile on the given tileset for the given local id. + /// + /// The tileset + /// The tile's local id + /// If a tileset tile has no special properties, there is no pre-made object for it. If this boolean is true, a stub object with no extra data will be created instead of returning null. + /// null if the tile is blank or the tileset tile if there is one or createStub is true + public static TiledMapTilesetTile GetTilesetTile(this TiledMapTileset tileset, int localId, bool createStub = true) { + var tilesetTile = tileset.Tiles.FirstOrDefault(t => t.LocalTileIdentifier == localId); + if (tilesetTile == null && createStub) { + if (!StubTilesetTiles.TryGetValue(localId, out tilesetTile)) { + tilesetTile = new TiledMapTilesetTile(localId); + StubTilesetTiles.Add(localId, tilesetTile); + } + } + return tilesetTile; + } + /// /// Gets the layer index of the layer with the given name in the array. /// @@ -179,6 +189,19 @@ namespace MLEM.Extended.Tiled { layer.SetTile((ushort) x, (ushort) y, (uint) globalTile); } + /// + /// Sets the tiled map tile at the given location to the given tile from the given tileset + /// + /// The map + /// The name of the layer + /// The x coordinate + /// The y coordinate + /// The tileset to useR + /// The tile to place, from the given tileset + public static void SetTile(this TiledMap map, string layerName, int x, int y, TiledMapTileset tileset, TiledMapTilesetTile tile) { + map.SetTile(layerName, x, y, tile.GetGlobalIdentifier(tileset, map)); + } + /// /// For an x and y coordinate, returns an enumerable of all of the tiles on each of the map's . ///