1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

some more tiled utilities

This commit is contained in:
Ellpeck 2020-04-29 22:14:33 +02:00
parent 35f53a978b
commit 36b7a16e24
2 changed files with 56 additions and 29 deletions

View file

@ -57,13 +57,12 @@ namespace MLEM.Extended.Tiled {
this.drawInfos[layerIndex, x, y] = new TileDrawInfo(this, tile, tileset, tilesetTile, pos, depth); this.drawInfos[layerIndex, x, y] = new TileDrawInfo(this, tile, tileset, tilesetTile, pos, depth);
} }
public void Draw(SpriteBatch batch, RectangleF? frustum = null) { public void Draw(SpriteBatch batch, RectangleF? frustum = null, DrawDelegate drawFunction = null) {
for (var i = 0; i < this.map.TileLayers.Count; i++) { for (var i = 0; i < this.map.TileLayers.Count; i++)
this.DrawLayer(batch, i, frustum); this.DrawLayer(batch, i, frustum, drawFunction);
}
} }
public void DrawLayer(SpriteBatch batch, int layerIndex, RectangleF? frustum = null) { public void DrawLayer(SpriteBatch batch, int layerIndex, RectangleF? frustum = null, DrawDelegate drawFunction = null) {
var frust = frustum ?? new RectangleF(0, 0, float.MaxValue, float.MaxValue); var frust = frustum ?? new RectangleF(0, 0, float.MaxValue, float.MaxValue);
var minX = Math.Max(0, frust.Left / this.map.TileWidth).Floor(); var minX = Math.Max(0, frust.Left / this.map.TileWidth).Floor();
var minY = Math.Max(0, frust.Top / this.map.TileHeight).Floor(); var minY = Math.Max(0, frust.Top / this.map.TileHeight).Floor();
@ -73,7 +72,7 @@ namespace MLEM.Extended.Tiled {
for (var y = minY; y < maxY; y++) { for (var y = minY; y < maxY; y++) {
var info = this.drawInfos[layerIndex, x, y]; var info = this.drawInfos[layerIndex, x, y];
if (info != null) if (info != null)
info.Draw(batch); info.Draw(batch, drawFunction);
} }
} }
} }
@ -85,34 +84,35 @@ namespace MLEM.Extended.Tiled {
public delegate float GetDepth(TiledMapTile tile, TiledMapTileLayer layer, int layerIndex, Point position); public delegate float GetDepth(TiledMapTile tile, TiledMapTileLayer layer, int layerIndex, Point position);
private class TileDrawInfo : GenericDataHolder { public delegate void DrawDelegate(SpriteBatch batch, TileDrawInfo info);
private readonly IndividualTiledMapRenderer renderer; public class TileDrawInfo : GenericDataHolder {
private readonly TiledMapTileset tileset;
private readonly TiledMapTilesetTile tilesetTile; public readonly IndividualTiledMapRenderer Renderer;
private readonly Point position; public readonly TiledMapTile Tile;
private readonly float depth; public readonly TiledMapTileset Tileset;
private readonly SpriteEffects flipping; public readonly TiledMapTilesetTile TilesetTile;
public readonly Point Position;
public readonly float Depth;
public TileDrawInfo(IndividualTiledMapRenderer renderer, TiledMapTile tile, TiledMapTileset tileset, TiledMapTilesetTile tilesetTile, Point position, float depth) { public TileDrawInfo(IndividualTiledMapRenderer renderer, TiledMapTile tile, TiledMapTileset tileset, TiledMapTilesetTile tilesetTile, Point position, float depth) {
this.renderer = renderer; this.Renderer = renderer;
this.tileset = tileset; this.Tile = tile;
this.tilesetTile = tilesetTile; this.Tileset = tileset;
this.position = position; this.TilesetTile = tilesetTile;
this.depth = depth; this.Position = position;
this.Depth = depth;
if (tile.IsFlippedHorizontally)
this.flipping |= SpriteEffects.FlipHorizontally;
if (tile.IsFlippedVertically)
this.flipping |= SpriteEffects.FlipVertically;
} }
public void Draw(SpriteBatch batch) { public void Draw(SpriteBatch batch, DrawDelegate drawFunction) {
var id = this.tilesetTile.LocalTileIdentifier; if (drawFunction == null) {
if (this.tilesetTile is TiledMapTilesetAnimatedTile animated) var region = this.Tileset.GetTextureRegion(this.TilesetTile);
id = animated.CurrentAnimationFrame.LocalTileIdentifier; var effects = this.Tile.GetSpriteEffects();
var drawPos = new Vector2(this.position.X * this.renderer.map.TileWidth, this.position.Y * this.renderer.map.TileHeight); var drawPos = new Vector2(this.Position.X * this.Renderer.map.TileWidth, this.Position.Y * this.Renderer.map.TileHeight);
batch.Draw(this.tileset.Texture, drawPos, this.tileset.GetTileRegion(id), Color.White, 0, Vector2.Zero, 1, this.flipping, this.depth); batch.Draw(this.Tileset.Texture, drawPos, region, Color.White, 0, Vector2.Zero, 1, effects, this.Depth);
} else {
drawFunction(batch, this);
}
} }
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended; using MonoGame.Extended;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
@ -43,6 +44,10 @@ namespace MLEM.Extended.Tiled {
return tile.GlobalIdentifier - map.GetTilesetFirstGlobalIdentifier(tileset); return tile.GlobalIdentifier - map.GetTilesetFirstGlobalIdentifier(tileset);
} }
public static int GetGlobalIdentifier(this TiledMapTilesetTile tile, TiledMapTileset tileset, TiledMap map) {
return map.GetTilesetFirstGlobalIdentifier(tileset) + tile.LocalTileIdentifier;
}
public static TiledMapTilesetTile GetTilesetTile(this TiledMapTileset tileset, TiledMapTile tile, TiledMap map, bool createStub = true) { public static TiledMapTilesetTile GetTilesetTile(this TiledMapTileset tileset, TiledMapTile tile, TiledMap map, bool createStub = true) {
if (tile.IsBlank) if (tile.IsBlank)
return null; return null;
@ -75,6 +80,12 @@ namespace MLEM.Extended.Tiled {
return layer != null ? layer.GetTile(x, y) : default; return layer != null ? layer.GetTile(x, y) : default;
} }
public static void SetTile(this TiledMap map, string layerName, int x, int y, int globalTile) {
var layer = map.GetLayer<TiledMapTileLayer>(layerName);
if (layer != null)
layer.SetTile((ushort) x, (ushort) y, (uint) globalTile);
}
public static IEnumerable<TiledMapTile> GetTiles(this TiledMap map, int x, int y) { public static IEnumerable<TiledMapTile> GetTiles(this TiledMap map, int x, int y) {
foreach (var layer in map.TileLayers) { foreach (var layer in map.TileLayers) {
var tile = layer.GetTile(x, y); var tile = layer.GetTile(x, y);
@ -115,5 +126,21 @@ namespace MLEM.Extended.Tiled {
} }
} }
public static Rectangle GetTextureRegion(this TiledMapTileset tileset, TiledMapTilesetTile tile) {
var id = tile.LocalTileIdentifier;
if (tile is TiledMapTilesetAnimatedTile animated)
id = animated.CurrentAnimationFrame.LocalTileIdentifier;
return tileset.GetTileRegion(id);
}
public static SpriteEffects GetSpriteEffects(this TiledMapTile tile) {
var flipping = SpriteEffects.None;
if (tile.IsFlippedHorizontally)
flipping |= SpriteEffects.FlipHorizontally;
if (tile.IsFlippedVertically)
flipping |= SpriteEffects.FlipVertically;
return flipping;
}
} }
} }