ponywolf / ponytiled

PonyTiled a simple Tiled Map Loader for Corona SDK
MIT License
83 stars 22 forks source link

Differences in rendering collection of images between json and lua format #12

Open ldurniat opened 6 years ago

ldurniat commented 6 years ago

Hi,

this problem is mentioned in this thread on Corona forum. I believe I find easy solution. In function gidLookup you only need to line 150:

if tonumber(k) == (gid - firstgid + 0 ) then

replace with

if (v.id or tonumber(k)) == (gid - firstgid + 0 ) then

NOTE:

I found that your solution don't work in every case since tile ids may be not consecutive numbers. Like in example below:

return {
  version = "1.1",
  luaversion = "5.1",
  tiledversion = "1.0.3",
  orientation = "orthogonal",
  renderorder = "right-down",
  width = 25,
  height = 40,
  tilewidth = 22,
  tileheight = 22,
  nextobjectid = 1,
  properties = {},
  tilesets = {
    {
      name = "dsfsdfdsf",
      firstgid = 1,
      tilewidth = 22,
      tileheight = 22,
      spacing = 1,
      margin = 33,
      image = "spritelib_gpl/fishdish/fishbaddie_parts.png",
      imagewidth = 640,
      imageheight = 480,
      tileoffset = {
        x = 0,
        y = 0
      },
      grid = {
        orientation = "orthogonal",
        width = 22,
        height = 22
      },
      properties = {},
      terrains = {},
      tilecount = 494,
      tiles = {
        {
          id = 0,
          properties = {
            ["property_1"] = "property_value"
          }
        }
      }
    },
    {
      name = "gdfgdfgdfgdfg",
      firstgid = 495,
      tilewidth = 640,
      tileheight = 480,
      spacing = 0,
      margin = 0,
      tileoffset = {
        x = 0,
        y = 0
      },
      grid = {
        orientation = "orthogonal",
        width = 1,
        height = 1
      },
      properties = {},
      terrains = {},
      tilecount = 3,
      tiles = {
        {
          id = 0,
          image = "spritelib_gpl/fishdish/fishbaddie_parts.png",
          width = 640,
          height = 480
        },
        {
          id = 1,
          image = "spritelib_gpl/fishdish/fishsalmon_parts.png",
          width = 640,
          height = 480
        },
        {
          id = 3,
          image = "spritelib_gpl/fishdish/fishtitle_parts.png",
          width = 640,
          height = 480
        }
      }
    }
  },
  layers = {
    {
      type = "tilelayer",
      name = "Tile Layer 1",
      x = 0,
      y = 0,
      width = 25,
      height = 40,
      visible = true,
      opacity = 1,
      offsetx = 0,
      offsety = 0,
      properties = {},
      encoding = "lua",
      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,
        495, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 496, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        133, 134, 0, 0, 0, 0, 0, 0, 54, 55, 56, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 498, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 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
      }
    }
  }
}

I first added four images to tileset and then removed 3rd image. After that I saw in Corona Simulator only two images. One image was missing. All three images were visible in Tiled Editor.

Have a nice day:) ldurniat

abigpotostew commented 6 years ago

I also ran into this. The issue is reproduced by adding images to a tileset, then removing some, then adding more, etc. This leads to non consecutive IDs for the images, and breaks your logic in findLast function in ponytiled.lua as well as gidLookup on this line if gid >= firstgid and gid <= lastgid then which assumes the gid is consecutive starting at 0 (or 1, not sure) when in fact the gid can start at any value and still be valid.

For now the workaround is to recreate the tileset and only add images, never remove. If any have been removed then recreate the tileset.

superqix commented 6 years ago

Is this just in the LUA format? Not something I use very often, but the JSON allows for removal and re-adding of tiles... I'll check into it. Maybe someone can upload a sample?

ldurniat commented 6 years ago

Tha JSON format is also affected by this issue.

I re-added last image shield.png.

Example:

{ "height":20,
 "infinite":false,
 "layers":[
        {
         "draworder":"topdown",
         "name":"Object Layer 1",
         "objects":[
                {
                 "gid":1,
                 "height":128,
                 "id":1,
                 "name":"",
                 "rotation":0,
                 "type":"",
                 "visible":true,
                 "width":128,
                 "x":157.333333333333,
                 "y":340
                }],
         "opacity":1,
         "type":"objectgroup",
         "visible":true,
         "x":0,
         "y":0
        }],
 "nextobjectid":2,
 "orientation":"orthogonal",
 "renderorder":"right-down",
 "tiledversion":"1.1.5",
 "tileheight":32,
 "tilesets":[
        {
         "columns":0,
         "firstgid":1,
         "grid":
            {
             "height":1,
             "orientation":"orthogonal",
             "width":1
            },
         "margin":0,
         "name":"fgdfg",
         "spacing":0,
         "tilecount":3,
         "tileheight":128,
         "tiles":
            {
             "0":
                {
                 "image":"heart.png",
                 "imageheight":128,
                 "imagewidth":128
                },
             "1":
                {
                 "image":"gem.png",
                 "imageheight":64,
                 "imagewidth":64
                },
             "3":
                {
                 "image":"shield.png",
                 "imageheight":64,
                 "imagewidth":64
                }
            },
         "tilewidth":128
        }],
 "tilewidth":32,
 "type":"map",
 "version":1,
 "width":20
}

Images come from https://github.com/coronalabs/Sticker-Knight-Platformer/tree/master/scene/game/img.