Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (843)
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 1206 times)
0 Members and 1 Guest are viewing this topic.
Offline Sanzeinga

Senior Newbie

« Posted 2009-03-13 00: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?

private void movementInputHandler(Input input){
      if (playerNumber == 1){
      else {
   private void p1MovementInputHandler(Input input){
      if (input.isKeyDown(Param.P1_8))
         if (input.isKeyDown(Param.P1_4)){

I can elaborate more if needed

Of and if this seems like unnecessary nitpicking, i plan to have lots and lots of inputs
   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)
   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: 11
Exp: 10 years

Game Engineer

« Reply #1 - Posted 2009-03-13 16: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 22:29:52 »

Alright, thanks
Pages: [1]
  ignore  |  Print  

EgonOlsen (37 views)
2018-06-10 19:43:48

EgonOlsen (20 views)
2018-06-10 19:43:44

EgonOlsen (39 views)
2018-06-10 19:43:20

DesertCoockie (186 views)
2018-05-13 18:23:11

nelsongames (122 views)
2018-04-24 18:15:36

nelsongames (121 views)
2018-04-24 18:14:32

ivj94 (861 views)
2018-03-24 14:47:39

ivj94 (122 views)
2018-03-24 14:46:31

ivj94 (758 views)
2018-03-24 14:43:53

Solater (139 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!