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.
The OO principle that is probably most pertinent here is the Single Responsibility Principle.
Consider these statements of responsibility:
Mapobject is responsible for holding the layout of the worldCameraobject is responsible for maintaining a viewpoint that is used to observe the worldGiven that, having the
Mapbe responsible for rendering what’s on-screen is clearly outside its jurisdiction.You should probably have a
WorldRenderer, which should take in both aMapand aCamerain order to render a screen.While we’re on the subject of good design, you might also want to make the
WorldRendererimmutable in nature – it will take in aMapandCameraupon 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.