2d, Tile-based Game


Vorporeal

Yes, no, I, this is.
Joined
Sep 13, 2007
Messages
1,614
Age
33
Website
Visit site
So I'm planning on writing a 2D, tile-based game (as the title suggests). For right now, I'm working on it as my final project for a CS class, but chances are it will eventually turn into a Pandora "title". I've got some questions on implementation of various parts that I'm having trouble with.

What would be the best way to do maps? I'm thinking on implementing the map data within the program as objects that contain other objects (tree, rock, player, monster, whatever). I'm not sure how I should format the data files that contain the maps (probably one map per file, at least for now) in such a way that it is easy to make new maps and load the data into my program?

That's the only question I can remember off the top of my head. Any suggestions are welcome. (I'm doing this in Java, if that makes a difference.)
 
I would use a two dimensional array for the map itself where each element is an object that contains information about that tile e.g. collision and what static objects are on this tile.
If you want to move something around the map only tile by tile you can put this into this map structure too.
For pixel movement i would use a seperate list of objects.

As for saving the map i would go for a custom format either binary or textbased.
Either way you would eventually need some kind of editor if you don't want to write large maps by hand.

Furthermore you should think about what type of tilemap you want. 2D, Isomap or Hexagonal shaped though with a good design you can support all of this types.

Thats anything i can think of without further knowing your idea.
 
The idea is to have a simple, rectangular based map.

Reading in and parsing the array is easy. What I'm mostly stuck on right now is how to parse an "object" from a text (or binary) file containing the map data. I guess the easiest way would be to have a strict number and order of parameters following the object name, and a switch statement in the method that reads the maps to determine how to read the object after determining what the type of the object is based on the first specification line?
 
As i remember from my java experience for a fast and simple binary saving method you could use the objectserializer (don't remember how exactly this was called) where you only have to write Serializer.write(objectarray) and everthing is done for you.
The downside would be no control over the layout of the file so you have to write an editor for it.
You could parse the file yourself as you suggested.
I like to use a main read loop where i read the count of the object and an identifier which object to use and pass the stream onwards to the specific loading function of that object (most time a static function of that object).

Another idea would be a factory pattern where you read all data for an object like key=value and pass it into a createobject function.

On thing that i liked from C# (don't know if it exists in java) was the XmlSerializer where i could tell which of the attributes of my classes should be saved to file and the XmlSerializer did the rest for me much like the ObjectSerializer but as humanreadable xml.

Hope anything of that helps you :)
 
As billykater said, serialization is your friend. You may also want to look at a 3rd party library for your project if the particulars of your CS assignment permit it. This one is designed for games such as you describe (free as in beer, I don't think it's open source) and is pretty simple to use. I'm sure there are others.

Edit: It seems it is open source, it uses the LGPL.
 
EDIT: Whoops, misunderstood what you needed. >_>

EDIT^2: Well, I still can't really figure out what exactly you're asking for so here's the original post I came up with, hopefully that's helpful and not way too basic and unhelpful and not what you needed at all. :p

A favourite of mine is to do this:

Create a dictionary keyed with the names of the objects that contains a reference to all the data the object needs. (bitmap, drawing offsets, etc) Then when saving your map create a header that links all names used in the map to a unique number, such as:

"TREE"<null><uint16>

Of course, I use fixed width names (8 characters or less, padded with spaces if shorter) and so I don't have the null byte terminating the string, but with variable width names you'll need that to know when to read the next int, obviously.

Then you simply iterate through X and Y for every layer and write out the tiles or objects occupying a given tile as integers corresponding to the header. Don't forget to write out the map width, height and number of header entries at the start of the file, of course.

When loading you can then load the header into an array, load the required objects into your dictionary, and fill the header array with references to the objects, then its simply this: (in pseudocode)

CODE

for (y = 0; y < mapheight; y++) {
for (x = 0; x < mapwidth; x++) {
tile[x,y] = headerarray[file.readuint16()] // if the array contains references
}
}


Um, anyway this is just my personal way of doing things, hope that helps.
 
Eniko: Why do all that when you could just serialize the map array? I'm not particularly familiar with Java, is there some problem with serialization?
 
Klepto said:
Eniko: Why do all that when you could just serialize the map array? I'm not particularly familiar with Java, is there some problem with serialization?
Well I'm not familiar with Java either, and when I think serialization I think of pickling, and when I think of pickling I think of a LOT more data being saved than being strictly necessary? And I find that sloppy.
 
Last edited by a moderator:
Eniko said:
Well I'm not familiar with Java either, and when I think serialization I think of pickling, and when I think of pickling I think of a LOT more data being saved than being strictly necessary? And I find that sloppy.

Fair comment, I assume from your terminology you are a Python programmer. I should point out that writing a serialization library for a reflective object orientated language should be fairly simple, I did one for Ruby in about 200 lines of code. If the default serialization methods don't meet your needs then rolling your own can be an option, which is what you are doing I suppose :)
 
Last edited by a moderator:
Yeah, serialization is definitely a possibility. One thing I could always do (and might resort to this, at least in the beginning) is write an editor that I can use to make the maps, then write them out using Java's serialization (ObjectWriter and the like). When I want to use them in my program, I just read them in and everything's done. It's a very simple way of doing it, but as stated, probably results in larger than necessary map files.
 
Serialization has a lot of other flaws as well, having to write new versioning information and then code when your code changes can be a major hassle and get very large rather quick, while if you save the minimal amount of required information from outside (load method, save method) you only need to call deprecated_load(file), save(file) and you're up to speed again.

I mean, it's a neat concept but be careful not to over apply it; just because something could be serialized doesn't necessarily mean you should.

Besides if you store your object definitions/information seperate from the map you can change the object properties and the changes will reflect in all maps without needing to take any further action. If you actually serialize entire objects and their properties then changing a property means you would have to load, change, and resave all maps just to reflect this one tiny change, which is obviously far less robust.
 
Thanks Eniko, I now understand your reservations in regard to serialization. All the problems you have mentioned can be worked around within a serialization library but I agree that the default serialization functions in the languages I am familiar with are pretty simple and don't handle details like that. Writing an enhanced serialization library probably isn't worth the effort if you just want to save a map file :)
 
I talked with the TAs for the class for a bit, and it seems like I can probably use some sort of XML parser to do the map data. That makes things a lot easier though. It's still a bit of a pain, I guess (really large switch statements), but still much easier than defining my own file format.
 
Yeah, I was actually looking through that as a reference. The tile part is a bit annoying, but I'm also trying to do some minimal MMO stuff. I've been doing some testing of multi-client networking stuff, but figuring it out in the context of a game seems like the complexity grows exponentially. (A bit overkill for an independent project, I know, but why not?)
 
Back
Top