3

I'm going to start working on a new game and one of the things I'd like to accomplish is a dynamic array sort of system that would hold map data. The game will be top-down 2d and made with XNA 4.0 and C#. You will begin in a randomized area which will essentially be tile based. As such a 2 dimensional array would be one way to accomplish this by holding numerical values which would correspond to a list of textures and that would be how it would draw this randomly created map. The problem is I would kind of only like to create the area around where you start and they could venture in which ever direction they wanted to. This would mean I'd have to populate the map array with more randomized data in the direction they go. I could make a really large array and use the center of it and the rest would be in anticipation of new content to be made, but that just seems very inefficient.

I suppose when they start a new game I could have a one time map creation process that would go through and create a large randomly generated map array, but holding all of in memory at all times seems also inefficient. Perhaps if there was a way that I'd only hold parts of that map data in memory at one time and somehow not hold the rest in memory. In the end I only need to have a chunk of the map somewhat close to them in memory so perhaps some of you might have suggestions on good ways to approach this kind of randomized map and dynamic array problem. It wouldn't need to be a dynamic array type of thing if I made it so that it pulled in map data nearby that is needed and then once off the screen and not needed it could somehow get rid of that memory that way I wouldn't have a huge array taking up a bunch of memory.

4

2 回答 2

2

我只是一个学生,没有任何游戏编程经验,但据我所知,你不能在使用时扩展数组。我想你需要在九个缓冲区(每个缓冲区,每个 NSEW 和对角线方向一个)之间来回移动资源,每个缓冲区都有 9 个图块。

例如:

玩家加载到一个区域中,并且位于 3x3 瓷砖阵列的中间,因此它们的瓷砖在各个方向上都是可见的。在后台,您计算周围缓冲区的纹理 ID 并准备好它们。

[  CENTER   ]
+---+---+---+
|   |   |   |
+---+---+---+
|   | X |   |
+---+---+---+
|   |   |   |
+---+---+---+

当玩家移出中心板块时,例如东边,他们将被定位在中心缓冲区的东边。

[  CENTER   ]
+---+---+---+
|   |   |   |
+---+---+---+
|   |   | X |
+---+---+---+
|   |   |   |
+---+---+---+

此时,将准备好向东方向的缓冲区沿中心缓冲区的边缘放置,使瓷砖重叠一个。

        [    EAST   ]
[  CENTER   ]
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+
|   |   | X |   |   |
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+

这使玩家能够进入东部缓冲区。在那里,东部缓冲区成为中心缓冲区,旧的中心缓冲区被重新循环为新的西部缓冲区。旧的西部缓冲区(以及与其垂直相邻的缓冲区)也可以回收成为新的东北部、东部和东南部缓冲区。

[  CENTER   ]
+---+---+---+
|   |   |   |
+---+---+---+
|   | X |   |
+---+---+---+
|   |   |   |
+---+---+---+

当然,您需要试验需要多少缓冲区,每个缓冲区有多少块,以及适当的回收/预处理时间何时可以提高性能,但我想这会奏效。

于 2012-10-27T03:19:49.800 回答
2

在我看来,在您确定问题确实存在之前,您正在尝试解决问题。

您是否真的尝试过以简单的方式(只是将所有内容加载到内存中)并看到它使用了太多内存?Quoth Knuth,“过早的优化是万恶之源”。

关键是,首先要专注于你想要完成的独特的事情,这就是你创造这个游戏的原因——肯定有很多东西可以让你忙碌。除非您对自上而下的 2D 滚动条进行了一些疯狂的范式转换,否则内存利用率不太可能成为限制因素——即使在智能手机等受限环境中也是如此。

如果您完成游戏开发后发现内存利用率实际上是一个问题,那么现在是开始优化的最佳时机。不要只关注你在问题中假设的解决方案(临时缓存和预取),而是要乐于寻找程序使用它不需要的内存的其他方式。

于 2012-10-27T04:55:49.133 回答