How to store a 2D tile map?
Consider the tiles and layers from Legend of Zelda: A Link To The Past. What would be an ideal way to store this map in a tile editor? Currently I am using a multidimensional array of Rectangles as a single layer. A list of these tile layers consist of the map. Every rectangle corresponds with the rectangle from the tileset. However, the problem with this is that editing the map size (width, height, and layer count) should be allowed while editing the map.
Currently when someone edits the dimensions of the map I just create a new array with the specified dimensions. This works fine and all but now that I am adding Undo and Redo support it's beginning to complicate things because every time the user changes the dimensions of the map I have to store a copy of the entire map before the change each time. Now I am considering other ways.
Would it be better to just have a MAX map size and just make the array that size at launch so I would not have to be creating new arrays and copying over data so often? What about maybe using a List of a List instead of a multidimensional array?
I'm not sure how I feel about my current setup. Initially I was fine with it but now I'm having second thoughts. I am not currently noticing a slowdown at all so perhaps I'm doing premature optimization (which is bad of course) and should just forget about开发者_Go百科 this whole post. I'm not sure. I would like to hear what you all think though.
For the purposes of Undo/Redo operations, you can safely assume that optimization is not necessary for this, for the following reasons.
1) It won't be very often that an undo/redo operation will be used to change map dimensions. Usually, when editing a map, the dimensions do not change very often.
2) Even if an undo/redo operation is used, re-creating the array need not be a large problem.
You can store a "Tile" as its own object, and have each element in the array be a reference to the "Tile" object. On a 32-bit computer, a 256x256 map should require 256 KB for the tile references and a 1024x1024 map would be 4 MB for the tile references. The data in the tiles themselves can stay in memory and not be moved.
A 4 MB array copy will not take any noticeable length of time on a modern computer.
If the data sizes must be larger, then the best solution is to warn the user that changing the dimensions cannot be undone. The user should save a copy of the map before changing dimensions. Again, the dimensions will not change very often, and since maps are generally radically different after the dimensions change, so the user may be inclined to have a copy of the previous version anyway.
精彩评论