Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wangsets parsing #93

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/TiledModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,75 @@ public class TiledChunk
public byte[] dataRotationFlags;
}

/// <summary>
/// Represents a wangset from the list of wangsets. There can be many wangsets representing different sets of tiles
/// </summary>
public class TiledWangset
{
/// <summary>
/// The wangsets name
/// </summary>
public string name;

/// <summary>
/// The wangsets type
/// </summary>
public string type;

/// <summary>
/// The array of wangsets colors
/// </summary>
public TiledWangcolor[] wangcolors;

/// <summary>
/// The array of wangsets tiles
/// </summary>
public TiledWangtile[] wangtiles;
}

/// <summary>
/// Represents colors used in the wangset. These nodes are set as direct childs of wangset and could be many.
/// </summary>
public class TiledWangcolor
{
/// <summary>
/// Represents an ID. An editor is not always generate them.
/// </summary>
public int id;
/// <summary>
/// Represents the colour name set by a user, which is more like a name of the whole group of tiles.
/// </summary>
public string name;
/// <summary>
/// Represents the colour code in hex started with #. (e.g. #ff00ff)
/// </summary>
public string color;
/// <summary>
/// Perhaps represents the sorting order. Usually it is set to -1
/// </summary>
public int tile;
/// <summary>
/// Represents the multiplier of probability 0.0 - 1.0
/// </summary>
public float probability;
}

/// <summary>
/// Represents a direction of the wangtile marker.
/// </summary>
public class TiledWangtile
{
/// <summary>
/// Represents an ID of the tile.
/// </summary>
public int id;
/// <summary>
/// Represents the direction where counting CCW
/// 0 - east, 1 - east-south, 2 - south, 3 - west-south, 4 - west, 5 - north-west, 6 - north, 7 - north-east.
/// </summary>
public int[] wangs;
}

/// <summary>
/// Represents a tile offset data object
/// </summary>
Expand Down
62 changes: 62 additions & 0 deletions src/TiledTileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class TiledTileset
/// <remarks>Not all tiles within a tileset have definitions. Only those with properties, animations, terrains, ...</remarks>
public TiledTile[] Tiles { get; set; }
/// <summary>
/// Set of wangsets
/// </summary>
public TiledWangset[] Wangsets { get; set; }
/// <summary>
/// An array of tileset properties
/// </summary>
public TiledProperty[] Properties { get; set; }
Expand Down Expand Up @@ -128,6 +132,7 @@ public void ParseXml(string xml)
var nodeOffset = nodeTileset.SelectSingleNode("tileoffset");
var nodesTile = nodeTileset.SelectNodes("tile");
var nodesProperty = nodeTileset.SelectNodes("properties/property");
var nodeWangsets = nodeTileset.SelectNodes("wangsets/wangset");

var attrMargin = nodeTileset.Attributes["margin"];
var attrSpacing = nodeTileset.Attributes["spacing"];
Expand All @@ -144,6 +149,8 @@ public void ParseXml(string xml)
if (attrSpacing != null) Spacing = int.Parse(nodeTileset.Attributes["spacing"].Value);
if (attrClass != null) Class = attrClass.Value;
if (nodeImage != null) Image = ParseImage(nodeImage);

if (nodeWangsets != null) Wangsets = ParseWangsets(nodeWangsets);
if (nodeOffset != null) Offset = ParseOffset(nodeOffset);

Tiles = ParseTiles(nodesTile);
Expand All @@ -155,13 +162,68 @@ public void ParseXml(string xml)
}
}

private TiledWangset[] ParseWangsets(XmlNodeList nodeWangsets)
{
var result = new List<TiledWangset>();

foreach (XmlNode node in nodeWangsets)
{

var Wangsets = new TiledWangset();
Wangsets.name = node.Attributes["name"].Value;
Wangsets.type = node.Attributes["type"]?.Value;
///layers
var nodeWangcolor = node.SelectNodes("wangcolor");
if (nodeWangcolor != null)
{
var wangcolors = new List<TiledWangcolor>();
int id = 1;
foreach (XmlNode wangcolornode in nodeWangcolor)
{
var wangcolor = new TiledWangcolor();
wangcolor.id = id;
wangcolor.name = wangcolornode.Attributes["name"].Value;
wangcolor.color = wangcolornode.Attributes["color"].Value;
wangcolor.probability = int.Parse(wangcolornode.Attributes["probability"].Value);
wangcolors.Add(wangcolor);
id++;
}
Wangsets.wangcolors = wangcolors.ToArray();
}
//tiles
var nodeWangtiles = node.SelectNodes("wangtile");
if (nodeWangtiles != null)
{
var wangtiles = new List<TiledWangtile>();
foreach (XmlNode wangtilenode in nodeWangtiles)
{
var wangtile = new TiledWangtile();
wangtile.id = int.Parse(wangtilenode.Attributes["tileid"].Value);
wangtile.wangs = new int[8];
var wangs = wangtilenode.Attributes["wangid"].Value.Split(",");
int counter = 0;
foreach (string w in wangs)
{
wangtile.wangs[counter] = int.Parse(w);
counter++;
}
wangtiles.Add(wangtile);
}
Wangsets.wangtiles = wangtiles.ToArray();
}
result.Add(Wangsets);
}

return result.ToArray();

private TiledOffset ParseOffset(XmlNode node)
{
var tiledOffset = new TiledOffset();
tiledOffset.x = int.Parse(node.Attributes["x"].Value);
tiledOffset.y = int.Parse(node.Attributes["y"].Value);

return tiledOffset;

}

private TiledImage ParseImage(XmlNode node)
Expand Down