/UPDATE:
I took one of the suggested solutions and tried to implement them. I implemented a BaseGameWorld
class that contains the basic tetris gameplay. Every game mode extends this base class. If any of the game modes differ from that classic gameplay they will simply override any methods needed or add new ones. My GameScreen
class is now structured like this:
public class GameScreen {
private BaseGameWorld gWorld;
private GameRenderer gRender;
public GameScreen (int gameMode) {
// PuzzleGameMode extends BaseGameWorld
gWorld = new PuzzleGameMode();
// Constructor of GameRenderer takes a BaseGameWorld object
gRender = new GameRenderer(gWorld);
}
public void render() {
// Update Game World
gWorld.update();
// Draw Game World
gRender.draw();
}
}
This is ofcourse not the complete code, but it should show everything important. The idea behind the GameWorld
and the GameRenderer
classes is to reduce any clutter in the GameScreen
class and seperate the game world and the drawing of the game world in two seperate steps with the goal to improve maintainability. So far this approach works quite well.
The type gWorld
get's initiated with depends on the gameMode
input variable and is handled through a switch-case in the actual code. Just as an example I used the class PuzzleGameMode
.
Now my problem is that any new things added in the PuzzleGameMode
class can't be drawn inside the GameRenderer
class, since the GameRenderer
constructor takes a BaseGameWorld
object. It can only draw things that are in the BaseGameWorld
.
Is there a way to resolve this situation that doesn't involve making a seperate GameRenderer
for every game mode? My hope was to only have different classes for each game mode but to have one universal GameRenderer
class.
I am currently making a tetris clone to get back into game programming using libGDX. The game is currently seperated in different screens. MenuScreen
(includes Options, Highscore, Credits etc.) and the GameScreen
itself. The GameScreen
also has one member (named GameWorld
) that manages all the calculations and game states, aswell as another one (named GameRenderer
) that does all the drawing.
This works fine for the moment, however I plan on adding multiple game modes. I could probably add them to the GameWorld
class, but it is already very full and managing multiple game modes inside a single class would end up very confusing and not very nice to maintain later down the line.
I could make a different class for every game mode and just use one of them depending on the selected game mode. This would be a solution, however this seems like a waste since I would just copy most of the original code for some of the game modes.
Alternativly I could make a Superclass that all game modes inherit from, however I am not sure how well it would work since game modes can differ quite a bit from each other. One mode might change the fundamentals like collision or intoduce new mechanics that need to be implemented on a deeper level, while others only differ a bit from the classic tetris mode and use most of the already implemented stuff.
What would be a good way to implement different game modes?
If more information is needed or if I should specify something better, just tell me and I will add the missing information. :)
BaseGameWorld
class that contains the basic Tetris gameplay I had implemented and other game modes override methods from that class. However I ran into a bit of a problem trying to implement it. MyGameRenderer
class takes aBaseGameWorld
object in the constructor. When I add something in one of the game modes I can't render the added things since theGameRenderer
class can ofcourse only use things that are present in theBaseGameWorld
. \$\endgroup\$BaseGameWorld
to render its contents, or just a subset, list of objects/rendering requests? Perhaps you could make someGetContentToRender
method and override it as well, or just have a list of game objects/prepared rendering information and pass it toGameRenderer
instead of wholeBaseGameWorld
. \$\endgroup\$