GithubHelp home page GithubHelp logo

thebonejarmer / tiledcs Goto Github PK

View Code? Open in Web Editor NEW
134.0 7.0 49.0 78 KB

TiledCS is a dotnet library for loading Tiled tilesets and maps

License: MIT License

C# 99.43% PowerShell 0.57%
tiled tiled-maps monogame monogame-pipeline tiled-map-editor tiled-parser tmx tmx-parser tmx-maps

tiledcs's Introduction

Important! It is with great sadness that I have to anounce that I no longer can find enough time to care for the project as it is. Not only do I personally no longer work with C#, there are other projects that require my attention.

TiledCS

TiledCS is a .NET library for loading Tiled maps and tilesets. It supports only the TMX and TSX file formats. The library requires no 3rd-party dependencies. This way the library can be used with popular game engines like Unity3D, MonoGame and Godot.

Installation

dotnet add package tiledcs

Usage

using System;
using System.IO;
using Newtonsoft.Json;
using TiledCS;

// For loading maps in XML format
var map = new TiledMap("path-to-map.tmx");
var tileset = new TiledTileset("path-to-tileset.tsx");

// Retrieving objects or layers can be done using Linq or a for loop
var myLayer = map.layers.First(l => l.name == "monsters");
var myObj = myLayer.objects.First(o => o.name == "monster");

// Since they are classes and not structs, you can do null checks to figure out if an object exists or not
if (myObj != null)
{
    var xx = myObj.x * 16;
    var yy = myObj.y * 16;
}

// You can use the helper methods to get useful information to generate maps
if (map.IsTileFlippedHorizontal(myLayer, 3, 5))
{
    // Do something
}

Examples

Rendering all tile layers

Within a layer whose type equals that of tilelayer, exists a field of type int[] called data. This array represents the gids of all tiles. A gid is a tile index from a tileset, not the tile id as some think. In order to render a specific tile from a tileset into your spritebatch, you would need to link the gid to a tileset.

If you have multiple tilesets, you need to figure out which gid belongs to which. To help you with this process, I have added some helper methods to make this more easier for you. Let's say for example you have the following list of tilesets:

main.tsx
dungeon.tsx
overworld.tsx

And each tileset has 10 tiles horizontal and 10 tiles vertical, then the gid's per tileset would look like this:

main.tsx 1..100
dungeon.tsx 101..200
overworld.tsx 201..300

Why? Because a tileset of 10x10 has 100 tiles in total and since 0 is used to tell that there is no tile, it starts with 1. Then the next included tileset's gid starts with the total amount of tiles from the tileset included before. Therefore you should be careful when extending an existing tileset when your map has already been drawn. The helper method TiledMap.GetTiledMapTileset returns a dictionary where the key represent the tileset's first gid and the value represents the tileset which is of type TiledTileset.

Warning! This works with external tilesets only for the moment. Support for embedded is on the way!

Once you have the tileset and the gid, you can use that data to retrieve the tile's source rect. You can than use this data to render the tile. See the example below. You may need to tweak things a bit to fit for you case but below should do the trick. Be aware the layer.width does not equal the layer's total width in pixels. The layer.width and layer.height value represents the layer's horizontal tiles and vertical tiles. So you need to know your tile's size in pixels too, which can be fetched from the TiledMap.TileWidth and TiledMap.TileHeight property.

var map = new TiledMap("path-to-map.tmx");
var tilesets = map.GetTiledTilesets("path-to-map-folder/"); // DO NOT forget the / at the end
var tileLayers = map.Layers.Where(x => x.type == TiledLayerType.TileLayer);

foreach (var layer in tileLayers)
{
    for (var y = 0; y < layer.height; y++)
    {
        for (var x = 0; x < layer.width; x++)
        {
            var index = (y * layer.width) + x; // Assuming the default render order is used which is from right to bottom
            var gid = layer.data[index]; // The tileset tile index
            var tileX = (x * map.TileWidth);
            var tileY = (y * map.TileHeight);

            // Gid 0 is used to tell there is no tile set
            if (gid == 0)
            {
                continue;
            }

            // Helper method to fetch the right TieldMapTileset instance. 
            // This is a connection object Tiled uses for linking the correct tileset to the gid value using the firstgid property.
            var mapTileset = map.GetTiledMapTileset(gid);
            
            // Retrieve the actual tileset based on the firstgid property of the connection object we retrieved just now
            var tileset = tilesets[mapTileset.firstgid];
            
            // Use the connection object as well as the tileset to figure out the source rectangle.
            var rect = map.GetSourceRect(mapTileset, tileset, gid);
            
            // Render sprite at position tileX, tileY using the rect
        }
    }
}

