1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

move out updating draw and collision info so that, when a tile on the map gets changed, they can be called individually

This commit is contained in:
Ellpeck 2019-09-19 11:11:47 +02:00
parent 6f8a414ee2
commit 16e7212190
2 changed files with 27 additions and 23 deletions

View file

@ -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,27 +18,29 @@ 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++) {
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)
continue;
var tileset = tile.GetTileset(map);
var source = tileset.GetTileRegion(tile.GetLocalIdentifier(tileset, map));
return;
var tileset = tile.GetTileset(this.map);
var source = tileset.GetTileRegion(tile.GetLocalIdentifier(tileset, this.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);
}
}
}
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) {

View file

@ -19,25 +19,26 @@ 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++) {
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)
continue;
var tilesetTile = tile.GetTilesetTile(map);
return;
var tilesetTile = tile.GetTilesetTile(this.map);
if (tilesetTile == null)
continue;
this.collisionInfos[i, x, y] = new TileCollisionInfo(map, new Vector2(x, y), tile, tilesetTile);
}
}
}
return;
this.collisionInfos[layerIndex, x, y] = new TileCollisionInfo(this.map, new Vector2(x, y), tile, tilesetTile);
}
public IEnumerable<TileCollisionInfo> GetCollidingTiles(RectangleF area, Func<TileCollisionInfo, bool> included = null) {