Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  OO Games - General theory  (Read 2130 times)
0 Members and 1 Guest are viewing this topic.
Offline jaid

Senior Newbie





« Posted 2011-11-21 22:59:47 »

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.

 

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:

1  
2  
3  
   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>();
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
@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

   }
1  
2  
3  
4  
5  
6  
7  
8  
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

   }
1  
2  
3  
4  
5  
6  
7  
8  
9  
@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
  }
1  
2  
3  
4  
5  
6  
7  
8  
@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.
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-11-22 00:06:26 »

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.

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #2 - Posted 2011-11-22 00:07:48 »

I have a screen object
1  
2  
3  
4  
5  
6  
7  
8  
9  
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
1  
activeScreen.render(g);

and it gets passed down.

From screen, you can go back and change the screen if something is clicked
1  
GameMain.setActiveScreen(SCREEN);

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline thaaks

Junior Member


Medals: 1
Projects: 1



« Reply #3 - Posted 2011-11-22 07:23:40 »

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.

Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-11-22 08:10:13 »

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

Offline jaid

Senior Newbie





« Reply #5 - Posted 2011-11-24 23:20:22 »

Ah, thanks, that's what I searched for. Smiley



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

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-11-25 03:23:24 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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
   }
}

Offline thaaks

Junior Member


Medals: 1
Projects: 1



« Reply #7 - Posted 2011-11-25 09:48:22 »

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

Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-11-25 10:04:54 »

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

Offline jaid

Senior Newbie





« Reply #9 - Posted 2011-11-27 02:53:32 »

The already implemented transitions seems to be sufficient for my purposes, thanks. Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!