Basic game queue in a turn-based game

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

Here is a loop:

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:

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:


                       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
		}

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()

ArrayList 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”