0
\$\begingroup\$

I am trying to create a custom map format for my own little 2D RPG, so my question is rather how do I manage reading and creating a custom map format properly and flexible. First off, I am writing my code in Java. The idea was to have a class called 'TileMap'. This class defines a 2-dimensional integer - array where all my entities are stored ( I'm using an entity-system to realize my game ). I also want to save and parse some information about the size of the map before the actual reading process happens. The map file should look much like this:

#This is a test map
width=4
height=3
layercount=1
tilesize=32

[1;0;0;0]
[23;1;0;0]
[5;0;1;0]

where layercount is the number of layers the z-dimension offers. and tilesize is the size of every tile in pixels. Entities are defined in between the brackets. The pattern goes: [entity_id;x_pos;y_pos;z_pos]. I already wrote the code to parse a file like this but its not very flexible because you just have to put one tiny whitespace in front of the square brackets and the map can't load up. I just need some few helpful tips to do this in a flexible way. Can anybody help me out?

\$\endgroup\$
1
  • \$\begingroup\$ Ultimately this comes down to personal preference and "what works for you". The important thing is to actually try something first. If it works - great! If not try the alternative. Simple. As this is mostly opinion based I'm voting to close the question. :) Try on of the above first, if you have issues implementing it specifically then we can help! \$\endgroup\$ Commented May 18, 2015 at 8:47

1 Answer 1

0
\$\begingroup\$

You want to look into parsing, which is a very well documented field.

To start off simply though, you might parse the file line-by-line, stripping '#' to the end of the line, and stripping any padding whitespace. This solves the issue of "its not very flexible because you just have to put one tiny whitespace in front of the square brackets".

You can then recognize (either via a search or, better, via regular expressions) and parse each type of line (key=value lines, [x;y;z] data lines) as it comes.

Regular expressions will allow you to easily define a simple grammar for the lines and as a bonus, easily extract the data from it.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.