From 45fc12b0cba2ff41321c203fb92f04135bd2c145 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 10 Mar 2021 02:13:07 +0100 Subject: [PATCH] added some additional tiled helper methods --- .../Tiled/IndividualTiledMapRenderer.cs | 39 +++++++++---------- MLEM.Extended/Tiled/TiledExtensions.cs | 32 +++++++++++++++ MLEM.Extended/Tiled/TiledMapCollisions.cs | 37 +++++++++++------- 3 files changed, 72 insertions(+), 36 deletions(-) diff --git a/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs b/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs index 0ba5e72..b55688e 100644 --- a/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs +++ b/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs @@ -24,7 +24,7 @@ namespace MLEM.Extended.Tiled { /// Creates a new individual tiled map renderer using the given map and depth function /// /// The map to use - /// The depth function to use + /// The depth function to use. Defaults to a function that assigns a depth of 0 to every tile. public IndividualTiledMapRenderer(TiledMap map = null, GetDepth depthFunction = null) { if (map != null) this.SetMap(map, depthFunction); @@ -34,7 +34,7 @@ namespace MLEM.Extended.Tiled { /// Sets this individual tiled map renderer's map and depth function /// /// The map to use - /// The depth function to use + /// The depth function to use. Defaults to a function that assigns a depth of 0 to every tile. public void SetMap(TiledMap map, GetDepth depthFunction = null) { this.map = map; this.depthFunction = depthFunction ?? ((tile, layer, layerIndex, position) => 0); @@ -84,7 +84,7 @@ namespace MLEM.Extended.Tiled { /// /// The sprite batch to use /// The area that is visible, in pixel space. - /// The draw function to use, or null for default + /// The draw function to use, or null to use public void Draw(SpriteBatch batch, RectangleF? frustum = null, DrawDelegate drawFunction = null) { for (var i = 0; i < this.map.TileLayers.Count; i++) { if (this.map.TileLayers[i].IsVisible) @@ -99,8 +99,9 @@ namespace MLEM.Extended.Tiled { /// The sprite batch to use /// The index of the layer in /// The area that is visible, in pixel space. - /// The draw function to use, or null for default + /// The draw function to use, or null to use public void DrawLayer(SpriteBatch batch, int layerIndex, RectangleF? frustum = null, DrawDelegate drawFunction = null) { + var draw = drawFunction ?? DefaultDraw; var frust = frustum ?? new RectangleF(0, 0, float.MaxValue, float.MaxValue); var minX = Math.Max(0, frust.Left / this.map.TileWidth).Floor(); var minY = Math.Max(0, frust.Top / this.map.TileHeight).Floor(); @@ -110,7 +111,7 @@ namespace MLEM.Extended.Tiled { for (var y = minY; y < maxY; y++) { var info = this.drawInfos[layerIndex, x, y]; if (info != null) - info.Draw(batch, drawFunction); + draw(batch, info); } } } @@ -124,6 +125,18 @@ namespace MLEM.Extended.Tiled { animation.Update(time); } + /// + /// The default implementation of that is used by if no custom draw function is passed + /// + /// The sprite batch to use for drawing + /// The to draw + public static void DefaultDraw(SpriteBatch batch, TileDrawInfo info) { + var region = info.Tileset.GetTextureRegion(info.TilesetTile); + var effects = info.Tile.GetSpriteEffects(); + var drawPos = new Vector2(info.Position.X * info.Renderer.map.TileWidth, info.Position.Y * info.Renderer.map.TileHeight); + batch.Draw(info.Tileset.Texture, drawPos, region, Color.White, 0, Vector2.Zero, 1, effects, info.Depth); + } + /// /// A delegate method used for . /// The idea is to return a depth (between 0 and 1) for the given tile that determines where in the sprite batch it should be rendererd. @@ -182,22 +195,6 @@ namespace MLEM.Extended.Tiled { this.Depth = depth; } - /// - /// Draws this tile draw info with the default settings. - /// - /// The sprite batch to use for drawing - /// The draw function used to draw, or null if there is no override - public void Draw(SpriteBatch batch, DrawDelegate drawFunction) { - if (drawFunction == null) { - var region = this.Tileset.GetTextureRegion(this.TilesetTile); - var effects = this.Tile.GetSpriteEffects(); - var drawPos = new Vector2(this.Position.X * this.Renderer.map.TileWidth, this.Position.Y * this.Renderer.map.TileHeight); - batch.Draw(this.Tileset.Texture, drawPos, region, Color.White, 0, Vector2.Zero, 1, effects, this.Depth); - } else { - drawFunction(batch, this); - } - } - } } diff --git a/MLEM.Extended/Tiled/TiledExtensions.cs b/MLEM.Extended/Tiled/TiledExtensions.cs index 706c1eb..8f2fa7a 100644 --- a/MLEM.Extended/Tiled/TiledExtensions.cs +++ b/MLEM.Extended/Tiled/TiledExtensions.cs @@ -175,6 +175,16 @@ namespace MLEM.Extended.Tiled { return layer != null ? layer.GetTile(x, y) : default; } + /// + /// Returns the tiled map tile at the given location on the layer with the given name. + /// + /// The map + /// The layer position to get the tile at + /// The tile at the given location, or default if the layer does not exist + public static TiledMapTile GetTile(this TiledMap map, LayerPosition pos) { + return map.GetTile(pos.Layer, pos.X, pos.Y); + } + /// /// Sets the tiled map tile at the given location to the given global tile identifier. /// @@ -203,6 +213,28 @@ namespace MLEM.Extended.Tiled { map.SetTile(layerName, x, y, tileset != null && tile != null ? tile.GetGlobalIdentifier(tileset, map) : 0); } + /// + /// Sets the tiled map tile at the given location to the given global tile identifier. + /// + /// The map + /// The layer position + /// The tile's global identifier to set + public static void SetTile(this TiledMap map, LayerPosition pos, int globalTile) { + map.SetTile(pos.Layer, pos.X, pos.Y, globalTile); + } + + /// + /// Sets the tiled map tile at the given location to the given tile from the given tileset. + /// If the passed or is null, the tile at the location is removed instead. + /// + /// The map + /// The layer position + /// The tileset to use, or null to remove the tile + /// The tile to place, from the given tileset, or null to remove the tile + public static void SetTile(this TiledMap map, LayerPosition pos, TiledMapTileset tileset, TiledMapTilesetTile tile) { + map.SetTile(pos.Layer, pos.X, pos.Y, tileset, tile); + } + /// /// For an x and y coordinate, returns an enumerable of all of the tiles on each of the map's . /// diff --git a/MLEM.Extended/Tiled/TiledMapCollisions.cs b/MLEM.Extended/Tiled/TiledMapCollisions.cs index ccfcfbc..e899c78 100644 --- a/MLEM.Extended/Tiled/TiledMapCollisions.cs +++ b/MLEM.Extended/Tiled/TiledMapCollisions.cs @@ -23,30 +23,20 @@ namespace MLEM.Extended.Tiled { /// Creates a new tiled map collision handler for the given map /// /// The map - public TiledMapCollisions(TiledMap map = null) { + /// The function used to collect the collision info of a tile, or null to use + public TiledMapCollisions(TiledMap map = null, CollectCollisions collisionFunction = null) { if (map != null) - this.SetMap(map); + this.SetMap(map, collisionFunction); } /// /// Sets this collision handler's handled map /// /// The map - /// The function used to collect the collision info of a tile, or null for the default handling + /// The function used to collect the collision info of a tile, or null to use public void SetMap(TiledMap map, CollectCollisions collisionFunction = null) { this.map = map; - this.collisionFunction = collisionFunction ?? ((collisions, tile) => { - foreach (var obj in tile.TilesetTile.Objects) { - var area = obj.GetArea(tile.Map); - if (tile.Tile.IsFlippedHorizontally) - area.X = 1 - area.X - area.Width; - if (tile.Tile.IsFlippedVertically) - area.Y = 1 - area.Y - area.Height; - area.Offset(tile.Position); - collisions.Add(area); - } - }); - + this.collisionFunction = collisionFunction ?? DefaultCollectCollisions; this.collisionInfos = new TileCollisionInfo[map.Layers.Count, map.Width, map.Height]; for (var i = 0; i < map.TileLayers.Count; i++) { for (var x = 0; x < map.Width; x++) { @@ -145,6 +135,23 @@ namespace MLEM.Extended.Tiled { } } + /// + /// The default implementation of which is used by if no custom collision collection function is passed + /// + /// The list of collisions to add to + /// The tile's collision information + public static void DefaultCollectCollisions(List collisions, TileCollisionInfo tile) { + foreach (var obj in tile.TilesetTile.Objects) { + var area = obj.GetArea(tile.Map); + if (tile.Tile.IsFlippedHorizontally) + area.X = 1 - area.X - area.Width; + if (tile.Tile.IsFlippedVertically) + area.Y = 1 - area.Y - area.Height; + area.Offset(tile.Position); + collisions.Add(area); + } + } + /// /// A delegate method used to override the default collision checking behavior. ///