Merge pull request #801 from Gandifil/tiled-parallax-factor

Tiled parallax factor
This commit is contained in:
Max Kopjev
2023-03-18 23:29:00 +01:00
committed by GitHub
9 changed files with 61 additions and 38 deletions
@@ -87,6 +87,8 @@ namespace MonoGame.Extended.Content.Pipeline.Tiled
writer.Write(layer.Opacity);
writer.Write(layer.OffsetX);
writer.Write(layer.OffsetY);
writer.Write(layer.ParallaxX);
writer.Write(layer.ParallaxY);
writer.WriteTiledMapProperties(layer.Properties);
@@ -215,9 +217,9 @@ namespace MonoGame.Extended.Content.Pipeline.Tiled
return TiledMapObjectType.Rectangle;
}
public override string GetRuntimeType(TargetPlatform targetPlatform) => "MonoGame.Extended.Tiled.TiledMap, MonoGame.Extended.Tiled";
public override string GetRuntimeReader(TargetPlatform targetPlatform) => "MonoGame.Extended.Tiled.TiledMapReader, MonoGame.Extended.Tiled";
}
}
}
@@ -131,17 +131,18 @@ namespace MonoGame.Extended.Tiled.Renderers
if (layer is TiledMapObjectLayer)
return;
Draw(layer, Vector2.Zero, ref viewMatrix, ref projectionMatrix, effect, depth);
Draw(layer, Vector2.Zero, Vector2.One, ref viewMatrix, ref projectionMatrix, effect, depth);
}
private void Draw(TiledMapLayer layer, Vector2 parentOffset, ref Matrix viewMatrix, ref Matrix projectionMatrix, Effect effect, float depth)
{
private void Draw(TiledMapLayer layer, Vector2 parentOffset, Vector2 parentParallaxFactor, ref Matrix viewMatrix, ref Matrix projectionMatrix, Effect effect, float depth)
{
var offset = parentOffset + layer.Offset;
var parallaxFactor = parentParallaxFactor * layer.ParallaxFactor;
if (layer is TiledMapGroupLayer groupLayer)
{
foreach (var subLayer in groupLayer.Layers)
Draw(subLayer, offset, ref viewMatrix, ref projectionMatrix, effect, depth);
Draw(subLayer, offset, parallaxFactor, ref viewMatrix, ref projectionMatrix, effect, depth);
}
else
{
@@ -152,9 +153,9 @@ namespace MonoGame.Extended.Tiled.Renderers
if (tiledMapEffect == null)
return;
// model-to-world transform
tiledMapEffect.World = _worldMatrix;
tiledMapEffect.View = viewMatrix;
// model-to-world transform
tiledMapEffect.World = _worldMatrix;
tiledMapEffect.View = parallaxFactor == Vector2.One ? viewMatrix : IncludeParallax(viewMatrix, parallaxFactor);
tiledMapEffect.Projection = projectionMatrix;
foreach (var layerModel in _mapModel.LayersOfLayerModels[layer])
@@ -181,5 +182,11 @@ namespace MonoGame.Extended.Tiled.Renderers
}
}
}
private Matrix IncludeParallax(Matrix viewMatrix, Vector2 parallaxFactor)
{
viewMatrix.Translation *=new Vector3(parallaxFactor, 1f);
return viewMatrix;
}
}
}
}
@@ -12,6 +12,8 @@ namespace MonoGame.Extended.Tiled.Serialization
{
Type = type;
Opacity = 1.0f;
ParallaxX = 1.0f;
ParallaxY = 1.0f;
Visible = true;
Properties = new List<TiledMapPropertyContent>();
}
@@ -31,16 +33,22 @@ namespace MonoGame.Extended.Tiled.Serialization
[XmlAttribute(AttributeName = "offsety")]
public float OffsetY { get; set; }
[XmlAttribute(AttributeName = "parallaxx")]
public float ParallaxX { get; set; }
[XmlAttribute(AttributeName = "parallaxy")]
public float ParallaxY { get; set; }
[XmlArray("properties")]
[XmlArrayItem("property")]
public List<TiledMapPropertyContent> Properties { get; set; }
[XmlIgnore]
public TiledMapLayerType Type { get; }
public override string ToString()
{
return Name;
}
}
}
}
@@ -8,8 +8,8 @@ namespace MonoGame.Extended.Tiled
public class TiledMapGroupLayer : TiledMapLayer
{
public List<TiledMapLayer> Layers { get; }
public TiledMapGroupLayer(string name, List<TiledMapLayer> layers, Vector2? offset = null, float opacity = 1, bool isVisible = true)
: base(name, offset, opacity, isVisible)
public TiledMapGroupLayer(string name, List<TiledMapLayer> layers, Vector2? offset = null, Vector2? parallaxFactor = null, float opacity = 1, bool isVisible = true)
: base(name, offset, parallaxFactor, opacity, isVisible)
{
Layers = layers;
}
@@ -5,8 +5,8 @@ namespace MonoGame.Extended.Tiled
{
public class TiledMapImageLayer : TiledMapLayer, IMovable
{
public TiledMapImageLayer(string name, Texture2D image, Vector2? position = null, Vector2? offset = null, float opacity = 1.0f, bool isVisible = true)
: base(name, offset, opacity, isVisible)
public TiledMapImageLayer(string name, Texture2D image, Vector2? position = null, Vector2? offset = null, Vector2? parallaxFactor = null, float opacity = 1.0f, bool isVisible = true)
: base(name, offset, parallaxFactor, opacity, isVisible)
{
Image = image;
Position = position ?? Vector2.Zero;
@@ -15,4 +15,4 @@ namespace MonoGame.Extended.Tiled
public Texture2D Image { get; }
public Vector2 Position { get; set; }
}
}
}
@@ -8,15 +8,17 @@ namespace MonoGame.Extended.Tiled
public bool IsVisible { get; set; }
public float Opacity { get; set; }
public Vector2 Offset { get; set; }
public Vector2 ParallaxFactor { get; set; }
public TiledMapProperties Properties { get; }
protected TiledMapLayer(string name, Vector2? offset = null, float opacity = 1.0f, bool isVisible = true)
protected TiledMapLayer(string name, Vector2? offset = null, Vector2? parallaxFactor = null, float opacity = 1.0f, bool isVisible = true)
{
Name = name;
Offset = offset ?? Vector2.Zero;
ParallaxFactor = parallaxFactor ?? Vector2.One;
Opacity = opacity;
IsVisible = isVisible;
Properties = new TiledMapProperties();
}
}
}
}
@@ -4,9 +4,9 @@ namespace MonoGame.Extended.Tiled
{
public class TiledMapObjectLayer : TiledMapLayer
{
public TiledMapObjectLayer(string name, TiledMapObject[] objects, Color? color = null, TiledMapObjectDrawOrder drawOrder = TiledMapObjectDrawOrder.TopDown,
Vector2? offset = null, float opacity = 1.0f, bool isVisible = true)
: base(name, offset, opacity, isVisible)
public TiledMapObjectLayer(string name, TiledMapObject[] objects, Color? color = null, TiledMapObjectDrawOrder drawOrder = TiledMapObjectDrawOrder.TopDown,
Vector2? offset = null, Vector2? parallaxFactor = null, float opacity = 1.0f, bool isVisible = true)
: base(name, offset, parallaxFactor, opacity, isVisible)
{
Color = color;
DrawOrder = drawOrder;
@@ -17,4 +17,4 @@ namespace MonoGame.Extended.Tiled
public TiledMapObjectDrawOrder DrawOrder { get; }
public TiledMapObject[] Objects { get; }
}
}
}
@@ -59,7 +59,7 @@ namespace MonoGame.Extended.Tiled
map.AddTileset(tileset, firstGlobalIdentifier);
}
}
private static TiledMapTileset ReadTileset(ContentReader reader, TiledMap map)
{
var external = reader.ReadBoolean();
@@ -93,6 +93,9 @@ namespace MonoGame.Extended.Tiled
var offsetX = reader.ReadSingle();
var offsetY = reader.ReadSingle();
var offset = new Vector2(offsetX, offsetY);
var parallaxX = reader.ReadSingle();
var parallaxY = reader.ReadSingle();
var parallaxFactor = new Vector2(parallaxX, parallaxY);
var properties = new TiledMapProperties();
ReadProperties(reader, properties);
@@ -102,16 +105,16 @@ namespace MonoGame.Extended.Tiled
switch (layerType)
{
case TiledMapLayerType.ImageLayer:
layer = ReadImageLayer(reader, name, offset, opacity, isVisible);
layer = ReadImageLayer(reader, name, offset, parallaxFactor, opacity, isVisible);
break;
case TiledMapLayerType.TileLayer:
layer = ReadTileLayer(reader, name, offset, opacity, isVisible, map);
layer = ReadTileLayer(reader, name, offset, parallaxFactor, opacity, isVisible, map);
break;
case TiledMapLayerType.ObjectLayer:
layer = ReadObjectLayer(reader, name, offset, opacity, isVisible, map);
layer = ReadObjectLayer(reader, name, offset, parallaxFactor, opacity, isVisible, map);
break;
case TiledMapLayerType.GroupLayer:
layer = new TiledMapGroupLayer(name, ReadGroup(reader, map), offset, opacity, isVisible);
layer = new TiledMapGroupLayer(name, ReadGroup(reader, map), offset, parallaxFactor, opacity, isVisible);
break;
default:
throw new ArgumentOutOfRangeException();
@@ -123,7 +126,7 @@ namespace MonoGame.Extended.Tiled
return layer;
}
private static TiledMapLayer ReadObjectLayer(ContentReader reader, string name, Vector2 offset, float opacity, bool isVisible, TiledMap map)
private static TiledMapLayer ReadObjectLayer(ContentReader reader, string name, Vector2 offset, Vector2 parallaxFactor, float opacity, bool isVisible, TiledMap map)
{
var color = reader.ReadColor();
var drawOrder = (TiledMapObjectDrawOrder)reader.ReadByte();
@@ -133,7 +136,7 @@ namespace MonoGame.Extended.Tiled
for (var i = 0; i < objectCount; i++)
objects[i] = ReadTiledMapObject(reader, map);
return new TiledMapObjectLayer(name, objects, color, drawOrder, offset, opacity, isVisible);
return new TiledMapObjectLayer(name, objects, color, drawOrder, offset, parallaxFactor, opacity, isVisible);
}
private static TiledMapObject ReadTiledMapObject(ContentReader reader, TiledMap map)
@@ -202,16 +205,16 @@ namespace MonoGame.Extended.Tiled
return points;
}
private static TiledMapImageLayer ReadImageLayer(ContentReader reader, string name, Vector2 offset, float opacity, bool isVisible)
private static TiledMapImageLayer ReadImageLayer(ContentReader reader, string name, Vector2 offset, Vector2 parallaxFactor, float opacity, bool isVisible)
{
var texture = reader.ReadExternalReference<Texture2D>();
var x = reader.ReadSingle();
var y = reader.ReadSingle();
var position = new Vector2(x, y);
return new TiledMapImageLayer(name, texture, position, offset, opacity, isVisible);
return new TiledMapImageLayer(name, texture, position, offset, parallaxFactor, opacity, isVisible);
}
private static TiledMapTileLayer ReadTileLayer(ContentReader reader, string name, Vector2 offset, float opacity, bool isVisible, TiledMap map)
private static TiledMapTileLayer ReadTileLayer(ContentReader reader, string name, Vector2 offset, Vector2 parallaxFactor, float opacity, bool isVisible, TiledMap map)
{
var width = reader.ReadInt32();
var height = reader.ReadInt32();
@@ -219,7 +222,7 @@ namespace MonoGame.Extended.Tiled
var tileHeight = map.TileHeight;
var tileCount = reader.ReadInt32();
var layer = new TiledMapTileLayer(name, width, height, tileWidth, tileHeight, offset, opacity, isVisible);
var layer = new TiledMapTileLayer(name, width, height, tileWidth, tileHeight, offset, parallaxFactor, opacity, isVisible);
for (var i = 0; i < tileCount; i++)
{
@@ -233,4 +236,4 @@ namespace MonoGame.Extended.Tiled
return layer;
}
}
}
}
@@ -5,8 +5,9 @@ namespace MonoGame.Extended.Tiled
{
public class TiledMapTileLayer : TiledMapLayer
{
public TiledMapTileLayer(string name, int width, int height, int tileWidth, int tileHeight, Vector2? offset = null, float opacity = 1, bool isVisible = true)
: base(name, offset, opacity, isVisible)
public TiledMapTileLayer(string name, int width, int height, int tileWidth, int tileHeight, Vector2? offset = null,
Vector2? parallaxFactor = null, float opacity = 1, bool isVisible = true)
: base(name, offset, parallaxFactor, opacity, isVisible)
{
Width = width;
Height = height;
@@ -56,7 +57,7 @@ namespace MonoGame.Extended.Tiled
var index = GetTileIndex(x, y);
Tiles[index] = new TiledMapTile(globalIdentifier, x, y);
}
public void RemoveTile(ushort x, ushort y)
{
SetTile(x, y, 0);