mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
added some additional tiled helper methods
This commit is contained in:
parent
1d80965d24
commit
45fc12b0cb
3 changed files with 72 additions and 36 deletions
|
@ -24,7 +24,7 @@ namespace MLEM.Extended.Tiled {
|
||||||
/// Creates a new individual tiled map renderer using the given map and depth function
|
/// Creates a new individual tiled map renderer using the given map and depth function
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="map">The map to use</param>
|
/// <param name="map">The map to use</param>
|
||||||
/// <param name="depthFunction">The depth function to use</param>
|
/// <param name="depthFunction">The depth function to use. Defaults to a function that assigns a depth of 0 to every tile.</param>
|
||||||
public IndividualTiledMapRenderer(TiledMap map = null, GetDepth depthFunction = null) {
|
public IndividualTiledMapRenderer(TiledMap map = null, GetDepth depthFunction = null) {
|
||||||
if (map != null)
|
if (map != null)
|
||||||
this.SetMap(map, depthFunction);
|
this.SetMap(map, depthFunction);
|
||||||
|
@ -34,7 +34,7 @@ namespace MLEM.Extended.Tiled {
|
||||||
/// Sets this individual tiled map renderer's map and depth function
|
/// Sets this individual tiled map renderer's map and depth function
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="map">The map to use</param>
|
/// <param name="map">The map to use</param>
|
||||||
/// <param name="depthFunction">The depth function to use</param>
|
/// <param name="depthFunction">The depth function to use. Defaults to a function that assigns a depth of 0 to every tile.</param>
|
||||||
public void SetMap(TiledMap map, GetDepth depthFunction = null) {
|
public void SetMap(TiledMap map, GetDepth depthFunction = null) {
|
||||||
this.map = map;
|
this.map = map;
|
||||||
this.depthFunction = depthFunction ?? ((tile, layer, layerIndex, position) => 0);
|
this.depthFunction = depthFunction ?? ((tile, layer, layerIndex, position) => 0);
|
||||||
|
@ -84,7 +84,7 @@ namespace MLEM.Extended.Tiled {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="batch">The sprite batch to use</param>
|
/// <param name="batch">The sprite batch to use</param>
|
||||||
/// <param name="frustum">The area that is visible, in pixel space.</param>
|
/// <param name="frustum">The area that is visible, in pixel space.</param>
|
||||||
/// <param name="drawFunction">The draw function to use, or null for default</param>
|
/// <param name="drawFunction">The draw function to use, or null to use <see cref="DefaultDraw"/></param>
|
||||||
public void Draw(SpriteBatch batch, RectangleF? frustum = null, DrawDelegate drawFunction = null) {
|
public void Draw(SpriteBatch batch, RectangleF? frustum = null, DrawDelegate drawFunction = null) {
|
||||||
for (var i = 0; i < this.map.TileLayers.Count; i++) {
|
for (var i = 0; i < this.map.TileLayers.Count; i++) {
|
||||||
if (this.map.TileLayers[i].IsVisible)
|
if (this.map.TileLayers[i].IsVisible)
|
||||||
|
@ -99,8 +99,9 @@ namespace MLEM.Extended.Tiled {
|
||||||
/// <param name="batch">The sprite batch to use</param>
|
/// <param name="batch">The sprite batch to use</param>
|
||||||
/// <param name="layerIndex">The index of the layer in <see cref="TiledMap.TileLayers"/></param>
|
/// <param name="layerIndex">The index of the layer in <see cref="TiledMap.TileLayers"/></param>
|
||||||
/// <param name="frustum">The area that is visible, in pixel space.</param>
|
/// <param name="frustum">The area that is visible, in pixel space.</param>
|
||||||
/// <param name="drawFunction">The draw function to use, or null for default</param>
|
/// <param name="drawFunction">The draw function to use, or null to use <see cref="DefaultDraw"/></param>
|
||||||
public void DrawLayer(SpriteBatch batch, int layerIndex, RectangleF? frustum = null, DrawDelegate drawFunction = null) {
|
public void DrawLayer(SpriteBatch batch, int layerIndex, RectangleF? frustum = null, DrawDelegate drawFunction = null) {
|
||||||
|
var draw = drawFunction ?? DefaultDraw;
|
||||||
var frust = frustum ?? new RectangleF(0, 0, float.MaxValue, float.MaxValue);
|
var frust = frustum ?? new RectangleF(0, 0, float.MaxValue, float.MaxValue);
|
||||||
var minX = Math.Max(0, frust.Left / this.map.TileWidth).Floor();
|
var minX = Math.Max(0, frust.Left / this.map.TileWidth).Floor();
|
||||||
var minY = Math.Max(0, frust.Top / this.map.TileHeight).Floor();
|
var minY = Math.Max(0, frust.Top / this.map.TileHeight).Floor();
|
||||||
|
@ -110,7 +111,7 @@ namespace MLEM.Extended.Tiled {
|
||||||
for (var y = minY; y < maxY; y++) {
|
for (var y = minY; y < maxY; y++) {
|
||||||
var info = this.drawInfos[layerIndex, x, y];
|
var info = this.drawInfos[layerIndex, x, y];
|
||||||
if (info != null)
|
if (info != null)
|
||||||
info.Draw(batch, drawFunction);
|
draw(batch, info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,6 +125,18 @@ namespace MLEM.Extended.Tiled {
|
||||||
animation.Update(time);
|
animation.Update(time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The default implementation of <see cref="DrawDelegate"/> that is used by <see cref="SetMap"/> if no custom draw function is passed
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="batch">The sprite batch to use for drawing</param>
|
||||||
|
/// <param name="info">The <see cref="TileDrawInfo"/> to draw</param>
|
||||||
|
public static void DefaultDraw(SpriteBatch batch, TileDrawInfo info) {
|
||||||
|
var region = info.Tileset.GetTextureRegion(info.TilesetTile);
|
||||||
|
var effects = info.Tile.GetSpriteEffects();
|
||||||
|
var drawPos = new Vector2(info.Position.X * info.Renderer.map.TileWidth, info.Position.Y * info.Renderer.map.TileHeight);
|
||||||
|
batch.Draw(info.Tileset.Texture, drawPos, region, Color.White, 0, Vector2.Zero, 1, effects, info.Depth);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A delegate method used for <see cref="IndividualTiledMapRenderer.depthFunction"/>.
|
/// A delegate method used for <see cref="IndividualTiledMapRenderer.depthFunction"/>.
|
||||||
/// The idea is to return a depth (between 0 and 1) for the given tile that determines where in the sprite batch it should be rendererd.
|
/// The idea is to return a depth (between 0 and 1) for the given tile that determines where in the sprite batch it should be rendererd.
|
||||||
|
@ -182,22 +195,6 @@ namespace MLEM.Extended.Tiled {
|
||||||
this.Depth = depth;
|
this.Depth = depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Draws this tile draw info with the default settings.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="batch">The sprite batch to use for drawing</param>
|
|
||||||
/// <param name="drawFunction">The draw function used to draw, or null if there is no override</param>
|
|
||||||
public void Draw(SpriteBatch batch, DrawDelegate drawFunction) {
|
|
||||||
if (drawFunction == null) {
|
|
||||||
var region = this.Tileset.GetTextureRegion(this.TilesetTile);
|
|
||||||
var effects = this.Tile.GetSpriteEffects();
|
|
||||||
var drawPos = new Vector2(this.Position.X * this.Renderer.map.TileWidth, this.Position.Y * this.Renderer.map.TileHeight);
|
|
||||||
batch.Draw(this.Tileset.Texture, drawPos, region, Color.White, 0, Vector2.Zero, 1, effects, this.Depth);
|
|
||||||
} else {
|
|
||||||
drawFunction(batch, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -175,6 +175,16 @@ namespace MLEM.Extended.Tiled {
|
||||||
return layer != null ? layer.GetTile(x, y) : default;
|
return layer != null ? layer.GetTile(x, y) : default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the tiled map tile at the given location on the layer with the given name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="map">The map</param>
|
||||||
|
/// <param name="pos">The layer position to get the tile at</param>
|
||||||
|
/// <returns>The tile at the given location, or default if the layer does not exist</returns>
|
||||||
|
public static TiledMapTile GetTile(this TiledMap map, LayerPosition pos) {
|
||||||
|
return map.GetTile(pos.Layer, pos.X, pos.Y);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the tiled map tile at the given location to the given global tile identifier.
|
/// Sets the tiled map tile at the given location to the given global tile identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -203,6 +213,28 @@ namespace MLEM.Extended.Tiled {
|
||||||
map.SetTile(layerName, x, y, tileset != null && tile != null ? tile.GetGlobalIdentifier(tileset, map) : 0);
|
map.SetTile(layerName, x, y, tileset != null && tile != null ? tile.GetGlobalIdentifier(tileset, map) : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the tiled map tile at the given location to the given global tile identifier.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="map">The map</param>
|
||||||
|
/// <param name="pos">The layer position</param>
|
||||||
|
/// <param name="globalTile">The tile's global identifier to set</param>
|
||||||
|
public static void SetTile(this TiledMap map, LayerPosition pos, int globalTile) {
|
||||||
|
map.SetTile(pos.Layer, pos.X, pos.Y, globalTile);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the tiled map tile at the given location to the given tile from the given tileset.
|
||||||
|
/// If the passed <paramref name="tileset"/> or <paramref name="tile"/> is null, the tile at the location is removed instead.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="map">The map</param>
|
||||||
|
/// <param name="pos">The layer position</param>
|
||||||
|
/// <param name="tileset">The tileset to use, or null to remove the tile</param>
|
||||||
|
/// <param name="tile">The tile to place, from the given tileset, or null to remove the tile</param>
|
||||||
|
public static void SetTile(this TiledMap map, LayerPosition pos, TiledMapTileset tileset, TiledMapTilesetTile tile) {
|
||||||
|
map.SetTile(pos.Layer, pos.X, pos.Y, tileset, tile);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// For an x and y coordinate, returns an enumerable of all of the tiles on each of the map's <see cref="TiledMap.TileLayers"/>.
|
/// For an x and y coordinate, returns an enumerable of all of the tiles on each of the map's <see cref="TiledMap.TileLayers"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -23,30 +23,20 @@ namespace MLEM.Extended.Tiled {
|
||||||
/// Creates a new tiled map collision handler for the given map
|
/// Creates a new tiled map collision handler for the given map
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="map">The map</param>
|
/// <param name="map">The map</param>
|
||||||
public TiledMapCollisions(TiledMap map = null) {
|
/// <param name="collisionFunction">The function used to collect the collision info of a tile, or null to use <see cref="DefaultCollectCollisions"/></param>
|
||||||
|
public TiledMapCollisions(TiledMap map = null, CollectCollisions collisionFunction = null) {
|
||||||
if (map != null)
|
if (map != null)
|
||||||
this.SetMap(map);
|
this.SetMap(map, collisionFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets this collision handler's handled map
|
/// Sets this collision handler's handled map
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="map">The map</param>
|
/// <param name="map">The map</param>
|
||||||
/// <param name="collisionFunction">The function used to collect the collision info of a tile, or null for the default handling</param>
|
/// <param name="collisionFunction">The function used to collect the collision info of a tile, or null to use <see cref="DefaultCollectCollisions"/></param>
|
||||||
public void SetMap(TiledMap map, CollectCollisions collisionFunction = null) {
|
public void SetMap(TiledMap map, CollectCollisions collisionFunction = null) {
|
||||||
this.map = map;
|
this.map = map;
|
||||||
this.collisionFunction = collisionFunction ?? ((collisions, tile) => {
|
this.collisionFunction = collisionFunction ?? DefaultCollectCollisions;
|
||||||
foreach (var obj in tile.TilesetTile.Objects) {
|
|
||||||
var area = obj.GetArea(tile.Map);
|
|
||||||
if (tile.Tile.IsFlippedHorizontally)
|
|
||||||
area.X = 1 - area.X - area.Width;
|
|
||||||
if (tile.Tile.IsFlippedVertically)
|
|
||||||
area.Y = 1 - area.Y - area.Height;
|
|
||||||
area.Offset(tile.Position);
|
|
||||||
collisions.Add(area);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
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++) {
|
||||||
for (var x = 0; x < map.Width; x++) {
|
for (var x = 0; x < map.Width; x++) {
|
||||||
|
@ -145,6 +135,23 @@ namespace MLEM.Extended.Tiled {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The default implementation of <see cref="CollectCollisions"/> which is used by <see cref="SetMap"/> if no custom collision collection function is passed
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="collisions">The list of collisions to add to</param>
|
||||||
|
/// <param name="tile">The tile's collision information</param>
|
||||||
|
public static void DefaultCollectCollisions(List<RectangleF> collisions, TileCollisionInfo tile) {
|
||||||
|
foreach (var obj in tile.TilesetTile.Objects) {
|
||||||
|
var area = obj.GetArea(tile.Map);
|
||||||
|
if (tile.Tile.IsFlippedHorizontally)
|
||||||
|
area.X = 1 - area.X - area.Width;
|
||||||
|
if (tile.Tile.IsFlippedVertically)
|
||||||
|
area.Y = 1 - area.Y - area.Height;
|
||||||
|
area.Offset(tile.Position);
|
||||||
|
collisions.Add(area);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A delegate method used to override the default collision checking behavior.
|
/// A delegate method used to override the default collision checking behavior.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in a new issue