melonJS icon indicating copy to clipboard operation
melonJS copied to clipboard

Support JSON format maps

Open parasyte opened this issue 14 years ago • 16 comments

It looks like Tiled supports exporting TMX to JSON since forever. We should really take advantage of this!

Also, it would be great to switch to JSON format "by default", because it won't require an XML parser. That means loading maps will be faster when using JSON. Also, CocoonJS doesn't support XML parsing. See: https://groups.google.com/d/msg/melonjs/VfXZ5LzTtzQ/B4LZ8_3Uc-cJ

The only problem I see with using JSON format maps by default is the file size. But maybe that's not really an important issue. The JSON output in Tiled is "readable" by adding a bunch of unnecessary whitespace. When I stripped the whitespace from my test map, the resulting file was actually SMALLER than the XML with Base64:

-rw-r--r-- 1 parasyte staff 7143 Nov 11 13:15 farm.json -rw-r--r-- 1 parasyte staff 3386 Oct 14 14:54 farm.tmx -rw-r--r-- 1 parasyte staff 2844 Nov 11 13:26 farm2.json -rw-r--r-- 1 parasyte staff 3260 Nov 11 13:31 farm2.tmx

"farm.json" is the original output from Tiled. "farm2.json" is the same file with unnecessary whitespace stripped. And "farm2.tmx" is the same as "farm.tmx" but with unnecessary whitespace stripped. JSON is the clear winner!

We could always recommend a "build step" in the tutorial/FAQ/documentation that strips whitespace from the JSON files. There are other important savings that could be had with concatenating source files and minifying, as well.

parasyte avatar Nov 11 '12 21:11 parasyte

This would definitely be a good feature for the framework. I would really like to help in the implementation of it, but all these TMX and Tiled classes are so messed up and undocumented that I can't understand anything. I guess new classes for parsing JSON maps need to be created. Can you guys give us more light into the tree of this classes, what each one is doing and what needs to be done/refactored?

nvlbg avatar Jan 31 '13 16:01 nvlbg

yep, that's definitely something i'm looking forward to as well for the next 0.9.6 version !

now in order to understand the current implementation :

  • you should probably start by having a look here, as it will allow you to understand how a TMX file is architectured, and then explain some choice in implementation
  • a second point to consider, is that the code has been somehow made to allow a future integration with another map format (you will notice that the TMXLayer inherit from the Layer Object). This might not be actually useful, and I would tend to agree if we would merge back those kind of classes to simplify the code (anyway at this point I don't really what other format could be added)
  • private piece of code, yes are poorly documented, it's not an excuse as doc can be added while kept private, but I just did not pay any special attention in documenting properly these parts.

then, in terms of addiing support for JSON, my idea was more to work around the TMXParser : today it only support XML files, but adding the JSON format to it would allow make this completely abstract from the rest of the code, as they would only request for a specific attributes and corresponding value, wihtout having to care about the format itself. I beleive that this is a way better approach, instead of rewriting the whole thing.

ghost avatar Feb 01 '13 08:02 ghost

@all @parasyte @nvlbg

Awesome news : http://blog.ludei.com/ Native support for XML loading is there :)

ghost avatar Feb 12 '13 06:02 ghost

That's great, but I still think it is really good for adding support for JSON format. I'll try adding it some in the future when I find time. For me it feels wrong to use XML in javascript when you can do it with JSON :laughing:

nvlbg avatar Feb 12 '13 11:02 nvlbg

@nvlbg personnaly I like to use XML, as during the level design phase it allows to easily test any changes just by doing a CTRL+S in Tiled and F5 in the browser. JSON could however be used when deploying the game for the here above listed benefits.

ghost avatar Feb 12 '13 11:02 ghost

@obiot et al. It deserves mention here, but exporting JSON is just as easy in Tiled; CTRL+E. ;)

parasyte avatar Feb 12 '13 16:02 parasyte

A few limitations I'm noticing :

  • the backgroundcolor property is not currently exported by Tiled 0.9 (should be there in 0.9.1)
  • both TMX and TSX file must use the same format (e.g. a XML map cannot use a JSON TSX file)

ghost avatar Mar 06 '13 01:03 ghost

@parasyte

amazing, I'm running right now the platformer example on my iphone using cocoonjs ! which means JSON parsing is done ! well it might still requires some code cleaning and deep testing to ensure nothing is missing, but so far so good ;)

I'll merge branches a bit later, and i'm so happy about it ! ;)

ghost avatar Mar 07 '13 07:03 ghost

WOOOOOO!!!! That's awesome news. How is the performance on CocoonJS compared to Safari? (Not sure you'll be able to tell right away with the platform demo, but thought I would ask!)

I'll do a quick code review for you tonight before sleep!

parasyte avatar Mar 07 '13 07:03 parasyte

for the performances, so far i'd say the same, but since all my demos are currently keyboard based games, i don't really have anything to really try it for now ...

that would be good for the code review, and that makes me think that I commented the polygon support in JSON :) be sure to check branch 140, in case i did not merge it yet!

ghost avatar Mar 07 '13 07:03 ghost

Feature request for Tiled to support exporting JSON-format tilesets (TSX): https://github.com/bjorn/tiled/issues/381

parasyte avatar Mar 07 '13 08:03 parasyte

Done with quick review! :D Good work, I like what I saw. I will give it some testing tomorrow maybe.

parasyte avatar Mar 07 '13 09:03 parasyte

thanks! honestly it was not an easy task, and there is still some code that can be optimized (not to mention the weird stuff you saw that need fixing).

I'll keep testing it this evening and eventually merge it if I feel happy about it :)

ghost avatar Mar 07 '13 09:03 ghost

Branch 140 merged !

Current limitations :

  • the backgroundcolor property is not currently exported by Tiled 0.9 (should be there in 0.9.1)
  • TSX tileset file are not exported when using JSON, see bjorn/tiled#381

Todo/known issues :

  • ~~fix the weird object issue when parsing tileset & layers (there is a workaround now that mask it)~~
  • ~~polygon/polyline support (currently disable)~~

ghost avatar Mar 07 '13 11:03 ghost

except from the two above limitations, JSON parsing should be complete now :)

ghost avatar Mar 11 '13 08:03 ghost

Closing the ticket, as everything seems to be working flawlessly :)

ghost avatar Mar 28 '13 05:03 ghost