mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +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,27 +18,29 @@ 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++) {
|
||||||
|
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);
|
var tile = layer.GetTile((ushort) x, (ushort) y);
|
||||||
if (tile.IsBlank)
|
if (tile.IsBlank)
|
||||||
continue;
|
return;
|
||||||
var tileset = tile.GetTileset(map);
|
var tileset = tile.GetTileset(this.map);
|
||||||
var source = tileset.GetTileRegion(tile.GetLocalIdentifier(tileset, map));
|
var source = tileset.GetTileRegion(tile.GetLocalIdentifier(tileset, this.map));
|
||||||
var pos = new Point(x, y);
|
var pos = new Point(x, y);
|
||||||
var depth = depthFunc(tile, layer, i, pos);
|
var depth = this.depthFunction(tile, layer, layerIndex, pos);
|
||||||
this.drawInfos[i, x, y] = new TileDrawInfo(this, tileset, pos, source, depth);
|
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) {
|
||||||
|
|
|
@ -19,25 +19,26 @@ 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++) {
|
||||||
|
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);
|
var tile = layer.GetTile((ushort) x, (ushort) y);
|
||||||
if (tile.IsBlank)
|
if (tile.IsBlank)
|
||||||
continue;
|
return;
|
||||||
var tilesetTile = tile.GetTilesetTile(map);
|
var tilesetTile = tile.GetTilesetTile(this.map);
|
||||||
if (tilesetTile == null)
|
if (tilesetTile == null)
|
||||||
continue;
|
return;
|
||||||
this.collisionInfos[i, x, y] = new TileCollisionInfo(map, new Vector2(x, y), tile, tilesetTile);
|
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) {
|
||||||
|
|
Loading…
Reference in a new issue