MonoGame

As you already know, TiledCS is a generic library which does not aim at specific frameworks or libraries. That said, I have been receiving lots of requests for an example on MonoGame. However, I do not use MonoGame. So it is up to the community to come up with an example on how to use TiledCS within monogame. And they did:

Note! Temeez has mentioned he is no longer actively working with C# anymore. So while the example may or may not work, it is not actively maintained anymore. Be aware of that.

Building

You need .NET 6 to compile TiledCS as it makes use of modern C# features that may or may not be included in earlier versions.

Version support

If you want to know what package supports your version of Tiled, please read the package's description.

Contribution

Feel free to open up an issue with your question or request. If you open a pull request, please use the develop branch as both source and target branch. The main branch is supposed to be production-ready and stable. I follow the GitFlow branching model and I ask you do too.

Credits

  • A very respectful thank you to Goodlyay who introduced support for tile rotations aka flipped tiles and taught me about bitwise operators in C#.
  • A very huge thanks towards user Temeez and IronCutter24 who put effort into providing a MonoGame example.

License

MIT

tiledcs's People

Contributors

bottledlactose avatar ebatiano avatar foreverwip avatar goodlyay avatar icodesometime avatar juanferrer avatar nedoxff avatar thebonejarmer avatar timjen3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

tiledcs's Issues

Feature Request: Tileset Collision Polygons

Thank you for adding support for object geometry in the last release.

Could you please add support for tile collision geometry? Attached is an example tileset.

Here is what the data looks like:

<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.5" tiledversion="1.7.2" name="Basic" tilewidth="32" tileheight="32" tilecount="1024" columns="32">
 <image source="Tileset.png" width="1024" height="1024"/>
 <tile id="0" type="Collapsable"/>
 <tile id="1">
  <objectgroup draworder="index" id="2">
   <object id="1" x="0" y="0" width="32" height="32"/>
   <object id="2" x="0" y="0" width="32" height="32"/>
  </objectgroup>
 </tile>
 <tile id="2" type="Spike"/>
 <tile id="3" type="Collapsable"/>
 <tile id="4">
  <objectgroup draworder="index" id="2">
   <object id="1" x="0" y="0" width="32" height="32"/>
  </objectgroup>
 </tile>
 <tile id="7">
  <objectgroup draworder="index" id="2">
   <object id="1" x="0" y="0">
    <polygon points="0,0 0,32 32,32 32,16"/>
   </object>
  </objectgroup>
 </tile>
 <tile id="8">
  <objectgroup draworder="index" id="2">
   <object id="1" x="0" y="16">
    <polygon points="0,0 0,16 32,16"/>
   </object>
  </objectgroup>
 </tile>
</tileset>

Tileset Collision Example.zip

Support for Base64 tile layers

Tiled offers the ability to encode tile data as base64 to reduce file size, and even has options for compression to reduce it further. In the past I've often found myself decoding and using these formats in game engine code so large maps could be stored in a much smaller size, and I'd love to see support for it come to this library. I have a fork ready that allows reading most of these base64 formats, this is just a check to see if there's interest in implementing it.

Fix rotated tiles for json maps

Up till now it is possible to deserialize the map from and to JSON, which is good. But this won't work with stuff like tile rotations. As the gid value of a rotated tile is bigger than the max of an int, the serialization would fail.

Add support for MonoGame content pipeline

According to several issues on the repo of TiledSharp, many users of MonoGame like to have a library like this to parse TiledMaps for their game. MonoGame however has its own way of loading assets using the content pipeline. And in order to make TiledCS work with MonoGame, some changes need to be made.

Parse Tiled objects from memory

Currently this library only supports loading maps and tilesets directly from disk. In my project however I store multiple files in an archive and can't access them directly through a path. The Tiled objects already contain a private Parse method which could be exposed to support loading from e.g. a string object.

monogame pipeline

Hi

Could you maybe add a monogame pipeline?
Not esy to find a library for monogame that has a Tiled pipeline

Replace GetSourceVector with GetSourceRect

