Sign in to follow this  
Josip Mati?

Level file creation

Recommended Posts

Hello

 

Following my code refactoring spree for Battle City, I'd like to improve loading and storing of levels. After some limited searching (I'm on vacation with limited access to the Internet) I couldn't find anything that I could use.

 

Currently, my levels look like this:

[Level1]
S0W0000S0000W0S
000000ttt000000
0MGM000h000MGM0
0GGG00ygl00GGG0
0MGM00BBB00MGM0
000000BWB000000
GGGGGGGWGGGGGGG
WWWWWWGGGWWWWWW
GGGGGGGWGGGGGGG
000000BWB000000
0MGM00BBB00MGM0
0GGG00s0a00GGG0
0MGM0000000MGM0
000000fhd000000
00W001kCj200W00

Which is simple: read with double For loop char by char and create tile which that char represents.

 

There are few problems with it that I notice :

  • Amount of characters I can use for that is limited. Tiles like brick wall require not 1 but about 13 - one character for each variation of the tile.
  • Approach is ok for 15x15 tiles, but it may cause problems if I ever expand to a variable dimensions (with 15x15 being minimum). Not only the text file will become huge or at least wide, reading each level will become painfully slow with current system.

So, my questions are:

  1. Since I use XNA, is XML good enough for storing level data? I'd like to store tiles as either list of "type subtype position" or "type position_rectangle source_rectangle". I've been searching Internet a little and found a way to do a variation of that but for single level only, is there a simple way to do that for all levels in one file?
  2. This is probably too broad question and too low-level for C# - if I decide on writing a custom level data type, what are the general ideas behind it? I couldn't find anything or the Net (or my searching skills suck) and with my knowledge I only though of creating the binary file and write in it what I need or want.

Maybe I'm just thinking too hard.

 

Thanks in advance, even if the answer is just "search better" :)

Share this post


Link to post
Share on other sites

You can look into using JSON rather than XML as parsing them in C# is a breeze although to be honest it's probably easier (and faster, if file read/write really is a bottleneck) to serialize and deserialize your level data to some custom binary type. How are you going to be constructing your level files? With some custom tool or hand written? I.e. how important is "human readable" to your format needs?

Share this post


Link to post
Share on other sites

Given that it's just a tilemap, can't you just resort to an array of bytes? (you'd get 256 possible tile types, should be more than enough for you) True, you'd need to add a small header at the beginning indicating the size, but that should be trivial.

 

Of course you'd need to make a level editor to go along with it, so that'd be probably the hardest part. But you were planning to do that anyway, right? (I don't think you'll want to edit a tilemap from an XML or JSON file!)

Share this post


Link to post
Share on other sites

Given that it's just a tilemap, can't you just resort to an array of bytes? (you'd get 256 possible tile types, should be more than enough for you) True, you'd need to add a small header at the beginning indicating the size, but that should be trivial.

 

*facedesk*

 

Array of bytes did NOT occur to me. So I just write header + array into binary file, right?

 

Level editor is actually complete and working, I'll just need to modify it for a new type. However, code for loading and saving is a mess since I currently use simple text file - one huge Switch which creates new object of type Tile (custom written class).

Share this post


Link to post
Share on other sites

Can't you use some tile editor (such as this: http://www.mapeditor.org ) and adapt your code to load from its format?

 

I could, thanks for informing me of it.

 

However, for the first major project I'd like to make everything myself in order to grasp the basics behind it ;) So, page is bookmarked for later projects.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this