using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Cameras; using MLEM.Extensions; using MLEM.Graphics; using MLEM.Misc; using MonoGame.Extended.Tiled; using RectangleF = MonoGame.Extended.RectangleF; namespace MLEM.Extended.Tiled { /// /// A tiled map renderer that renders each tile individually, while optionally supplying a depth for each tile. /// Rendering in this manner allows for entities to be behind or in front of buildings based on their y height. /// public class IndividualTiledMapRenderer { private TiledMap map; private TileDrawInfo[,,] drawInfos; private GetDepth depthFunction; private List animatedTiles; /// /// Creates a new individual tiled map renderer using the given map and depth function /// /// The map 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); } /// /// Sets this individual tiled map renderer's map and depth function /// /// The map 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); this.drawInfos = new TileDrawInfo[map.TileLayers.Count, map.Width, map.Height]; for (var i = 0; i < map.TileLayers.Count; i++) { for (var x = 0; x < map.Width; x++) { for (var y = 0; y < map.Height; y++) { this.UpdateDrawInfo(i, x, y); } } } this.animatedTiles = new List(); foreach (var tileset in map.Tilesets) { foreach (var tile in tileset.Tiles) { if (tile is TiledMapTilesetAnimatedTile animated) { this.animatedTiles.Add(animated); } } } } /// /// Updates the rendering information for the tile in the given layer, x and y. /// /// The index of the layer in /// The x coordinate of the tile /// The y coordinate of the tile public void UpdateDrawInfo(int layerIndex, int x, int y) { var layer = this.map.TileLayers[layerIndex]; var tile = layer.GetTile(x, y); if (tile.IsBlank) { this.drawInfos[layerIndex, x, y] = null; return; } var tileset = tile.GetTileset(this.map); var tilesetTile = tileset.GetTilesetTile(tile, this.map); var pos = new Point(x, y); var depth = this.depthFunction(tile, layer, layerIndex, pos); this.drawInfos[layerIndex, x, y] = new TileDrawInfo(this, tile, tileset, tilesetTile, pos, depth); } /// /// Draws this individual tiled map renderer. /// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. provides for this purpose. /// /// The sprite batch to use /// The area that is visible, in pixel space. /// 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) this.DrawLayer(batch, i, frustum, drawFunction); } } /// /// Adds this individual tiled map renderer to the given . /// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. provides for this purpose. /// /// The static sprite batch to use for drawing. /// The area that is visible, in pixel space. /// The add function to use, or null to use . public void Add(StaticSpriteBatch batch, RectangleF? frustum = null, AddDelegate addFunction = null) { for (var i = 0; i < this.map.TileLayers.Count; i++) { if (this.map.TileLayers[i].IsVisible) this.AddLayer(batch, i, frustum, addFunction); } } /// /// Draws the given layer of this individual tiled map renderer. /// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. provides for this purpose. /// /// 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 to use public void DrawLayer(SpriteBatch batch, int layerIndex, RectangleF? frustum = null, DrawDelegate drawFunction = null) { var draw = drawFunction ?? IndividualTiledMapRenderer.DefaultDraw; var (minX, minY, maxX, maxY) = this.GetFrustum(frustum); for (var x = minX; x < maxX; x++) { for (var y = minY; y < maxY; y++) { var info = this.drawInfos[layerIndex, x, y]; if (info != null) draw(batch, info); } } } /// /// Adds the given layer of this individual tiled map renderer to the given . /// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. provides for this purpose. /// /// The static sprite batch to use for drawing. /// The index of the layer in . /// The area that is visible, in pixel space. /// The add function to use, or null to use . public void AddLayer(StaticSpriteBatch batch, int layerIndex, RectangleF? frustum = null, AddDelegate addFunction = null) { var add = addFunction ?? IndividualTiledMapRenderer.DefaultAdd; var (minX, minY, maxX, maxY) = this.GetFrustum(frustum); for (var x = minX; x < maxX; x++) { for (var y = minY; y < maxY; y++) { var info = this.drawInfos[layerIndex, x, y]; if (info != null) add(batch, info); } } } /// /// Update all of the animated tiles in this individual tiled map renderer /// /// The game's time public void UpdateAnimations(GameTime time) { foreach (var animation in this.animatedTiles) animation.Update(time); } private (int MinX, int MinY, int MaxX, int MaxY) GetFrustum(RectangleF? frustum) { 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(); var maxX = Math.Min(this.map.Width, frust.Right / this.map.TileWidth).Ceil(); var maxY = Math.Min(this.map.Height, frust.Bottom / this.map.TileHeight).Ceil(); return (minX, minY, maxX, maxY); } /// /// 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); } /// /// The default implementation of that is used by if no custom add function is passed. /// /// The static sprite batch to use for drawing. /// The to add. public static void DefaultAdd(StaticSpriteBatch 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.Add(info.Tileset.Texture, drawPos, region, Color.White, 0, Vector2.Zero, 1, effects, info.Depth); } /// /// A delegate method used for an 's depth function. /// 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. /// Note that, for this depth function to take effect, the sprite batch needs to begin with or . /// /// The tile whose depth to get /// The layer the tile is on /// The index of the layer in /// The tile position of this tile public delegate float GetDepth(TiledMapTile tile, TiledMapTileLayer layer, int layerIndex, Point position); /// /// A delegate method used for drawing an . /// /// The sprite batch to use for drawing /// The to draw public delegate void DrawDelegate(SpriteBatch batch, TileDrawInfo info); /// /// A delegate method used for adding an to a . /// /// The static sprite batch to use for drawing. /// The to add. public delegate void AddDelegate(StaticSpriteBatch batch, TileDrawInfo info); /// /// A tile draw info contains information about a tile at a given map location. /// It caches a lot of data that is required for drawing a tile efficiently. /// public class TileDrawInfo : GenericDataHolder { /// /// The renderer used by this info /// public readonly IndividualTiledMapRenderer Renderer; /// /// The tiled map tile to draw /// public readonly TiledMapTile Tile; /// /// The tileset that is on /// public readonly TiledMapTileset Tileset; /// /// The tileset tile that corresponds to /// public readonly TiledMapTilesetTile TilesetTile; /// /// The position, in tile space, of /// public readonly Point Position; /// /// The depth calculated by the depth function /// public readonly float Depth; internal TileDrawInfo(IndividualTiledMapRenderer renderer, TiledMapTile tile, TiledMapTileset tileset, TiledMapTilesetTile tilesetTile, Point position, float depth) { this.Renderer = renderer; this.Tile = tile; this.Tileset = tileset; this.TilesetTile = tilesetTile; this.Position = position; this.Depth = depth; } } } }