Bjorn himself made a good point in our Discord discussion that the helper method, GetSourceVector, is going to be a problem when new features are being introduced. Right now it can work based on the current requirements but in near features it may not be sufficient anymore. And that all have to do with the fact that the GetSourceVector method is not returning absolute pixel values, more like the horizontal and vertical position on the tileset grid.

So I am considering to introduce a new method called GetSourceRect which basically returns a basic struct of absolute pixel values.

Support Tiled 1.7

While I was busy at work, a couple of new Tiled versions were released. With 1.7 being the current one. I support Tiled 1.5. so far but clearly need to put some effort into supporting Tiled 1.7.

Add .editorconfig file to the project

As the amount of contributors for this project seems to be growing, I would suggest adding a .editorconfig file to the project. This file is a standard that helps keeping things like line endings, charsets, indents and whitespaces consistent.

It prevents and fixes problems with development across different operating systems and text editor configurations. For more information, check the .editorconfig home page.

Most popular text editors and IDEs support the .editorconfig file. For instance, here's the VS Code extension that I'm using that updates my file with the specifications set in the file as soon as I save it.

I have an example of such file in my dotfiles repository which I'm pretty much copy-pasting into new projects I create.

Missing opacity from TiledLayer

One of the attributes in the resulting XML for a layer is opacity, I noticed TiledLayer doesn't have this property. Are you accepting PRs?

Add support for embedded tilesets

Like the title says really. Currently only external tilesets are supported but I should add logic to support internal or embedded tilesets as well.

Fix JSON support

Up till now the idea was to have the tiled maps being (de)serialized from JSON string input.. However, it has become clear to me that this approach may have been enough for earlier versions but I started realizing more and more differences between the XML format and the JSON format. Hence, simply (de)serializing won't cut it anymore. Therefore I would like to use a different approach.

With NewtonSoft.Json there is also the possibility to parse a JSON file just the way I am doing now with XML. This does require however that I have to rewrite both the TiledMap and TiledTileset classes entirely as parsing now have to happen in two directions.

Infinite layer support

A while ago a pull request got opened for the support of infinite layers. I had to reject the pull request but the need for it is valid and therefore I should look into adding support for it.

Missing Properties definition for Object Layers and Tile Layers

You can define custom properties for Object Layers/ Tile Layers themselves, similar to how you can define custom properties on Maps:

 <objectgroup id="38" name="ForegroundShader">
  <properties>
   <property name="EffectName" value="Fog"/>
  </properties>
 </objectgroup>

My previous TiledSharp implementation relied on this for certain layer wide parameters, would be handy to have in TiledCS

Add "Type" property to TiledTileset

It would be useful if the default Type property was included in the loaded TiledTileset class members after being loaded. If you look in the .tsx file the type is defined alongside the ID within the tags. E.g .

I assume these types are usually included in Object definitions instead?

Thought/Feature Request - Create method to draw Tile Map with user passed in function

I haven't started using TiledCS yet but plan on trying it out instead of Monogame.extended. I've run into a few issues with trying to get extended to work with Monogame 3.8 related to content pipeline.

Thought Setup: In your example, and tameez example, both are showing different ways to iterate over the tiles in a map so they can be drawn. These are great, and a user of TiledCS can just take those and toss them in their project/game/etc.

Thought: What if TiledCS had it's own map.draw method that took (as input) functions that it would use to draw the tiles. Then your library could house the mechanisms to do all the heavy lifting, leaving the users with just creating a method that would be passed in for drawing the individual tile. Things like the camera position (world offset), zoom, could all be passed in along with the function.

Just a thought, I didn't see a discord or other option for discussions.

Add JSON support

Right now only the TMX format is supported. I prefer to extend the support for JSON maps as well. I already have plans to start with this but I git an issue. The structure of the map in JSON is different than the one in XML. And I am not talking about syntax.

