Hi all
I am writing a game in
Java for Android (although my question isn't Android or Game Dev specific).
I have a
SceneManager class and a
Scene interface and then various other classes that implement the Scene interface (Code at the end of this post).
Basically, in my MainGame class (which also implements the Scene Interface for Touch Event capturing purposes) I hold the bulk of my game code. Methods in this class are then called from my Level classes. (most of these are needed in all levels so it makes sense to hold them here and call them from the levels to eliminate unnecessary code duplication)
So, I have Level1, Level2......... Level20 classes which all implement
Scene.
Now, the problem comes because in only 2 of my Levels something can happen (that can't in the other 18) and I need to run a response method in these 2 levels (the method isn't exactly the same, the response to this event happening is different for both levels).
To run common methods from my classes, I use my Scene Manager like this:
(The above is from my gameloop) - So it will run the updateLogic(); and render(); methods from whichever is the current scene (Level).
Scene is changed like so:
This works great as all Level's have an updateLogic(); and render(); method.
So from my mainGame class, I am doing something like : (pseudo code)
The above would be called from my 2 level classes. So something like:
I don't really want to have this (second) 'if' statement here in the middle of my performance critical game loop.
What I'm after is something like this:
However, this would require me to put stubs in the other 18 classes.
I'm thinking there must be a way to do this as the SceneManager already knows the current scene so it seems a waste checking it again via an if (or switch) statement. What is the best way to do this without having to put stubs into classes that don't require this method?
Appreciate any insight - thanks!!
Code
SceneManager
Scene