2017-09-26 8 views
0

Ich habe es geschafft, eine Datei in meinem Plattform-Spiel zu lesen, das vom 'Tiled' Level-Editor erstellt wurde und die TiledSharp-Bibliothek benutzt. Es ist mir schließlich gelungen, ein Level zu laden und korrekt in meinem Spiel anzuzeigen, aber ich kann nicht herausfinden, wie ich auf die benutzerdefinierten Eigenschaften zugreifen kann, die ich auf Kacheln in der Map eingestellt habe. Zum Beispiel habe ich eine boolesche Eigenschaft namens 'Ignorieren', die angibt, ob die Kachel bei Kollisionsberechnungen ignoriert werden soll. Ich würde gerne die Ignore-Eigenschaft jeder Kachel lesen können, wenn sie geladen ist. Etwas wie das:Zugreifen auf benutzerdefinierte Eigenschaften in 'Tiled' Ebenen-Map (C#, Monogame)

for (var i = 0; i < map.Layers[0].Tiles.Count; i++) 
{ 
    Console.WriteLine(map.Layers[0].Tiles[i].Ignore); 
} 

Allerdings kann ich keinen Weg finden, auf diese Eigenschaften zuzugreifen. Kann mir bitte jemand helfen? Vielen Dank!

Edit: Hier ist ein wenig von dem Inhalt der Datei in lese ich gerade:

<?xml version="1.0" encoding="UTF-8"?> 
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="16" height="9" tilewidth="80" tileheight="80" nextobjectid="1"> 
<tileset firstgid="1" name="GroundTileSet" tilewidth="80" tileheight="80" tilecount="24" columns="6"> 
    <image source="GroundTileSet2.png" trans="df7126" width="480" height="320"/> 
    <tile id="0"> 
    <properties> 
    <property name="Ignore" type="bool" value="true"/> 
    <property name="OneWay" type="bool" value="false"/> 
    </properties> 
    </tile> 

Antwort

1

ich das Problem lösen geführt. Ich poste meine gesamte Level-Klasse für den Fall, dass es jemandem hilft.

using System; 
using TiledSharp; 
using System.Xml.Linq; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework; 

namespace Platformer 
{ 
    class Level 
    { 
     public Level(TmxMap map, Texture2D tileSet) 
     { 
      var tileMap = map; 
      Texture2D levelTilesetTexture = tileSet; 

      int tileWidth = tileMap.Tilesets[0].TileWidth; 
      int tileHeight = tileMap.Tilesets[0].TileHeight; 
      int tilesetTilesWide = levelTilesetTexture.Width/tileWidth; 
      int tilesetTilesHigh = levelTilesetTexture.Height/tileHeight; 
      GameInfo.gameInfo.bottomOfLevel = tileMap.Height * tileHeight; 
      GameInfo.gameInfo.leftOfLevel = 0; 


      int staticObjectSpriteWidth = tileMap.TileWidth; 
      int staticObjectSpriteHeight = tileMap.TileHeight; 
      Vector2 staticObjectBoundingBoxOffset = new Vector2(0, 0); 
      int staticObjectBoundingBoxWidth = tileMap.TileWidth; 
      int staticObjectBoundingBoxHeight = tileMap.TileHeight; 
      bool oneWayPlatform = false; 
      bool collisionObject = true; 
      bool ignore = false; 
      int drawLayer = 1; 

      for (var i = 0; i < tileMap.Layers[0].Tiles.Count; i++) 
      { 
       int tileNumber = tileMap.Layers[0].Tiles[i].Gid; 
       if (tileNumber != 0) // If not empty tile. 
       { 
        tileNumber--; 
        var tileProperties = tileMap.Tilesets[0].Tiles[tileNumber].Properties; 
        string ignoreValue = "nothing"; 
        string oneWayValue = "nothing"; 
        tileProperties.TryGetValue("Ignore", out ignoreValue); 
        ignore = Convert.ToBoolean(ignoreValue); 
        tileProperties.TryGetValue("OneWay", out oneWayValue); 
        oneWayPlatform = Convert.ToBoolean(oneWayValue); 

        int column = tileNumber % tilesetTilesWide; 
        int row = (int)Math.Floor((double)tileNumber/(double)tilesetTilesWide); 

        float x = (i % tileMap.Width) * tileMap.TileWidth; 
        float y = (float)Math.Floor(i/(double)tileMap.Width) * tileMap.TileHeight; 

        PlatformerGame.game.CreateStaticObject(levelTilesetTexture, new Vector2(x, y), staticObjectSpriteWidth, staticObjectSpriteHeight, 
          staticObjectBoundingBoxOffset, staticObjectBoundingBoxWidth, staticObjectBoundingBoxHeight, 
          column, row, oneWayPlatform, collisionObject, drawLayer, ignore); 
       } 
      } 
     } 
    } 
} 
Verwandte Themen