简体   繁体   English

基于二维平铺的游戏:每个平铺作为一个对象是不切实际的?

[英]2-Dimensional Tile-Based Game: Each tile as an object impractical?

I've been trying various ways of creating a two-dimensional tile-based game for a few months now. 我已经尝试了各种方法来创建一个二维的基于平铺的游戏几个月了。 I have always had each tile be a separate object of a 'Tile' class. 我总是让每个瓷砖成为“瓷砖”类的独立对象。 The tile objects are stored in a two-dimensional array of objects. tile对象存储在二维对象数组中。 This has proven to be extremely impractical, mostly in terms of performance with many tiles being rendered at once. 事实证明这是非常不切实际的,主要是在性能方面,同时渲染了许多瓷砖。 I have aided in this by only allowing tiles within a certain distance of the player being rendered, but this isn't that great either. 我已经帮助了这个,只允许玩家在一定距离内渲染,但这也不是那么好。 I have also had problems with the objects returning a null-pointer exception when I try to edit the tile's values in-game. 当我尝试在游戏中编辑tile的值时,我也遇到了返回空指针异常的对象的问题。 This has to do with the objects in the 2D array not being properly initialized. 这与2D阵列中未正确初始化的对象有关。

Is there any other, simpler way of doing this? 这样做还有其他更简单的方法吗? I can't imagine every tile-based game uses this exact way, I must be overlooking something. 我无法想象每个基于平铺的游戏都使用这种方式,我必须忽略一些东西。

EDIT: Perhaps LWJGL just isn't the correct library to use? 编辑:也许LWJGL只是不正确的库使用? I am having similar problems with implementing a font system with LWJGL... typing out more than a sentence will bring down the FPS by 100 or even more. 我在使用LWJGL实现字体系统方面遇到了类似的问题...键入多个句子会使FPS降低100甚至更多。

For static objects (not going anywhere, staying where they are) 1 tile = 1 object is OK. 对于静态对象(不会去任何地方,保持它们所在的位置)1 tile = 1对象就可以了。 That's how it was done in Wolf3d. 这就是在Wolf3d中完成的方式。 For moving objects you have multiple options. 对于移动对象,您有多个选项。

You can, if you really really want to, store object sub-parts in adjacent cells/tiles when an object isn't contained fully within just one of them and crosses one or more cell/tile boundaries. 如果您真的真的想要,当对象未完全包含在其中一个对象中并跨越一个或多个单元格/图块边界时,您可以将对象子部件存储在相邻单元格/图块中。 But that may be not quite handy as you'd need to split your objects into parts on the fly. 但这可能不太方便,因为您需要动态地将对象分成几部分。

A more reasonable approach is to not store moving objects in cells/tiles at all and process them more or less independently of the static objects. 更合理的方法是根本不将移动对象存储在单元格/切片中,并且或多或少地独立于静态对象处理它们。 But then you will need to have some code to determine object visibility. 但是,您需要使用一些代码来确定对象可见性。 Actually, in graphics the most basic performance problems come from unnecessary calculations and rendering. 实际上,在图形中,最基本的性能问题来自不必要的计算和渲染。 Generally, you don't want to even try to render what's invisible. 通常,您甚至不想尝试渲染不可见的东西。 Likewise, if some computations (especially complex ones) can be moved outside of the innermost loops, they should be. 同样,如果某些计算(特别是复杂的计算)可以移动到最里面的循环之外,它们应该是。

Other than that it's pretty hard to give any specific advice given so little details about what you're doing, how you're doing it and seeing the actual code. 除此之外,很难给出任何具体的建议给出关于你正在做什么,如何做和看到实际代码的细节。 You should really try to make your questions specific. 你应该真的尝试让你的问题具体化。

A two-dimensional array of Tile objects should be fine........ this is what most 2D games use and you should certainly be able to get good enough performance out of OpenGL / LWJGL to render this at a good speed (100FPS+). 一个二维Tile对象数组应该很好........这是大多数2D游戏使用的,你当然应该能够从OpenGL / LWJGL中获得足够好的性能以便以良好的速度渲染它( 100FPS +)。

Things to check: 要检查的事项:

  • Make sure you are clipping to only deisplay the visible set of tiles (According to the screen width and height and the player's position) 确保你剪裁只显示可见的一组图块(根据屏幕宽度和高度以及玩家的位置)
  • Make sure the code to draw each tile is fast... ideally you should be drawing just one textured square for each tile. 确保绘制每个图块的代码很快...理想情况下,您应该为每个图块绘制一个纹理正方形。 In particular, you shouldn't be doing any complex operations on a per-tile basis in your rendering code. 特别是,您不应该在渲染代码中基于每个tile执行任何复杂的操作。
  • If you're clever, you can draw multiple tiles in one OpenGL call with VBOs / clever use of texture coordinates etc. But this is probably unnecessary for a tile-based game. 如果你很聪明,你可以在一个OpenGL调用中使用VBO /巧妙地使用纹理坐标等绘制多个图块。但这对于基于图块的游戏来说可能是不必要的。

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

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