1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-24 01:09:23 +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,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);

View file

@ -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<TileCollisionInfo> GetCollidingTiles(RectangleF area, Func<TileCollisionInfo, bool> included = null) {
var inclusionFunc = included ?? (tile => tile.Collisions.Any(c => c.Intersects(area)));
for (var i = 0; i < this.map.TileLayers.Count; i++) {