Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
object types, and size replaced with vector2
Browse files Browse the repository at this point in the history
  • Loading branch information
nolemretaW committed May 1, 2024
1 parent f3a0612 commit 139c127
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 66 deletions.
36 changes: 24 additions & 12 deletions TiledCSPlus/TiledMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private TiledLayer ParseLayer(XmlNode node, TiledLayerType type)
Name = node.Attributes["name"].Value,
Visible = true,
Opacity = 1.0f,
Parrallax = new Vector2(1.0f, 1.0f)
Parallax = new Vector2(1.0f, 1.0f)
};

if(attrWidth != null) tiledLayer.Width = int.Parse(attrWidth.Value);
Expand All @@ -355,13 +355,19 @@ private TiledLayer ParseLayer(XmlNode node, TiledLayerType type)
if(attrTint != null) tiledLayer.TintColor = ParseColor(attrTint.Value);
if(attrClass != null) tiledLayer.Class = attrClass.Value;
if(attrOpacity != null) tiledLayer.Opacity = float.Parse(attrOpacity.Value, CultureInfo.InvariantCulture);
if(attrOffsetX != null || attrOffsetY != null) tiledLayer.Offset = new Vector2(0, 0);
if(attrOffsetX != null) tiledLayer.Offset.X = float.Parse(attrOffsetX.Value, CultureInfo.InvariantCulture);
if(attrOffsetY != null) tiledLayer.Offset.Y = float.Parse(attrOffsetY.Value, CultureInfo.InvariantCulture);

Vector2 offset = new Vector2();
if(attrOffsetX != null) offset.X = float.Parse(attrOffsetX.Value, CultureInfo.InvariantCulture);
if(attrOffsetY != null) offset.Y = float.Parse(attrOffsetY.Value, CultureInfo.InvariantCulture);
if(attrOffsetX != null || attrOffsetY != null) tiledLayer.Offset = offset;

Vector2 parallax = new Vector2();
if(attrParallaxX != null)
tiledLayer.Parrallax.X = float.Parse(attrParallaxX.Value, CultureInfo.InvariantCulture);
parallax.X = float.Parse(attrParallaxX.Value, CultureInfo.InvariantCulture);
if(attrParallaxY != null)
tiledLayer.Parrallax.Y = float.Parse(attrParallaxY.Value, CultureInfo.InvariantCulture);
parallax.Y = float.Parse(attrParallaxY.Value, CultureInfo.InvariantCulture);
if (attrParallaxX != null || attrParallaxY != null) tiledLayer.Parallax = parallax;

if(nodesProperty != null) tiledLayer.Properties = ParseProperties(nodesProperty);

if(type == TiledLayerType.TileLayer)
Expand Down Expand Up @@ -631,12 +637,14 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
Name = node.Attributes["name"]?.Value,
Class = MapVersion == "1.9" ? node.Attributes["class"]?.Value : node.Attributes["type"]?.Value,
Position = new Vector2(float.Parse(node.Attributes["x"].Value, CultureInfo.InvariantCulture),
float.Parse(node.Attributes["y"].Value, CultureInfo.InvariantCulture))
float.Parse(node.Attributes["y"].Value, CultureInfo.InvariantCulture)),
Type = TiledObjectType.Rectangular
};

if(attrGid != null)
{
ParseObjectGid(ref obj, attrGid.Value);
obj.Type = TiledObjectType.Tile;
}

if(nodesProperty != null)
Expand All @@ -659,6 +667,7 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
}

obj.Polygon = polygon;
obj.Type = TiledObjectType.Polygon;
}

if (nodePolyline != null)
Expand All @@ -676,29 +685,32 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
}

obj.Polyline = polyline;
obj.Type = TiledObjectType.Polyline;
}

if(nodeEllipse != null)
{
obj.Ellipse = new TiledEllipse();
obj.Type = TiledObjectType.Eclipse;
}

if(nodePoint != null)
{
obj.Point = new TiledPoint();
obj.Type = TiledObjectType.Point;
}

if(node.Attributes["width"] != null || node.Attributes["height"] != null) obj.Size = new Size(0, 0);
Vector2 size = new Vector2();
if(node.Attributes["width"] != null)
{
obj.Size.Width = float.Parse(node.Attributes["width"].Value, CultureInfo.InvariantCulture);
size.X = float.Parse(node.Attributes["width"].Value, CultureInfo.InvariantCulture);
}

if(node.Attributes["height"] != null)
{
obj.Size.Height = float.Parse(node.Attributes["height"].Value, CultureInfo.InvariantCulture);
size.Y = float.Parse(node.Attributes["height"].Value, CultureInfo.InvariantCulture);
}

if (node.Attributes["width"] != null || node.Attributes["height"] != null) obj.Size = size;

