Hello everyone and thanks for the help in advance.
I am working on a clone of Minecraft in Rust, and I am confronted with an architecture problem.
I can gladly provide an playground to illustrate it, but a picture is probably the easiest. The diagram below just shows the references between the classes that I would like to have
I know that this is impossible in Rust, but I am not sure of what would be the idiomatic way to do this. Nothing simple comes to my mind.
- The
WorldRenderer
is the OpenGL main loop. It captures keyboard events and therefore must be able to modify both the camera (position, orientation) and the world (add and delete cubes). - The
Camera
struct must be able to access the model, for instance to adapt the speed & acceleration depending on the position of some cubes (collision detection, free-fall). So it needs to have an immutable access to the world. Also, it can't be created at each render iteration because it has some internal state (eg the speed) that outlives the render iteration. - The
World
just contains a description of the world...
So, I know that the problem comes from the reference from the camera to the world, but I really don't think of a way to get ride of this one. I tried to delete the reference &World
inside Camera
and instead to use a callback (of type is_position_free: Box<dyn Fn([f32;3])->bool + 'a>
and constructed as Camera::new(|pos| world.is_position_free(pos))
) but, as I expected, it still didn't work for the same problem.
Of course I know that I can inline the code of Camera
inside WorldRenderer
, but it would really become nasty, and I would like a solution that respects good coding practice.
Let me know if I can provide more info (I actually have the code on my public github if it helps GitHub - arthurBricq/crafty: (the beggining of a) Minecraft clone in pure Rust with OpenGL)
Thanks in advance