Reputation: 1358
Short version: Please link me to a orginized code of a libgdx game with box2d or just tell me what members go in what classes
Long version: I have a GameScreen with GameStage object inside it. However as I develop my game, I see that GameStage has a lot of code while GameScreen is almost empty. Initially I put camera and viewport inside GameScreen, but that's it.
private Jumper game;
private GameStage stage;
private OrthographicCamera camera;
private FitViewport viewport;
Here are members that are inside my GameStage:
public World world;
private Ground ground;
private Player player;
private float accumulator = 0f;
private Vector3 touch = new Vector3();
private Rectangle rightSide;
private Rectangle leftSide;
private Queue<Platform> platforms;
private int score;
private OrthographicCamera camera;
private Box2DDebugRenderer debugRenderer;
private FitViewport viewport;
That's not that bad for me atm, however I'm only at the prototype build and there is so much more mechanics to be added, I'm sure that it will end up like in my previous game (50+ members), and have so much code that I will get lost in it everytime
Is it normal for a game to have so much code? Or is there a way to optimize this?
PS: Here's my class structure: https://i.sstatic.net/8uYgA.png
Upvotes: 0
Views: 486
Reputation: 210
I'm in the process of making a Box2D game as well, and organization was a big concern of mine. In a game, there are a lot of things you have to manage. The camera, game logic, moving entities, etc. To get this to all work together, I've started to use what I call handlers. These handlers will handle a specific aspect of the game. This allows me to keep camera code away from Box2D code.
For the project I'm working on right now, I have five handlers inside my "Universe" class:
public WorldHandler world_handler;
public MapCameraHandler map_camera_handler;
public EntityHandler entity_handler;
public LevelHandler level_handler;
public WaveHandler wave_handler;
When I initialize each of them, I pass a reference to this Universe class to their constructors so the handlers can communicate with each other. Then, during my update step, I call each handler's update individually.
public void update(float delta)
{
world_handler.update(delta);
entity_handler.update(delta);
map_camera_handler.setCameraBounds();
wave_handler.update();
}
I do the same for the draw method.
public void draw()
{
map_camera_handler.map_renderer.setView(map_camera_handler);
map_camera_handler.map_renderer.render();
entity_handler.draw();
}
Upvotes: 1