Hey,
I am thinking much about modeling an efficient game the last time. I’m using Java since six months and making games since about 5 years, but this is the first time I want to realize a whole game in Java. I planned a Space-Shooter MMORPG with Java7, Slick2D and Kryonet.
http://img7.imagebanana.com/img/ym25vslm/thumb/dv.png
The only problem that I have right know is that I dont know how to switch between main menu, login screen, option panel, pause screen and The Game. This is some of my current code:
static HashMap<String, Image> i = new HashMap<String, Image>(); // images
static HashMap<String, AngelCodeFont> f = new HashMap<String, AngelCodeFont>(); //fonts
static ArrayList<Entity> e = new ArrayList<Entity>();
@Override // entities
public void init(GameContainer c) throws SlickException {
Network.load(); // Init Kryonet
Network.tcp(new Start()); //send first packet to check for updates
try { // load all pictures found in the image directory
for (String n : new File(ClassLoader.getSystemResource("i").toURI()).list())
i.put(n.substring(0, n.indexOf('.')), new Image("i/" + n));
i.put("clickable1", i.get("clickable0").getFlippedCopy(true, false));
} catch (URISyntaxException e) {
e.printStackTrace();
}
try { // load all ACFonts found in the font directory
for (String n : new File(ClassLoader.getSystemResource("f").toURI()).list()) {
if (n.endsWith("t")) {
n = n.substring(0, n.indexOf('.'));
f.put(n, new AngelCodeFont("f/" + n + ".fnt", "f/" + n + ".png"));
}
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
// (main menu)
// (login screen)
start(); // game start
}
void start() {
Interface.create(); //create instances of buttons
Cursor.load(); // load cursor sprite
Player p = new Player(Ship.SHIPID_SHUTTLE, 5, 5); // create new shuttle at 5, 5
e.add(p); // make shuttle runnable
}
@Override
public void update(GameContainer c, int delta) throws SlickException {
Interface.run(); // update all components
for (ListIterator<Entity> n = e.listIterator(e.size()); n.hasPrevious();) {
if (!n.previous().run()) // boolean(), so entities can say that they want to get removed
n.remove();
}
Cursor.run(); // update cursor
}
@Override
public void render(GameContainer c, Graphics g) throws SlickException {
for (Entity n : e) {
n.render();
}
Interface.render();
Cursor.render();
}
This works fine, but what should I do to only run an instance of a MainMenu class at first?
I could create an int gamestate and do a switch(gamestate) at render() and update(),
but I would come up with an undynamic clump of bad written code.
I’d be thankful for every approach and every source improvements.