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:
parent
6f8a414ee2
commit
16e7212190
2 changed files with 27 additions and 23 deletions
|
@ -10,6 +10,7 @@ namespace MLEM.Extended.Tiled {
|
||||||
|
|
||||||
private TiledMap map;
|
private TiledMap map;
|
||||||
private TileDrawInfo[,,] drawInfos;
|
private TileDrawInfo[,,] drawInfos;
|
||||||
|
private GetDepth depthFunction;
|
||||||
|
|
||||||
public IndividualTiledMapRenderer(TiledMap map = null, GetDepth depthFunction = null) {
|
public IndividualTiledMapRenderer(TiledMap map = null, GetDepth depthFunction = null) {
|
||||||
if (map != null)
|
if (map != null)
|
||||||
|
@ -17,29 +18,31 @@ namespace MLEM.Extended.Tiled {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetMap(TiledMap map, GetDepth depthFunction = null) {
|
public void SetMap(TiledMap map, GetDepth depthFunction = null) {
|
||||||
if (this.map == map)
|
|
||||||
return;
|
|
||||||
this.map = map;
|
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];
|
this.drawInfos = new TileDrawInfo[map.TileLayers.Count, map.Width, map.Height];
|
||||||
for (var i = 0; i < map.TileLayers.Count; i++) {
|
for (var i = 0; i < map.TileLayers.Count; i++) {
|
||||||
var layer = map.TileLayers[i];
|
|
||||||
for (var x = 0; x < map.Width; x++) {
|
for (var x = 0; x < map.Width; x++) {
|
||||||
for (var y = 0; y < map.Height; y++) {
|
for (var y = 0; y < map.Height; y++) {
|
||||||
var tile = layer.GetTile((ushort) x, (ushort) y);
|
this.UpdateDrawInfo(i, x, 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
public void Draw(SpriteBatch batch, RectangleF? frustum = 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);
|
||||||
|
|
|
@ -19,27 +19,28 @@ namespace MLEM.Extended.Tiled {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetMap(TiledMap map) {
|
public void SetMap(TiledMap map) {
|
||||||
if (this.map == map)
|
|
||||||
return;
|
|
||||||
this.map = map;
|
this.map = map;
|
||||||
|
|
||||||
this.collisionInfos = new TileCollisionInfo[map.Layers.Count, map.Width, map.Height];
|
this.collisionInfos = new TileCollisionInfo[map.Layers.Count, map.Width, map.Height];
|
||||||
for (var i = 0; i < map.TileLayers.Count; i++) {
|
for (var i = 0; i < map.TileLayers.Count; i++) {
|
||||||
var layer = map.TileLayers[i];
|
|
||||||
for (var x = 0; x < map.Width; x++) {
|
for (var x = 0; x < map.Width; x++) {
|
||||||
for (var y = 0; y < map.Height; y++) {
|
for (var y = 0; y < map.Height; y++) {
|
||||||
var tile = layer.GetTile((ushort) x, (ushort) y);
|
this.UpdateCollisionInfo(i, x, 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
public IEnumerable<TileCollisionInfo> GetCollidingTiles(RectangleF area, Func<TileCollisionInfo, bool> included = null) {
|
||||||
var inclusionFunc = included ?? (tile => tile.Collisions.Any(c => c.Intersects(area)));
|
var inclusionFunc = included ?? (tile => tile.Collisions.Any(c => c.Intersects(area)));
|
||||||
for (var i = 0; i < this.map.TileLayers.Count; i++) {
|
for (var i = 0; i < this.map.TileLayers.Count; i++) {
|
||||||
|
|
Loading…
Reference in a new issue