Let's take objects for example. In the TMX format they are listed in a XML node called ObjectGroups, which is a direct child node of the root node. Logically you'd expect the JSON format to have a similar node in the root node with the same name. However, that's not the case. In JSON the objects are part of a layer. See the example map contents below:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.4" tiledversion="1.4.3" orientation="staggered" renderorder="right-down" width="40" height="20" tilewidth="16" tileheight="16" infinite="0" staggeraxis="y" staggerindex="odd" nextlayerid="3" nextobjectid="2">
 <editorsettings>
  <export target="."/>
 </editorsettings>
 <properties>
  <property name="mybool" type="bool" value="true"/>
  <property name="mycolor" type="color" value="#ff6391bc"/>
  <property name="myfile" type="file" value="autotiles.png"/>
  <property name="myfloat" type="float" value="3.141"/>
  <property name="myint" type="int" value="24"/>
  <property name="myobj" type="object" value="1"/>
  <property name="name" value="Test 3"/>
 </properties>
 <layer id="1" name="Tile Layer 1" width="40" height="20">
  <data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
 </layer>
 <objectgroup id="2" name="Object Layer 1">
  <object id="1" x="200" y="56" width="32" height="32"/>
 </objectgroup>
</map>
{ "compressionlevel":-1,
 "height":20,
 "infinite":false,
 "layers":[
        {
         "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         "height":20,
         "id":1,
         "name":"Tile Layer 1",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":40,
         "x":0,
         "y":0
        }, 
        {
         "draworder":"topdown",
         "id":2,
         "name":"Object Layer 1",
         "objects":[
                {
                 "height":32,
                 "id":1,
                 "name":"",
                 "rotation":0,
                 "type":"",
                 "visible":true,
                 "width":32,
                 "x":200,
                 "y":56
                }],
         "opacity":1,
         "type":"objectgroup",
         "visible":true,
         "x":0,
         "y":0
        }],
 "nextlayerid":3,
 "nextobjectid":2,
 "orientation":"staggered",
 "properties":[
        {
         "name":"mybool",
         "type":"bool",
         "value":true
        }, 
        {
         "name":"mycolor",
         "type":"color",
         "value":"#ff6391bc"
        }, 
        {
         "name":"myfile",
         "type":"file",
         "value":"autotiles.png"
        }, 
        {
         "name":"myfloat",
         "type":"float",
         "value":3.141
        }, 
        {
         "name":"myint",
         "type":"int",
         "value":24
        }, 
        {
         "name":"myobj",
         "type":"object",
         "value":1
        }, 
        {
         "name":"name",
         "type":"string",
         "value":"Test 3"
        }],
 "renderorder":"right-down",
 "staggeraxis":"y",
 "staggerindex":"odd",
 "tiledversion":"1.4.3",
 "tileheight":16,
 "tilesets":[],
 "tilewidth":16,
 "type":"map",
 "version":1.4,
 "width":40
}

As you can see in the JSON version, there are 2 layers from which one now is an object layer. And this layer contains an array with all the objects. I get why this was done since the Tiled interface also uses layers for objects. Sadly, this is a problem for me because I cannot use the same class structure for both formats. So I think the only solution is to a class per format.

Support for Layer Groups

It seems Groups aren't supported, when I read a (v1.5) TMX file, which has layers in groups, those layers aren't available using map.Layers, nor is there a map.Groups property.

Loading a TiledMap when the map has tiles with rotations results in an Int32 OverflowException

I'm using TiledCS as an easy way to get data from Tiled maps, since I'm building my world out of 3D object tiles and wanted an easy way to edit maps, and it's been very helpful so far (thank you!), but, as the title states, I ran into a problem.

I'm guessing this is due to the tile flipping being stored in bits 32, 31, and 30, as specified here. However the tiles are read as signed ints, which can't hold values that large.

Here is an example of the error I get. Using Unity 2019.4.17f1 if it's relevant. I can supply the entire project if needed.

