Tiled中的Tile ID来自哪里

Where do the Tile IDs come from in Tiled?

本文关键字:ID 中的 Tile Tiled      更新时间:2023-09-26

我过去曾使用Tiled为Phaser游戏构建JSON tilemap文件。我现在想用PHP动态构建我的JSON映射文件。

下面是一个示例对象:

{
    "height": 50,
    "layers": [{
        "data": [5884, 5885, 5886, 5887, 5888, 5885],
        "height": 50,
        "name": "background",
        "opacity": 1,
        "type": "tilelayer",
        "visible": true,
        "width": 50,
        "x": 0,
        "y": 0
    }],
    "orientation": "orthogonal",
    "properties": {},
    "tileheight": 16,
    "tilesets": [{
        "firstgid": 1,
        "image": "tiles.png",
        "imageheight": 1684,
        "imagewidth": 2738,
        "margin": 1,
        "name": "tiles",
        "properties": {},
        "spacing": 1,
        "tileheight": 16,
        "tilewidth": 16
    }],
    "tilewidth": 16,
    "version": 1,
    "width": 50
}

这些属性非常不言自明,并记录在Github上,Github引用了Tiled网站上的TMX Map格式文档,但都没有解释层的data属性的tile id是如何生成的。

两个问题:

1) 如何为"数据"数组生成平铺ID?例如,如果我有一张5x5的瓷砖,它们会从左到右,从上到下只是1到25吗?

2) 数据数组使用全局id,全局id在所有平铺页中都是唯一的。这些是如何产生的?例如,如果我有3块5x5瓷砖,它们会是1-75吗?但顺序是什么?

基于测试,我将根据Tiled 0.16.1的行为以相反的顺序回答您的问题。

您可能还想在最初添加单独的图像,并使用XML输出(*.tmx),因为在我看来,这比JSON输出更容易查看数据格式。我提供以下一个:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="64" tileheight="64" nextobjectid="1">
 <tileset firstgid="1" name="second" tilewidth="64" tileheight="64" tilecount="3" columns="0">
  <tile id="0">
   <image width="64" height="64" source="../../../OneDrive/Projects/Tiles/forest.png"/>
  </tile>
  <tile id="1">
   <image width="64" height="64" source="../../../OneDrive/Projects/Tiles/forest-dirt-corner-ne.png"/>
  </tile>
  <tile id="2">
   <image width="64" height="64" source="../../../OneDrive/Projects/Tiles/forest-dirt-corner-nw.png"/>
  </tile>
 </tileset>
 <tileset firstgid="4" name="third" tilewidth="64" tileheight="64" tilecount="1" columns="0">
  <tile id="0">
   <image width="64" height="64" source="../../../OneDrive/Projects/Tiles/grass.png"/>
  </tile>
 </tileset>
 <tileset firstgid="5" name="first" tilewidth="64" tileheight="64" tilecount="2" columns="0">
  <tile id="0">
   <image width="64" height="64" source="../../../OneDrive/Projects/Tiles/dirt.png"/>
  </tile>
  <tile id="1">
   <image width="64" height="64" source="../../../OneDrive/Projects/Tiles/dirt-forest-corner-ne.png"/>
  </tile>
 </tileset>
 <layer name="Tile Layer 1" width="10" height="10">
  <data encoding="csv">
1,2,3,0,0,0,0,0,0,0,
4,0,0,0,0,0,0,0,0,0,
5,6,0,0,0,0,0,0,0,0,
1,2,3,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,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>
</map>

2.如何生成id

首先,重要的是要理解存在一个或多个具有firstgid属性的tileset元素。根据文件:

firstgid:此tileset的第一个全局tile ID(此全局ID映射到此tileset中的第一个tile)。

另外:

第一个CCD_ 4总是具有CCD_。

tileid:作了进一步的解释

id:其tileset中的本地tile id。

因此,在上面的示例中,您将看到tileset firstgid="1"中有三个tile元素,其中ids的范围从0到2。计算一下,你会发现这些瓷砖的唯一id分别为1、2和3。

下一个tilesetfirstgid为4,因为具有id的1、2和3的瓦片已经在之前。其中的第一个tileid为0,由于4+0=4,我们知道我们的第四个tile是什么

如果tilesets在Tiled接口中移动,那么id也会相应地更新。因此,第一个瓦片集中的第一个瓦片将始终具有id 1。

例如,如果我有3块5x5瓷砖,它们会是1-75吗?但顺序是什么?

所以是的,这将基于瓷砖片被添加到的瓷砖集的顺序。

1.如何为data数组生成id

id是基于生成的id,这与上面回答的第二个问题有关"0"表示没有平铺,而"1"将是第一个平铺集中的第一个平铺(左上角)。

因此:

例如,如果我有一张5x5的瓷砖,它们会从左到右,从上到下只是1到25吗?

是的。