在 'Tiled' 关卡地图中访问自定义属性(C#、Monogame)
Accessing custom properties in 'Tiled' level map (C#, Monogame)
我已经使用 TiledSharp 库成功地将一个文件读入我的由 'Tiled' 关卡编辑器制作的平台游戏中。我终于成功地在我的游戏中加载并正确显示了一个关卡,但我不知道如何访问我在地图中的图块上设置的自定义属性。例如,我有一个名为 'Ignore' 的布尔值 属性,它指定在碰撞计算中是否应忽略图块。我希望能够在加载时读取每个图块的 'Ignore' 属性。像这样:
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>
我设法解决了这个问题。我发布了我的整个级别 class 以防它对任何人有帮助。
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);
}
}
}
}
}
我已经使用 TiledSharp 库成功地将一个文件读入我的由 'Tiled' 关卡编辑器制作的平台游戏中。我终于成功地在我的游戏中加载并正确显示了一个关卡,但我不知道如何访问我在地图中的图块上设置的自定义属性。例如,我有一个名为 'Ignore' 的布尔值 属性,它指定在碰撞计算中是否应忽略图块。我希望能够在加载时读取每个图块的 'Ignore' 属性。像这样:
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>
我设法解决了这个问题。我发布了我的整个级别 class 以防它对任何人有帮助。
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);
}
}
}
}
}