OverflowException: Value was either too large or too small for an Int32.
System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <9577ac7a62ef43179789031239ba8798>:0)
System.Int32.Parse (System.String s) (at <9577ac7a62ef43179789031239ba8798>:0)
TiledCS.Extensions+<>c.b__1_0 (System.String x) (at Assets/Scripts/TiledCS/Extensions.cs:26)
System.Linq.Enumerable+SelectArrayIterator`2[TSource,TResult].ToArray () (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
System.Linq.Enumerable.ToArray[TSource] (System.Collections.Generic.IEnumerable`1[T] source) (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
TiledCS.Extensions.AsIntArray (System.String[] src) (at Assets/Scripts/TiledCS/Extensions.cs:26)
TiledCS.TiledMap.ParseLayers (System.Xml.XmlNodeList nodeListLayers, System.Xml.XmlNodeList nodeListObjGroups) (at Assets/Scripts/TiledCS/TiledMap.cs:182)
TiledCS.TiledMap.Parse (System.String xml) (at Assets/Scripts/TiledCS/TiledMap.cs:116)
Rethrow as TiledException: Unable to parse xml data, make sure the xml data represents a valid Tiled map
TiledCS.TiledMap.Parse (System.String xml) (at Assets/Scripts/TiledCS/TiledMap.cs:120)
TiledCS.TiledMap..ctor (System.String path) (at Assets/Scripts/TiledCS/TiledMap.cs:83)
Game.Start () (at Assets/Scripts/Game.cs:29)

code examples

Hi

I'm looking for code examples especially to read all layers from a Tiled map.
Maybe you can make an 'examples' folder with code to do the most common things?

Is it also possible with this library to read the type of each Tile?
In my game I try to find out on which kind of tile (grass or road ..) the player is standing.

Tile Objects in objectgroup layers currently unusable

One of my scenes relies on extracting the gid similar to how Tile Layers do it. However I noticed Tile Objects do not have the gid.

In the tmx file, here is an example on how a Tile Object looks inside an object layer:

  <object id="1661" gid="496" x="864" y="160" width="32" height="32">
   <properties>
    <property name="ShadowOffset" value="-7"/>
    <property name="Z" type="float" value="11"/>
   </properties>
  </object>

You can find the gid value inside the

(apologies for the issue spam, I'm converting my game from TiledSharp to TiledCS and its a bit of a hastle haha)

how to loop through Tiles in a layer?

Hi

In the past I used TiledSharp and looped through a tiled map to draw it like this (below is TiledSharp code).
I changed the code to TiledCS and try to get all layers of a map using:

map.Layers[i]

then I can't get the list of Tiles by doing map.Layers[i].Tiles

There doesn't exist a way to get all Tiles from a layer. How to loop through all tiles then in a for-loop?

How to do this please?
Please give small code example for the 2nd for-loop to loop through all tiles of a layer.

`public void Draw(Matrix matrix)
{

        spriteBatch.Begin(
            SpriteSortMode.Deferred,
            samplerState:SamplerState.PointClamp,
            effect:null,
            blendState:null,
            rasterizerState:null,
            depthStencilState:null,
            transformMatrix:matrix);
        for (var j = 0; j < map.TileLayers.Count; j++)
        {
            for (var i = 0; i < map.TileLayers[j].Tiles.Count; i++)
            {
                int gid = map.TileLayers[j].Tiles[i].Gid;
                if (gid == 0)
                {
                    //If empty tile then do nothing
                }
                else
                {
                    int tileFrame = gid - 1;
                    int column = tileFrame % tilesetTilesWide;
                    int row = (int)Math.Floor((double)tileFrame / (double)tilesetTilesWide);
                    float x = (i % map.Width) * map.TileWidth;
                    float y = (float)Math.Floor(i / (double)map.Width) * map.TileHeight;

                    Rectangle tilesetRec = new Rectangle((tileWidth) * column, (tileHeight) * row, tileWidth, tileHeight);
                    spriteBatch.Draw(tileset, new Rectangle((int)x, (int)y, tileWidth, tileHeight), tilesetRec, Color.White);
                }
            }

        }
        spriteBatch.End();
    }`

Unit tests and some serialization cleanup

Hi, I've been tinkering with the library, and I felt like doing some additions I need.

I've forked the library here: https://github.com/vpenades/TiledCS

And I've did these changes:

  • Constrained to C# LangVersion 5 (which is a requirement of MonoGame)
  • Added much needed unit tests
  • Deserialization code has been simplified a bit (partially forced due to LangVersion 5)
  • Added debugger display attributes for easy read
  • Some methods can be declared as static

I'm going to keep moving with my fork, but if you wish, I could open a PR now with all those changes.

Also, how's the status of #26 ? I've seen there's PRs in the works, but not merged yet?

Unable to access points of polygon object (?)

I'm not sure if I'm missing something because I'm pretty new to this, but since I don't know where else to ask or contact you I thought I'd open this issue. When I've got an object of type TiledObject there seems to be no way to access the points of the polygon (if it's a polygon). Is that currently not possible or am I missing something?

Missing xml comments

I got some build warnings indicating there are some missing xml comments on some fields

"Background Color" map property missing

Hello again!
As the title states, the TiledMap property "backgroundcolor" (as it is written in the .tmx file) is not available through TiledCS.
I'm not sure how this data should be represented in the class, but at least for my purposes it doesn't matter how it is stored as long as I can access it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.