if(node.Attributes["rotation"] != null)
{
obj.Rotation = float.Parse(node.Attributes["rotation"].Value, CultureInfo.InvariantCulture);
Expand Down
93 changes: 45 additions & 48 deletions TiledCSPlus/TiledModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,6 @@

namespace TiledCSPlus
{
/// <summary>
/// Represents a size
/// </summary>
public class Size : IEquatable<Size>
{
public float Width { get; internal set; }
public float Height { get; internal set; }

public Size(float width = 0, float height = 0)
{
this.Width = width;
this.Height = height;
}

public bool Equals(Size other)
{
return Width.Equals(other.Width) && Height.Equals(other.Height);
}
}

/// <summary>
/// Represents an element within the Tilesets array of a TiledMap object
/// </summary>
Expand Down Expand Up @@ -115,12 +95,12 @@ public class TiledLayer
/// <summary>
/// Layer offset
/// </summary>
public Vector2 Offset;
public Vector2 Offset { get; internal set; }

/// <summary>
/// Parallax position
/// </summary>
public Vector2 Parrallax;
public Vector2 Parallax { get; internal set; }

/// <summary>
/// The layer opacity
Expand Down Expand Up @@ -200,7 +180,12 @@ public class TiledObject
/// <summary>
/// Object's size in pixels
/// </summary>
public Size Size { get; internal set; }
public Vector2 Size { get; internal set; }

/// <summary>
/// Object type
/// </summary>
public TiledObjectType Type { get; internal set; }

/// <summary>
/// The tileset gid when the object is linked to a tile
Expand Down Expand Up @@ -230,16 +215,6 @@ public class TiledObject
/// If an object was set to a polyline shape, this property will be set and can be used to access the polyline's data
/// </summary>
public TiledPolyline Polyline { get; internal set; }

/// <summary>
/// If an object was set to a point shape, this property will be set
/// </summary>
public TiledPoint Point { get; internal set; }

/// <summary>
/// If an object was set to an ellipse shape, this property will be set
/// </summary>
public TiledEllipse Ellipse { get; internal set; }
}

/// <summary>
Expand All @@ -264,20 +239,6 @@ public class TiledPolyline
public Vector2[] Points { get; internal set; }
}

/// <summary>
/// Represents a point shape
/// </summary>
public class TiledPoint
{
}

/// <summary>
/// Represents an ellipse shape
/// </summary>
public class TiledEllipse
{
}

/// <summary>
/// Represents a tile within a tileset
/// </summary>
Expand Down Expand Up @@ -339,7 +300,7 @@ public class TiledImage
/// <summary>
/// The image source path
/// </summary>
public string Source;
public string Source { get; internal set; }
}

/// <summary>
Expand Down Expand Up @@ -588,4 +549,40 @@ public class TiledTerrainSetTile
/// </summary>
public int TopLeft { get; internal set; }
}

/// <summary>
/// Represents object types in Tiled
/// </summary>
public enum TiledObjectType
{
/// <summary>
/// Point object type
/// </summary>
Point,

/// <summary>
/// Eclipse object type
/// </summary>
Eclipse,

/// <summary>
/// Polygon object type
/// </summary>
Polygon,

/// <summary>
/// Polyline object type (used when polygon object is not closed)
/// </summary>
Polyline,

/// <summary>
/// Tile object type
/// </summary>
Tile,

/// <summary>
/// Rectangular object type
/// </summary>
Rectangular
}
}
16 changes: 10 additions & 6 deletions TiledCSPlus/TiledTileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
Class = TilesetVersion == "1.9" ? node.Attributes["class"]?.Value : node.Attributes["type"]?.Value,
Gid = int.Parse(node.Attributes["gid"]?.Value ?? "0"),
Position = new Vector2(float.Parse(node.Attributes["x"].Value, CultureInfo.InvariantCulture),
float.Parse(node.Attributes["y"].Value, CultureInfo.InvariantCulture))
float.Parse(node.Attributes["y"].Value, CultureInfo.InvariantCulture)),
Type = TiledObjectType.Rectangular
};

if(nodesProperty != null)
Expand All @@ -334,6 +335,7 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
}

obj.Polygon = polygon;
obj.Type = TiledObjectType.Polygon;
}

if (nodePolyline != null)
Expand All @@ -351,28 +353,30 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
}

obj.Polyline = polyline;
obj.Type = TiledObjectType.Polyline;
}

if(nodeEllipse != null)
{
obj.Ellipse = new TiledEllipse();
obj.Type = TiledObjectType.Eclipse;
}

if(nodePoint != null)
{
obj.Point = new TiledPoint();
obj.Type = TiledObjectType.Point;
}

if(node.Attributes["width"] != null || node.Attributes["height"] != null) obj.Size = new Size();
Vector2 size = new Vector2();
if(node.Attributes["width"] != null)
{
obj.Size.Width = float.Parse(node.Attributes["width"].Value, CultureInfo.InvariantCulture);
size.X = float.Parse(node.Attributes["width"].Value, CultureInfo.InvariantCulture);
}

if(node.Attributes["height"] != null)
{
obj.Size.Height = float.Parse(node.Attributes["height"].Value, CultureInfo.InvariantCulture);
size.Y = float.Parse(node.Attributes["height"].Value, CultureInfo.InvariantCulture);
}
if (node.Attributes["width"] != null || node.Attributes["height"] != null) obj.Size = size;

if(node.Attributes["rotation"] != null)
{
Expand Down

0 comments on commit 139c127

Please sign in to comment.