0

I'm currently working on a project to make a simple 2d platformer in Java for uni, and the way we've been suggested to handle levels is to create an abstract level class, and then other classes implementing it for each individual level, so:

abstract class GameLevel {}
class Level1 extends GameLevel {}
class Level2 extends GameLevel {}
etc...

And then in my main class, I have a variable to keep track of the current level number, and code to move on to the next level, which essentially is:

public void goNextLevel() {
    world.stop();
    if (level == FINAL_LEVEL) {
        // win code
    } else {
        level++;
        world = new Level/*next level number*/();
    }
}

And I was wondering if there was a way to take the variable containing the level number and add it on to the end of the class call, to save having to have a stack of if else statements.

Thanks for any help in advance

edit: The reason I'm not keen on putting all of it in one class and then using a variable in that to change the level is that each level has to have a relatively large number of entities, which each take a few lines to set up, and adding all of the levels in one file would lead to hundreds of lines of code which would be difficult to maintain

JBrown521
  • 94
  • 9
  • 1
    There is no metaprogramming in java. Why not making level a field in your class? – Andronicus Mar 07 '19 at 13:48
  • 1
    Are your levels actually only different by their level number? or are they functionally different? – khelwood Mar 07 '19 at 13:50
  • Instead of an if-else-cascade you could simplify your code by using a switch statement – obecker Mar 07 '19 at 13:50
  • 2
    There are design patterns for such problems. Search for pattern descriptions that have “factory” in their name. – Ole V.V. Mar 07 '19 at 13:54
  • Each level has to create a bunch of different platforms and enemies and the like, which is relatively simple in code, but there's enough of a difference that they can't be the same, and combining them would probably lead to a class hundreds of lines long, which would be a pain in the butt to maintain – JBrown521 Mar 07 '19 at 13:54
  • Possible duplicate of [Create new class from a Variable in Java](https://stackoverflow.com/questions/1268817/create-new-class-from-a-variable-in-java) – PM 77-1 Mar 07 '19 at 13:54
  • Thanks for adding depth, it’s welcome. Please [edit your question](https://stackoverflow.com/posts/55045329/edit) and add information in it so we have everything in one place. Thank you. – Ole V.V. Mar 07 '19 at 13:55
  • create an array of `GameLevel` and access it by index `level`. – luk2302 Mar 07 '19 at 13:58
  • 1
    @Andronicus Java absolutely supports doing this; it's just not a great idea. – chrylis -cautiouslyoptimistic- Mar 07 '19 at 14:24
  • 1
    Generally speaking, these levels shouldn't be different *classes*; they're all the same sort of thing, just with different data. The layout, enemies, etc., are all properties of `GameLevel`, and you would usually instantiate each level with a description from an input such as a file (exactly as level editors work). – chrylis -cautiouslyoptimistic- Mar 07 '19 at 14:25

2 Answers2

0

You would do so:

      try {
         final Class<?> clazz = Class.forName("your.package.Level" + levelInteger);
         final Constructor<?> ctor = clazz.getConstructor();
         final GameLevel level = (GameLevel) ctor.newInstance();
         System.out.println(level.getClass().getName());
      } catch (final Exception e) {
         e.printStackTrace();
      }

Source and explanation: https://stackoverflow.com/a/6094602/8160563

Marvin Klar
  • 1,869
  • 3
  • 14
  • 32
0

Assuming that there in only ever one object of each LevelN class, the simplest thing to do would be to store them into an array

GameLevel[] levels = new GameLevel[5];
levels[0] = new LevelOne();
levels[1] = new LevelTwo();

And then have a little helper method to retrieve them

GameLevel getNextLevel(int currentLevel) {
    return levels[currentLevel];
}