• Post Reply Bookmark Topic Watch Topic
  • New Topic

Additional methods from specific classes that implement an interface  RSS feed

 
Stephen Bell
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't there supposed to be a response() method in interface Scene?

You could create an abstract class, for example class AbstractScene, that implements interface Scene (it doesn't need to have actual implementations for all the methods in interface Scene). Make your Level classes extend that class, instead of having them implement interface Scene directly. In AbstractScene, implement the response() method to do nothing. In the relevant Level classes, override the response() method to do whatever needs to be done.
 
Stephen Bell
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper

No, I removed response() from the interface Scene because I didn't want to put stubs into all the classes that didn't require it.

I'm still a little confused. I can actually put a local response(); method into my Level classes. But the issue I'm having is how to call them from my MainGame class.

I simply want to be able to say 'call response(); from whatever scene is currently set' rather than having to say if Level is 1 then call it from level1 etc...
I may have misunderstood your reply, please could you elaborate on how I could call the method implemented as you describe - cheers
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!