Level file creation

Started by
5 comments, last by Josip Mati? 10 years, 8 months ago

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" :)

Advertisement

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?

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!)

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

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).

http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx

If you are familiar with streams these are very easy to use to create simple custom binary files.

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

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

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.

This topic is closed to new replies.

Advertisement