Add texture for tile.

This commit is contained in:
Gandifil
2023-12-22 20:40:06 +03:00
parent c3fe4b071c
commit 7b0668a6bd
3 changed files with 36 additions and 19 deletions
@@ -1,6 +1,7 @@
using System;
using System.Collections.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MonoGame.Extended.Tiled
{
@@ -12,8 +13,9 @@ namespace MonoGame.Extended.Tiled
public ReadOnlyCollection<TiledMapTilesetTileAnimationFrame> AnimationFrames { get; }
public TiledMapTilesetTileAnimationFrame CurrentAnimationFrame { get; private set; }
public TiledMapTilesetAnimatedTile(int localTileIdentifier, TiledMapTilesetTileAnimationFrame[] frames, string type = null, TiledMapObject[] objects = null)
: base(localTileIdentifier, type, objects)
public TiledMapTilesetAnimatedTile(int localTileIdentifier,
TiledMapTilesetTileAnimationFrame[] frames, string type = null, TiledMapObject[] objects = null, Texture2D texture = null)
: base(localTileIdentifier, type, objects, texture)
{
if (frames.Length == 0) throw new InvalidOperationException("There must be at least one tileset animation frame");
@@ -33,4 +35,4 @@ namespace MonoGame.Extended.Tiled
CurrentAnimationFrame = AnimationFrames[_frameIndex];
}
}
}
}
@@ -29,25 +29,35 @@ namespace MonoGame.Extended.Tiled
var tileset = new TiledMapTileset(texture, tileWidth, tileHeight, tileCount, spacing, margin, columns);
for (var tileIndex = 0; tileIndex < explicitTileCount; tileIndex++)
{
var localTileIdentifier = reader.ReadInt32();
var type = reader.ReadString();
var animationFramesCount = reader.ReadInt32();
var tilesetTile = animationFramesCount <= 0
? ReadTiledMapTilesetTile(reader, tileset, objects =>
new TiledMapTilesetTile(localTileIdentifier, type, objects))
: ReadTiledMapTilesetTile(reader, tileset, objects =>
new TiledMapTilesetAnimatedTile(localTileIdentifier, ReadTiledMapTilesetAnimationFrames(reader, tileset, animationFramesCount), type, objects));
ReadProperties(reader, tilesetTile.Properties);
tileset.Tiles.Add(tilesetTile);
}
ReadTile(reader, tileset);
ReadProperties(reader, tileset.Properties);
return tileset;
}
private static TiledMapTilesetTileAnimationFrame[] ReadTiledMapTilesetAnimationFrames(ContentReader reader, TiledMapTileset tileset, int animationFramesCount)
private static void ReadTile(ContentReader reader, TiledMapTileset tileset)
{
var texture = reader.ReadExternalReference<Texture2D>();
var localTileIdentifier = reader.ReadInt32();
var type = reader.ReadString();
var animationFramesCount = reader.ReadInt32();
var objectCount = reader.ReadInt32();
var objects = new TiledMapObject[objectCount];
for (var i = 0; i < objectCount; i++)
objects[i] = ReadTiledMapObject(reader, tileset);
var tilesetTile = animationFramesCount <= 0
? new TiledMapTilesetTile(localTileIdentifier, type, objects, texture)
: new TiledMapTilesetAnimatedTile(localTileIdentifier,
ReadTiledMapTilesetAnimationFrames(reader, tileset, animationFramesCount), type, objects, texture);
ReadProperties(reader, tilesetTile.Properties);
tileset.Tiles.Add(tilesetTile);
}
private static TiledMapTilesetTileAnimationFrame[] ReadTiledMapTilesetAnimationFrames(ContentReader reader, TiledMapTileset tileset, int animationFramesCount)
{
var animationFrames = new TiledMapTilesetTileAnimationFrame[animationFramesCount];
@@ -64,6 +74,7 @@ namespace MonoGame.Extended.Tiled
private static TiledMapTilesetTile ReadTiledMapTilesetTile(ContentReader reader, TiledMapTileset tileset, Func<TiledMapObject[], TiledMapTilesetTile> createTile)
{
var texture = reader.ReadExternalReference<Texture2D>();
var objectCount = reader.ReadInt32();
var objects = new TiledMapObject[objectCount];
@@ -1,13 +1,16 @@
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
namespace MonoGame.Extended.Tiled
{
[DebuggerDisplay("{LocalTileIdentifier}: Type: {Type}, Properties: {Properties.Count}, Objects: {Objects.Count}")]
public class TiledMapTilesetTile
{
public TiledMapTilesetTile(int localTileIdentifier, string type = null, TiledMapObject[] objects = null)
public TiledMapTilesetTile(int localTileIdentifier, string type = null,
TiledMapObject[] objects = null, Texture2D texture = null)
{
Texture = texture;
LocalTileIdentifier = localTileIdentifier;
Type = type;
Objects = objects != null ? new List<TiledMapObject>(objects) : new List<TiledMapObject>();
@@ -18,5 +21,6 @@ namespace MonoGame.Extended.Tiled
public string Type { get; }
public TiledMapProperties Properties { get; }
public List<TiledMapObject> Objects { get; }
public Texture2D Texture { get; }
}
}
}