1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

made the individual tile map renderer show animations

This commit is contained in:
Ellpeck 2019-12-07 02:24:02 +01:00
parent f93117622c
commit 7952ba1582

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions; using MLEM.Extensions;
@ -11,6 +12,7 @@ namespace MLEM.Extended.Tiled {
private TiledMap map; private TiledMap map;
private TileDrawInfo[,,] drawInfos; private TileDrawInfo[,,] drawInfos;
private GetDepth depthFunction; private GetDepth depthFunction;
private List<TiledMapTilesetAnimatedTile> animatedTiles;
public IndividualTiledMapRenderer(TiledMap map = null, GetDepth depthFunction = null) { public IndividualTiledMapRenderer(TiledMap map = null, GetDepth depthFunction = null) {
if (map != null) if (map != null)
@ -29,6 +31,15 @@ namespace MLEM.Extended.Tiled {
} }
} }
} }
this.animatedTiles = new List<TiledMapTilesetAnimatedTile>();
foreach (var tileset in map.Tilesets) {
foreach (var tile in tileset.Tiles) {
if (tile is TiledMapTilesetAnimatedTile animated) {
this.animatedTiles.Add(animated);
}
}
}
} }
public void UpdateDrawInfo(int layerIndex, int x, int y) { public void UpdateDrawInfo(int layerIndex, int x, int y) {
@ -39,10 +50,10 @@ namespace MLEM.Extended.Tiled {
return; return;
} }
var tileset = tile.GetTileset(this.map); var tileset = tile.GetTileset(this.map);
var source = tileset.GetTileRegion(tile.GetLocalIdentifier(tileset, this.map)); var tilesetTile = tileset.GetTilesetTile(tile, this.map);
var pos = new Point(x, y); var pos = new Point(x, y);
var depth = this.depthFunction(tile, layer, layerIndex, pos); var depth = this.depthFunction(tile, layer, layerIndex, pos);
this.drawInfos[layerIndex, x, y] = new TileDrawInfo(this, tileset, pos, source, depth); this.drawInfos[layerIndex, x, y] = new TileDrawInfo(this, tileset, tilesetTile, pos, depth);
} }
public void Draw(SpriteBatch batch, RectangleF? frustum = null) { public void Draw(SpriteBatch batch, RectangleF? frustum = null) {
@ -66,27 +77,35 @@ namespace MLEM.Extended.Tiled {
} }
} }
public void UpdateAnimations(GameTime time) {
foreach (var animation in this.animatedTiles)
animation.Update(time);
}
public delegate float GetDepth(TiledMapTile tile, TiledMapTileLayer layer, int layerIndex, Point position); public delegate float GetDepth(TiledMapTile tile, TiledMapTileLayer layer, int layerIndex, Point position);
private class TileDrawInfo { private class TileDrawInfo {
private readonly IndividualTiledMapRenderer renderer; private readonly IndividualTiledMapRenderer renderer;
private readonly TiledMapTileset tileset; private readonly TiledMapTileset tileset;
private readonly TiledMapTilesetTile tile;
private readonly Point position; private readonly Point position;
private readonly Rectangle source;
private readonly float depth; private readonly float depth;
public TileDrawInfo(IndividualTiledMapRenderer renderer, TiledMapTileset tileset, Point position, Rectangle source, float depth) { public TileDrawInfo(IndividualTiledMapRenderer renderer, TiledMapTileset tileset, TiledMapTilesetTile tile, Point position, float depth) {
this.renderer = renderer; this.renderer = renderer;
this.tileset = tileset; this.tileset = tileset;
this.tile = tile;
this.position = position; this.position = position;
this.source = source;
this.depth = depth; this.depth = depth;
} }
public void Draw(SpriteBatch batch) { public void Draw(SpriteBatch batch) {
var id = this.tile.LocalTileIdentifier;
if (this.tile is TiledMapTilesetAnimatedTile animated)
id = animated.CurrentAnimationFrame.LocalTileIdentifier;
var drawPos = new Vector2(this.position.X * this.renderer.map.TileWidth, this.position.Y * this.renderer.map.TileHeight); var drawPos = new Vector2(this.position.X * this.renderer.map.TileWidth, this.position.Y * this.renderer.map.TileHeight);
batch.Draw(this.tileset.Texture, drawPos, this.source, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, this.depth); batch.Draw(this.tileset.Texture, drawPos, this.tileset.GetTileRegion(id), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, this.depth);
} }
} }