Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
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  
  Basic game queue in a turn-based game  (Read 1099 times)
0 Members and 1 Guest are viewing this topic.
Offline aankhra

Junior Newbie





« Posted 2006-01-03 11:24:34 »

Hi All!

I want to make a turn-based game and i don't know how to notify my Player/Game object about the input from the player.

My main routine should be:

- wait for input from the player
- process input
- process npcs
- repaint the screen.

But how should i send the result of the keyListener to the Game object? Dont want to do it with a global variable.

What should be the best way to do this?

cu,
aankhra
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #1 - Posted 2006-01-03 13:05:14 »

Here is a loop:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public static void main(String[] args) {
      Main main = new Main();
     
      while (!main.exit) {
         main.game.changeResolution(main.frame.getWidth(), main.frame.getHeight());
         main.input();
         main.logic();
         main.render();
         
         try {
            Thread.sleep(main.timeToSleep);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
     
      System.exit(0);
   }


Sending Input to Game:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
private void input() {
      if (mouseClick != null) {
         getDeviceCoords(mouseClick);
         game.mouseClick(mouseButtonPressed, mouseClick);
         mouseClick = null;
      }
     
      if (mouseLocation != null) {
         getDeviceCoords(mouseClick);
         game.mouseLocation(mouseLocation);
      }
   }


After this you may want to create class or a collection that stores the inputs and the process them within the logic.

Like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
                       if(!plays.isEmpty())
            for (Point click : plays)
               world.playCard(player, click);
         
         if (!discards.isEmpty())
            for (Point click : discards)
               world.discardCard(player, click);

             if(turn.isPlayersTurn(ai)) {
         //TODO: add more advanced AI
     }


Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline aankhra

Junior Newbie





« Reply #2 - Posted 2006-01-04 21:56:04 »

OK,

i assume, that your input() method implements a kind of a (Mouse)Adapter. My game will be controlled only by the keyboard. But how to notify the main object about the input? I want to pass the action (like Player.GO_WEST, or Player.USE_SKILL) to the game.logic()
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #3 - Posted 2006-01-05 07:44:26 »

ArrayList<KeyEvent> keysPressed = new (...);

public void keyPressed(...){
        keysPressed.add(evt);
}


public void input(){
      for(KeyEvent keys : keysPressed){
            switch(keys.getKeyCode()){
               case KeyEvent.VK_UP:
                   game.movePlayerUp();
                   break;
            }
      }
}


Make sure you add the required listeners to your class.

"class Main implements KeyboardListener"

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
BrassApparatus (8 views)
2013-06-19 08:52:37

NegativeZero (14 views)
2013-06-19 03:31:52

NegativeZero (17 views)
2013-06-19 03:24:09

Jesse_Attard (20 views)
2013-06-18 22:03:02

HeroesGraveDev (62 views)
2013-06-15 23:35:23

Vermeer (61 views)
2013-06-14 20:08:06

davedes (61 views)
2013-06-14 16:03:55

alaslipknot (55 views)
2013-06-13 07:56:31

Roquen (77 views)
2013-06-12 04:12:32

alaslipknot (60 views)
2013-06-10 19:30:18
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
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!