设计,关于游戏的 OOP
我正在制作一个小型视频游戏。我正在努力做好 oop/设计。
我有一个地图对象和一个相机对象。它们都是在“世界”对象中创建的。
但有一个问题。在地图中,我渲染并更新所有内容。出于性能原因,我只想更新/渲染仅在播放器屏幕上的内容。
相机对象具有此信息 - 但地图对象无法获取它。
我可以通过多种方式获取此信息,但我想知道如何获取此信息。
I'm building a small video game. I'm trying to do good oop/design.
I have a map object and a camera object. These are both created in the 'world' object.
There is a problem though. In the map I render and update everything. For performance reasons I only want to update/render what is only on the player's screen.
The camera object has this information - but the map object can't get to it.
There is a couple ways I could get this information, but wanted to get an option on how to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里可能最相关的面向对象原则是单一职责原则。
考虑这些责任声明:
Map
对象负责保存世界的布局Camera
对象负责维护用于观察世界的视点,让
Map
负责渲染屏幕上的内容显然超出了其管辖范围。您可能应该有一个
WorldRenderer
,它应该接收Map
和Camera
以便渲染屏幕。虽然我们讨论的是良好的设计,但您可能还希望使
WorldRenderer
本质上不可变 - 它将包含Map
和Camera 在构造时,从那时起,这些引用就不能更改,例如:
...您可能已经注意到该示例也提供了一些光源。同样,这是使用 SRP 将渲染过程与其组件元素解耦。
The OO principle that is probably most pertinent here is the Single Responsibility Principle.
Consider these statements of responsibility:
Map
object is responsible for holding the layout of the worldCamera
object is responsible for maintaining a viewpoint that is used to observe the worldGiven that, having the
Map
be responsible for rendering what's on-screen is clearly outside its jurisdiction.You should probably have a
WorldRenderer
, which should take in both aMap
and aCamera
in order to render a screen.While we're on the subject of good design, you might also want to make the
WorldRenderer
immutable in nature - it will take in aMap
andCamera
upon construction, and from that point on, those references can't be changed, e.g.:... you might have noticed that the example provides some light sources as well. Again, that's using SRP to decouple the rendering process from its component elements.
我会亲自更新所有内容,即使它不在视图中,这样当某些东西进入视图时它就已经处于其位置了。至于渲染,您始终可以实现自己版本的draw()或paint()方法,以仅绘制相机看到的东西。这只是某种简单的算法。
I would personally update everything, even if it is not in the view just so that when something does come into view it is already at its position. As for the rendering, you could always implement your own version of draw() or paint() methods to only draw things that are seen by the camera. It would just be some sort of simple algorithm.