简体   繁体   English

如何存储 2D 瓷砖 map?

[英]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?将这个 map 存储在磁贴编辑器中的理想方法是什么? Currently I am using a multidimensional array of Rectangles as a single layer.目前我正在使用一个多维矩形数组作为单层。 A list of these tile layers consist of the map.这些瓦片层的列表包括 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.但是,这样做的问题是在编辑 map 时应该允许编辑 map 大小(宽度、高度和层数)。

Currently when someone edits the dimensions of the map I just create a new array with the specified dimensions.目前,当有人编辑 map 的尺寸时,我只需创建一个具有指定尺寸的新数组。 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.这工作正常,但现在我添加了撤消和重做支持,它开始使事情复杂化,因为每次用户更改 map 的尺寸时,我必须在每次更改之前存储整个 map 的副本。 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?最好只有一个 MAX map 大小并在启动时制作该大小的阵列,这样我就不必创建新的 arrays 并经常复制数据? What about maybe using a List of a List instead of a multidimensional array?也许使用 List 的 List 而不是多维数组呢?

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 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. 1)不会经常使用撤消/重做操作来更改 map 尺寸。 Usually, when editing a map, the dimensions do not change very often.通常,在编辑 map 时,尺寸不会经常更改。

2) Even if an undo/redo operation is used, re-creating the array need not be a large problem. 2) 即使使用了撤消/重做操作,重新创建数组也不一定是大问题。

You can store a "Tile" as its own object, and have each element in the array be a reference to the "Tile" object.您可以将“Tile”存储为它自己的 object,并让数组中的每个元素都是对“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.在 32 位计算机上,256x256 map 应该需要 256 KB 用于切片引用,而 1024x1024 map 需要 4 MB 用于切片引用。 The data in the tiles themselves can stay in memory and not be moved.瓦片本身中的数据可以保留在 memory 中而不会被移动。

A 4 MB array copy will not take any noticeable length of time on a modern computer.在现代计算机上,4 MB 数组副本不会花费任何明显的时间。

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.在更改尺寸之前,用户应保存一份 map 的副本。 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.同样,尺寸不会经常改变,并且由于地图在尺寸改变后通常会完全不同,因此用户可能倾向于拥有先前版本的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM