Java - How to read a custom map format

339 Views Asked by At

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?

1

There are 1 best solutions below

0
Sertage On

I think that may have 3 different ways to solve that:
First, you can use a Map with Maps: Map<Serializable,Map<String,Object>> where Serializable is your entity_id, and the map are the attributes that you need, like ("width",4), ("height",3):

public static final String WIDTH = "WIDTH";
public static final String HEIGHT = "HEIGHT";

...

Map<String,Object> mapProperties = new HashMap<String,Object>();
mapProperties.put(WIDTH, 4);
mapProperties.put(HEIGHT, 3);

....

Map<Serializable,Map<String,Object>> map = new  HashMap<Serializable,Map<String,Object>>();
map.put(myEntity.getId(), mapProperties);

Second way could be like this: http://java.dzone.com/articles/hashmap-%E2%80%93-single-key-and
Third way could be like this: Java Tuple Without Creating Multiple Type Parameters