OO Games - General theory

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.

How about each screen is a class that implements a Screen interface that has the update and render methods? Then your main class could just update and render the current Screen until it is changed.

I have a screen object


public class Screen {
   public Screen(GameMain) {
   }

   public void render(Graphics) {
   }

   public void update(delta) {
   }

and in my gamemain class I have a HashMap holding all the different screens, mapped to an enum of screens. Like <SCREEN.TITLE, new TitleScreen(this), and a Screen object, holding the current active screen.
Each render, I just go


activeScreen.render(g);

and it gets passed down.

From screen, you can go back and change the screen if something is clicked


GameMain.setActiveScreen(SCREEN);

To the OP: You mention you want to use Slick. Slick already supports GameStates, where each GameState can be in fact a separate screen.
So you have one GameState subclass for the title screen, one for the game itself, one for options and so on.
Look up the GameState test case inside Slick. It’s all there.

Aha! I knew Slick2D supported game states, I just forgot where hehe: http://slick.cokeandcode.com/javadoc/org/newdawn/slick/state/package-summary.html

Ah, thanks, that’s what I searched for. :slight_smile:


http://img6.imagebanana.com/img/5w2jorzl/thumb/image.png

Is there any tutorial or example that shows how to create transitions?

A transition is basically a State that applies a transform the previous State, and then applies a transform to the next State.
A basic transition would be fade-in/fade-out, pseudocode:


public class FadeTransition extends State {
    private Game game;
    private State previous, next;
    
    private double fadeValue, delta;
    private final double END_VALUE;
    
    public FadeTransition(Game game, State previous, State next) {
        this.game = game;
        this.previous = previous;
        this.next = next;
    }
    
    public update() {
        fadeValue += delta;
        
        if(fadeValue >= END_VALUE)
            delta = -delta;
        if(fadeValue <= 0)
            game.setState(next);
    }

    public void render() {
        if(delta > 0)
            //draw previous state with translucent black over it
        else
            //draw next state with translucent black over it
    }
}

I’m going to repeat myself: it’s already done in Slick.

See here: https://bob.newdawnsoftware.com/repos/slick/trunk/Slick/src/org/newdawn/slick/state/transition/

10 transitions are already implemented and should be helpful enough to create your own ones.

Keep in mind when you use them that it might occur that your GameState.render() method is called before(!) an update() happened! Some transitions already call render() of your “target” state to create some nice transition effect. So you’ll have to take care that your target GameState is able to be rendered without being updated.
Normal FadeIn/FadeOutTransitions won’t require this.

Hope that helps,
Tommy

Wow…Slick already has transitions and I wasted my time writing that pretty class T___T

The already implemented transitions seems to be sufficient for my purposes, thanks. :slight_smile: