I think that depends entirely on how you use the information contained in the file. Here's what I'm currently doing for my 2d platformer:
, not a lua file. This creates a
. This is because I just want to know the numbers of the tiles, but none of the code that Tiled adds. I want a clean file.
that I wrote myself, that looks in the directory, and merges the contents of all the CSV files into one
, with all the desired code and formatting that I want, but with the two layers as separate variables that the game can load.
, which is basically a lua file with a lookup table, that tells my game exactly what each tile is. So any wall tiles that I place in Tiled, are automatically added as collision, thanks to the reference file. The game basically checks every tile that it loads, against the reference file, and processes the tile in accordance with my rules.
Code: Select all
-- Collision reference sheet for the tilesheet
--"X"=wall--"."=floor--"W"=water--"SW"=shallowwater--"L"=lava--"F"=firepit--"E"=emptyfirepit
--"P"=pit--"T"=spiketrap--"S"=stairs--"U"=hammerblockup--"D"=hammerblockdown
--"C"=chest--"I"=blockup--"O"=blockdown--"B"=blockswitch--"Q"=pressurepad
--"V"=vase--"R"=button--"M"=MoveableObject--
local collisionsheet={
{ "X","X","X","X","X","X","X","X","X","X","P","P","P","X","X","X","X"},
{ "X","X","X","X","X","X",".",".","X","X","P","P","P","X","X","X","X"},
{ "X","X","X","X","X","X",".",".","X","X","P","P","P","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","P","P","X","X","X","."},
{ "X","X","X",".",".",".",".",".",".",".","X","P","P","X","X","X","."},
{ "X","X","X",".",".",".",".",".",".",".","X","X","X","X","X","X","Q"},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X",".",".",".","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","W",".",".","X","P"},
{ "X","X","X","X","X","X","X","X","X","X","X","X","S","S","S","S","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","S","S","S","S","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","S","S","S","S","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","S","S","S","S","."},
{ ".",".",".",".",".",".","X","X","X","X",".",".",".",".",".",".","X"},
{ ".",".",".",".",".",".","X","X","X","X",".",".",".",".",".",".","."},
{ ".","X","X","X","X","X","X","E","F","F","F","X","X","X","T","T","T"},
{ ".","X","X","X","X","X","X","O","I","I","O","I","I",".",".","X","S"},
{ "L","SW","SW","SW","X",".",".",".","X",".",".",".",".","B","B","X","S"},
{ "L","X","X","X","X",".","X","X","X",".","X","X","X",".",".",".","S"},
{ "L","X","X","X",".",".",".","X","X","X","X","X","X",".",".",".","W"},
{ "X","X","X","X","X","X","X","X","X","X","X","X",".",".",".",".","."},
{ "X","X","X","X","X",".",".","X","X","X",".",".",".","D","U","U","U"},
{ ".",".",".",".","X",".",".",".",".",".",".",".","S","S","S","S","C"},
{ ".",".",".",".",".",".",".",".",".",".",".",".","S","S","S","S","X"},
{ ".",".",".",".","S","S","X","X",".",".",".",".","X","X","X","X","C"},
{ "C","C","X","X","S","S","X","X",".",".",".",".","X","X","X","X","X"},
{ ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","V"},
{ ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."},
{ ".",".",".",".",".","P","P","P","P",".",".",".",".",".",".",".","."},
{ ".",".",".",".",".","P","P","P","P",".",".",".",".",".",".",".","."},
{ "S","S","S","X","X","X","X","X",".",".",".","S","S","S",".","X","X"},
{ "X","X","S","S","X","X","X","X",".",".",".",".",".",".",".","X","X"},
{ "X","X","S","S","X","X","X","X",".",".",".",".",".",".",".","X","X"},
{ "X","X","S","S","X","X","X","X",".",".",".",".",".",".",".","X","X"},
{ "S","S","S","S","S","S","S","S","X","X","X","X","X",".",".",".","."},
{ "S","S","S","S","S","S","S","S","X","X","X","X","X",".",".",".","."}
}
return collisionsheet
And here's the tilesheet that this file refers to. In this example I used some classic Zelda tiles for this. As you can see, the reference file tells the game exactly what the tile is supposed to be. "X" for a wall, or "." for a floor tile, or "L" for hot boiling lava.
I do the same with the entities layer, with the difference that entities simply add functionality, and are usually not added to the spritebatch. This allows me to add things on top of the normal tiles, such as enemies, items, invisible zones, water and ladders... etc.
Since all of this information is only processed upon loading the map, none of this really affects performance during the game.
So, you don't need to have a separate layer in Tiled for collisions. That seems like an awful lot of work. If all wall tiles are always considered collision by your game, then that is one layer that you can leave out completely.
And one thing that I did in my current project, was add a "fake-wall" tile to the entity layer. This tells my game that when it looks up a tile, which is considered a wall, but it has a "fake-wall" tile on top of it in the entities layer, that those tiles do not need to be added to the collision. It is basically checking each tile against two reference files, and deciding what to do with it upon loading the map.