From 16e72121903a8d90ec79d1d1fef5916d5b7bfef0 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 19 Sep 2019 11:11:47 +0200 Subject: [PATCH] move out updating draw and collision info so that, when a tile on the map gets changed, they can be called individually --- .../Tiled/IndividualTiledMapRenderer.cs | 27 ++++++++++--------- MLEM.Extended/Tiled/TiledMapCollisions.cs | 23 ++++++++-------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs b/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs index 388392c..9307fa9 100644 --- a/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs +++ b/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs @@ -10,6 +10,7 @@ namespace MLEM.Extended.Tiled { private TiledMap map; private TileDrawInfo[,,] drawInfos; + private GetDepth depthFunction; public IndividualTiledMapRenderer(TiledMap map = null, GetDepth depthFunction = null) { if (map != null) @@ -17,29 +18,31 @@ namespace MLEM.Extended.Tiled { } public void SetMap(TiledMap map, GetDepth depthFunction = null) { - if (this.map == map) - return; this.map = map; - var depthFunc = depthFunction ?? ((tile, layer, layerIndex, position) => 0); + 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++) { - var layer = map.TileLayers[i]; for (var x = 0; x < map.Width; x++) { for (var y = 0; y < map.Height; y++) { - var tile = layer.GetTile((ushort) x, (ushort) y); - if (tile.IsBlank) - continue; - var tileset = tile.GetTileset(map); - var source = tileset.GetTileRegion(tile.GetLocalIdentifier(tileset, map)); - var pos = new Point(x, y); - var depth = depthFunc(tile, layer, i, pos); - this.drawInfos[i, x, y] = new TileDrawInfo(this, tileset, pos, source, depth); + this.UpdateDrawInfo(i, x, y); } } } } + public void UpdateDrawInfo(int layerIndex, int x, int y) { + var layer = this.map.TileLayers[layerIndex]; + var tile = layer.GetTile((ushort) x, (ushort) y); + if (tile.IsBlank) + return; + var tileset = tile.GetTileset(this.map); + var source = tileset.GetTileRegion(tile.GetLocalIdentifier(tileset, this.map)); + var pos = new Point(x, y); + var depth = this.depthFunction(tile, layer, layerIndex, pos); + this.drawInfos[layerIndex, x, y] = new TileDrawInfo(this, tileset, pos, source, depth); + } + public void Draw(SpriteBatch batch, RectangleF? frustum = null) { for (var i = 0; i < this.map.TileLayers.Count; i++) { this.DrawLayer(batch, i, frustum); diff --git a/MLEM.Extended/Tiled/TiledMapCollisions.cs b/MLEM.Extended/Tiled/TiledMapCollisions.cs index a4fdc3d..559c72c 100644 --- a/MLEM.Extended/Tiled/TiledMapCollisions.cs +++ b/MLEM.Extended/Tiled/TiledMapCollisions.cs @@ -19,27 +19,28 @@ namespace MLEM.Extended.Tiled { } public void SetMap(TiledMap map) { - if (this.map == map) - return; this.map = map; - this.collisionInfos = new TileCollisionInfo[map.Layers.Count, map.Width, map.Height]; for (var i = 0; i < map.TileLayers.Count; i++) { - var layer = map.TileLayers[i]; for (var x = 0; x < map.Width; x++) { for (var y = 0; y < map.Height; y++) { - var tile = layer.GetTile((ushort) x, (ushort) y); - if (tile.IsBlank) - continue; - var tilesetTile = tile.GetTilesetTile(map); - if (tilesetTile == null) - continue; - this.collisionInfos[i, x, y] = new TileCollisionInfo(map, new Vector2(x, y), tile, tilesetTile); + this.UpdateCollisionInfo(i, x, y); } } } } + public void UpdateCollisionInfo(int layerIndex, int x, int y) { + var layer = this.map.TileLayers[layerIndex]; + var tile = layer.GetTile((ushort) x, (ushort) y); + if (tile.IsBlank) + return; + var tilesetTile = tile.GetTilesetTile(this.map); + if (tilesetTile == null) + return; + this.collisionInfos[layerIndex, x, y] = new TileCollisionInfo(this.map, new Vector2(x, y), tile, tilesetTile); + } + public IEnumerable GetCollidingTiles(RectangleF area, Func included = null) { var inclusionFunc = included ?? (tile => tile.Collisions.Any(c => c.Intersects(area))); for (var i = 0; i < this.map.TileLayers.Count; i++) {