Reputation: 78
I am working on making a game where each level has different mechanics, but I want to keep all the input handling, rendering, etc in one place. My approach is to have a Level
class with all the needed methods, then each level is its own class (Intro
, BossRoom
, etc) that extends Level
and override methods to change the parts that need to change for each without having to copy over all of the contents of Level
for each class.
I have a LevelManager class that will hold all of the reference to the levels, and I was wanting to use ArrayList
to do this.
I've tried using ArrayList<Level>
, ArrayList<?>
, ArrayList<? extends Level>
, and ArrayList<? instanceof Level>
both when I declare and instantiate the variable. Nothing I've tried seems to work. Is there something is Java that does this or should I just have each Level
as a variable?
Sorry for block of text, I'm new to SO and do not know a better way to word it.
Upvotes: 0
Views: 82
Reputation: 131346
It should rather be straight. Here is a little example.
Base class :
public abstract class Level{
....
}
Edit : You could declare the Level
class as abstract if you deem that it should not (no sense) or it cannot be (at least one method is abstract ) instantiated in your application. According to your context, it is likely.
Child class :
public class IntroLevel extends Level{
...
}
Other child class :
public class LevelOne extends Level{
...
}
Your Level Manager :
public class LevelManager{
private List<Level> levels;
// constructor
public LevelManager(){
levels = new ArrayList<Level>();
levels.add(new IntroLevel());
levels.add(new LevelOne());
...
}
}
Upvotes: 1