I'm implementing a game in Java, using the classes shown below to control the game logic. I try to be very clear when explaining my question.
GamePanel
I use this class to start the game loop with a thread (only game loop)
public void run() { init(); //initialize gamePanel components // game loop while (running) { start = System.nanoTime(); gameManager.update(); KeyInput.update(); MouseInput.update(); gameManager.draw(graphic); Graphics g2 = getGraphics(); g2.drawImage(image, 0, 0, null); g2.dispose(); elapsed = System.nanoTime() - start; wait = targetTime - elapsed / 1000000; if (wait < 0) wait = 5; try { Thread.sleep(wait); } catch (Exception e) { e.printStackTrace(); } }
GameManager
I made this class to control each aspect of the game as (update,graphics)
private void loadState(int state) { if (state == States.MENU.getValue()) gameState = new Menu(this); else if (state == States.GAME.getValue()) gameState = new Game(this); else if (state == States.PATHSELECT.getValue()) gameState = new SelectPath(this); } public void update() { if (gameState != null) gameState.update(); } public void draw(java.awt.Graphics2D graphic) { if (gameState != null) gameState.draw(graphic); else { JOptionPane.showMessageDialog(null, JOptionPane.ERROR_MESSAGE); } }
Gamestate
GameState is an abstract class that receives, as a constructor parameter, an instance of GameManager and it has abstract methods implemented in each GameState (Menu,Game,MultiPlayer,etc..)
this class has these abstract methods:
controller() -> that verify the state logic
update() -> in case of the currentState is a MultyPlayer , this method call the update method of the class Player , etc...
draw(Graphics g) -> draw to screen the objects of the currentState
I thought of making a utility class singleton that implements all the draw() of each state, but it is very nasty to insert all in a single class.
Example:
instead of calling gameState.draw(graphic);
I call Render.getRenderInstance().draw(graphic,gameState);
this solutions works, but I don't like it.
So how can I divide the draw method from the rest of the logic in a pretty way?
Some advice? Thank you.