Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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  
  Best way to deal with multiple player inputs  (Read 719 times)
0 Members and 1 Guest are viewing this topic.
Offline Sanzeinga

Senior Newbie





« Posted 2009-03-13 01:05:17 »

(using slick, if that matters)

The input comes in the form of an Input object that notes every key that is pressed.
I have a Player object that keeps track of each ( theres only 2 right now ) player's stuff for example their character ( assume its like a space shooter game or something i guess )
The Player object is passed the Input object and tells the containing Character object ( contains the sprites and velocities etc ) how to move by calling a move method inside the Character object.
EX: in one update i pressed forward and right ( diagonally up right ) for player one.  The input is passed to player 1 first, which now has to read that forward and right for player one are pressed and then tells the Character to move in that direction.  My problem is, i have gigantic if statements to deal with the movements in the first place but, now with an additional player theres multiple sets of gigantic if statements since the player has no way to tell if the inputs are for player 1 or for player 2 without checking each one every time.  Is there a speedy(as of yet im unsure of things related to speed and how things can tax the system but this game will require very precise movements and thus maximum framerates and update rates, and it is planned for there to be many objects on screen which means lots of intersections) yet clean way to deal with this?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
private void movementInputHandler(Input input){
      if (playerNumber == 1){
         p1MovementInputHandler(input);
      }
      else {
         p2MovementInputHandler(input);
      }
   }
   private void p1MovementInputHandler(Input input){
      if (input.isKeyDown(Param.P1_8))
      {
         if (input.isKeyDown(Param.P1_4)){
            chara.move7();


I can elaborate more if needed

Of and if this seems like unnecessary nitpicking, i plan to have lots and lots of inputs
Move
   NW, N, NE   ( 7 8 9 )
   W, E      ( 4    6 )
   SW, S, SE   ( 1 2 3 )



Basic Action
   Shoot 1 - defaulted to energy projectile (A)
   Held shoot 1
   rapid shoot 1 ? (AAA)
   Shoot 2 - defaulted to ballistic projectile (B)
   Held shoot 2
   rapid shoot 2 ? (BBB)
   Action X, held, rapid (X)
   Action Y, held, rapid (Y)
   
   
Modifier
   Dash modifier (D)
   Melee modifier (M)
   Special modifier (S)
   Defensive modifier (?)

a complicated input could be moving in the NE direction while dashing, and turning that into a dashing melee attack ( 2 direction, dash modifier, melee modifier, shoot1 button)
(its meant to be played on controller since keyboards choke on this stuff, but one step at a time...)

I'm open to all kinds of suggestions, and a variety would be good ( unless theres a single best way to deal with this ).  Maybe i should just keep all that if statement clutter and fold it away?

This is heavily influenced by a certain game, for those who notice
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #1 - Posted 2009-03-13 17:54:26 »

I think you're close to doing this the "right" way. Typically you want to have a Player object that contains a set of fields which denote all the different input types it can use. Say it has up, right, down, left, and fire fields. Then when you first create the Player object you set all those values to match the key codes of the keys you want to use for that Player. Then you do the same for the next Player, and so on and so forth. Each Player is attached to a Character or a Unit or whatever you want to call it which receives the resultant input. So it's very similar to what you have, it's just that you modularize the keys so that a) each Player knows nothing about the other Player's keys, and b) the user can assign keys to different actions if you put in UI for this.

There's really no way to get around a lot of if statements for each key you use. Some people do things like store the combination of the movement key presses as a vector and then use that later, but in the end this is giving you the same number of if statements anyway.

See my work:
OTC Software
Offline Sanzeinga

Senior Newbie





« Reply #2 - Posted 2009-03-13 23:29:52 »

Alright, thanks
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.

xsi3rr4x (22 views)
2014-04-15 18:08:23

BurntPizza (17 views)
2014-04-15 03:46:01

UprightPath (31 views)
2014-04-14 17:39:50

UprightPath (15 views)
2014-04-14 17:35:47

Porlus (31 views)
2014-04-14 15:48:38

tom_mai78101 (57 views)
2014-04-10 04:04:31

BurntPizza (115 views)
2014-04-08 23:06:04

tom_mai78101 (214 views)
2014-04-05 13:34:39

trollwarrior1 (182 views)
2014-04-04 12:06:45

CJLetsGame (189 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!