2017-09-26 58 views
0

我已經設法使用TiledSharp圖書館將文件讀入由'平鋪'級別編輯器製作的平臺遊戲中。我終於設法加載了一個關卡,並在我的遊戲中正確顯示,但我無法解決如何訪問我在地圖上的瓷磚上設置的自定義屬性。例如,我有一個名爲'忽略'的布爾屬性,它指定在碰撞計算中瓦片是否應該被忽略。我希望能夠讀取每個圖塊加載時的「忽略」屬性。像這樣的:在'平鋪'級別地圖(C#,Monogame)中訪問自定義屬性

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

但是我找不到一種方法來訪問這些屬性。任何人都可以幫忙嗎?謝謝!

編輯:這裏有一個小的文件我讀的內容:

<?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> 

回答

1

我設法解決這個問題。我發佈了我的整個Level課程,以防止任何人受到影響。

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); 
       } 
      } 
     } 
    } 
}