Google Summer of Code 2022 - The ENIGMA Team

Week 2 & 3: Importing Tiled .tmx files, the first iteration

Contributor: Kartik Shrivastava
Project: Add Tiled compatibility to/from Enigma
Guided by: bjorn, fundies and Josh

Highlights:
  1. Resolved(sort of) the confusion regarding implementation of tsx importer (took a while)
  2. Added an implementation based on idea of existing gmx/tsx importer (didn't last tho) #0ea925f
  3. Scrapped the initial implementation for a better one - first working iteration (little fragile atm) #477adc1



A side note

What you can learn from this as a casual reader?

Details

Removing the confusion😵

2nd week of GSoC started with a lot of confusion regarding the way I should implement the importer. In the first week I had a plan to just write it like tsx importer, but that plan went haywire after I started analysing Room.proto carefully. I wasn't able to make use of initial plan mainly due to presence of repeated fields in Room.proto, as there is no direct connection between Tiled elements and the repeated fields present in Room.proto, so I had to do some more homework (research).

It took me couple of days but the discussion with mentors and the connections uncovered (between Tiled elements and Room fields) during those days helped me get through. Here are few important ones:



Now which way to go? Just go and scrap it😝

That's exactly what I did, without worrying way too much about what I'm doing or most importantly "it will work or not", I tried to proceed with the connections I made in last few days. TBH I'm not certain if this is final usable version, but at least it gave some base to improve upon.

So I started with the pugixml traverse method, traversing the tmx nodes one-by-one. A little modification to existing tsx importer was made to support both types of tilesets, and also made it accessible to other objects which was private initially.

Original version

Then Room optional fields was linked with similar Map attributes.

Original version

After testing that Backgrounds are loading properly and Room is loading properly with optional fields, its time to add tiles to Room.tile repeated field.
But at this point problems start to appear in the current implementation, and there are couple of them.

Original version

So I felt that its time to say good bye to this approach and think of a one which is less complex and takes less number of(or just single) iterations of tmx file.



🤔How about iterating only required xml node tree & in the order we want?

Now the big change is not to use pugi traverse method and not to traverse tmx xml tree node-by-node from top to bottom, but rather iterate it according to our use case. To do that a custom Load method is added which looks like this:

Original version

At first to resolve issue of not having Backgrounds to refer while filling Room.proto, we will initially traverse all the tileset nodes present in tmx file.

Now we will traverse map node and start to fill Room.proto. At the end we will handle the fields which needs to be set explicitly, without need of any if-else chain.

At the end we will traverse all the map.objectgroups.objects (using LoadObjects method) from the given Room instance one-by-one and fill the Room.tile repeated fields correctly, again we have eliminated lot of if-else complexity.

So, that's how a working first iteration of tmx importer is created. There are lot of missing bits and TODOs which will be addressed in coming weeks. :)