diff --git a/MLEM.Extended/Tiled/TiledMapCollisions.cs b/MLEM.Extended/Tiled/TiledMapCollisions.cs index 3eb8065..09fa655 100644 --- a/MLEM.Extended/Tiled/TiledMapCollisions.cs +++ b/MLEM.Extended/Tiled/TiledMapCollisions.cs @@ -12,14 +12,27 @@ namespace MLEM.Extended.Tiled { private TiledMap map; private TileCollisionInfo[,,] collisionInfos; + private CollectCollisions collisionFunction; public TiledMapCollisions(TiledMap map = null) { if (map != null) this.SetMap(map); } - public void SetMap(TiledMap map) { + 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.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++) { @@ -38,7 +51,7 @@ namespace MLEM.Extended.Tiled { return; } var tilesetTile = tile.GetTilesetTile(this.map); - this.collisionInfos[layerIndex, x, y] = new TileCollisionInfo(this.map, new Vector2(x, y), tile, layer, tilesetTile); + this.collisionInfos[layerIndex, x, y] = new TileCollisionInfo(this.map, new Vector2(x, y), tile, layer, tilesetTile, this.collisionFunction); } public IEnumerable GetCollidingTiles(RectangleF area, Func included = null) { @@ -64,22 +77,26 @@ namespace MLEM.Extended.Tiled { return this.GetCollidingTiles(area, included).Any(); } + public delegate void CollectCollisions(List collisions, TileCollisionInfo tile); + public class TileCollisionInfo { + public readonly TiledMap Map; + public readonly Vector2 Position; public readonly TiledMapTile Tile; public readonly TiledMapTileLayer Layer; public readonly TiledMapTilesetTile TilesetTile; - public readonly ReadOnlyCollection Collisions; + public readonly List Collisions; - public TileCollisionInfo(TiledMap map, Vector2 position, TiledMapTile tile, TiledMapTileLayer layer, TiledMapTilesetTile tilesetTile) { + public TileCollisionInfo(TiledMap map, Vector2 position, TiledMapTile tile, TiledMapTileLayer layer, TiledMapTilesetTile tilesetTile, CollectCollisions collisionFunction) { this.TilesetTile = tilesetTile; this.Layer = layer; this.Tile = tile; + this.Map = map; + this.Position = position; - var collisions = new List(); - foreach (var obj in tilesetTile.Objects) - collisions.Add(obj.GetArea(map, position)); - this.Collisions = collisions.AsReadOnly(); + this.Collisions = new List(); + collisionFunction(this.Collisions, this); } } diff --git a/Sandbox/Content/Tiled/Tileset.tsx b/Sandbox/Content/Tiled/Tileset.tsx index 4109ccb..ea29feb 100644 --- a/Sandbox/Content/Tiled/Tileset.tsx +++ b/Sandbox/Content/Tiled/Tileset.tsx @@ -1,5 +1,5 @@ - + @@ -16,6 +16,16 @@ + + + + + + + + + + @@ -72,6 +82,11 @@ + + + + + diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index e9dee12..2c76ec6 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -12,6 +12,7 @@ using MLEM.Textures; using MLEM.Ui; using MLEM.Ui.Elements; using MLEM.Ui.Style; +using MonoGame.Extended; using MonoGame.Extended.Tiled; namespace Sandbox { @@ -20,6 +21,7 @@ namespace Sandbox { private Camera camera; private TiledMap map; private IndividualTiledMapRenderer mapRenderer; + private TiledMapCollisions collisions; public GameImpl() { this.IsMouseVisible = true; @@ -33,6 +35,7 @@ namespace Sandbox { this.map = LoadContent("Tiled/Map"); this.mapRenderer = new IndividualTiledMapRenderer(this.map); + this.collisions = new TiledMapCollisions(this.map); this.camera = new Camera(this.GraphicsDevice) { AutoScaleWithScreen = true, @@ -74,6 +77,13 @@ namespace Sandbox { this.GraphicsDevice.Clear(Color.Black); this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, this.camera.ViewMatrix); this.mapRenderer.Draw(this.SpriteBatch, this.camera.GetVisibleRectangle().ToExtended()); + + foreach (var tile in this.collisions.GetCollidingTiles(new RectangleF(0, 0, this.map.Width, this.map.Height))) { + foreach (var area in tile.Collisions) { + this.SpriteBatch.DrawRectangle(area.Position * this.map.GetTileSize(), area.Size * this.map.GetTileSize(), Color.Red); + } + } + this.SpriteBatch.End(); base.DoDraw(gameTime); }